分类: Android平台
2014-09-23 00:34:59
点击(此处)折叠或打开
点击(此处)折叠或打开
Q_PROPERTY(QString notification READ notification WRITE setNotification NOTIFY sigNotificationChanged)一直以来将Qt视作一个基本C++库,所以对于Q_PROPERTY宏也为留意,但是现在看来开发QML时使用这些特性可以避免一些awkard approaches。
点击(此处)折叠或打开
package org.qtproject.example.notification;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.app.Activity;
import android.app.Service;
import android.os.Vibrator;
import android.os.PowerManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
{
private static NotificationManager m_notificationManager;
private static Notification.Builder m_builder;
private static NotificationClient m_instance;
private static Vibrator m_vibrator;
private static AlarmManager m_alarmManager;
private static PowerManager m_powerManager;
private static PowerManager.WakeLock m_wakeLock;
public NotificationClient()
{
m_instance = this;
// m_powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
// m_wakeLock = m_powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
// "MyWakelockTag");
}
public static void wakeLockAcquire(){
if(m_powerManager == null)
m_powerManager = (PowerManager)m_instance.getSystemService(Context.POWER_SERVICE);
if(m_wakeLock == null){
m_wakeLock = m_powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"NotifierWakeLock");
}
m_wakeLock.acquire();
return;
}
public static void wakeLockRelease(){
if(m_wakeLock != null){
m_wakeLock.release();
m_wakeLock = null;
}
}
public static void notify(String s)
{
if (m_notificationManager == null) {
m_notificationManager = (NotificationManager)m_instance.getSystemService(Context.NOTIFICATION_SERVICE);
m_builder = new Notification.Builder(m_instance);
m_builder.setSmallIcon(R.drawable.icon);
m_builder.setContentTitle("A message from Qt!");
}
m_builder.setContentText(s);
m_notificationManager.notify(1, m_builder.build());
}
public static void vibrate(long[] pattern,boolean isRepeat) {
if(m_vibrator == null){
m_vibrator = (Vibrator) m_instance.getSystemService(Context.VIBRATOR_SERVICE);
}
m_vibrator.vibrate(pattern, isRepeat ? 1 : -1);
}
public static void vibrate(boolean isRepeat) {
if(m_vibrator == null){
m_vibrator = (Vibrator) m_instance.getSystemService(Context.VIBRATOR_SERVICE);
}
m_vibrator.vibrate(new long[]{100,10,100,100},isRepeat ? 1 : -1);
}
}
点击(此处)折叠或打开
点击(此处)折叠或打开
void NotificationClient::sltUpdateAndroidNotification()
{
QAndroidJniObject javaNotification = QAndroidJniObject::fromString(m_notification);
QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/NotificationClient",
"notify",
"(Ljava/lang/String;)V",
javaNotification.object<jstring>());
}
void NotificationClient::sltVibrate(){
QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/NotificationClient",
"vibrate",
"(Z)V",
false);
}
void NotificationClient::sltWakeLockAcquire(){
QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/NotificationClient",
"wakeLockAcquire",
"()V");
}
void NotificationClient::sltWakeLockRelease(){
QAndroidJniObject::callStaticMethod<void>("org/qtproject/example/notification/NotificationClient",
"wakeLockRelease",
"()V");
}