无法在Android上使用AlarmManager安排通知(使用Qt)

时间:2015-11-24 19:09:43

标签: android qt alarmmanager

我从qt 5.5开始做以下操作。项目

我正在尝试使用android中的闹钟管理器安排本地通知。这是安排通知的代码:

class ScheduledNotifications {
    static public int notification_id = 0;
    static int scheduleNotification(String title, String content, int futureInMilliseconds) {
        ++notification_id;

        Intent notificationIntent = new Intent(QtNative.activity(),  NotificationPublisher.class);
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, notification_id);
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, createNotification(title,content));
        PendingIntent pendingIntent = PendingIntent.getBroadcast(QtNative.activity(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager)QtNative.activity().getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, /*futureInMilliseconds*/0, pendingIntent);

        Log.d("!" ,"Scheduled");
        return notification_id;
    }

    static public Notification createNotification(String title, String content) {
            Notification.Builder builder = new Notification.Builder(QtNative.activity());


            builder.setContentTitle(title);
            builder.setContentText(content);
            return builder.build();
    }
}

这是NotificationPublisher,它应该显示通知:

class NotificationPublisher extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification-id";
    public static String NOTIFICATION = "notification";

    public void onReceive(Context context, Intent intent) {//Called when its time to show the notification ...

        Log.d("!", "Notified");
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);

    }
}

对于调试傀儡,我将“唤醒”时间设置为0(因此通知应立即显示)。

Lod.d("!","Scheduled")输出显示在控制台输出中,但Log.d("!", "Notified")没有。所以我安排闹钟不正确吗?

2 个答案:

答案 0 :(得分:2)

老实说,不是100%肯定你为什么不工作,但我怀疑它是0传入。我认为这应该是System.currentTimeInMillis()+ SOME_SHORT_INTERVAL?

这是Alarm Manager设置的一个工作示例。是的,我知道它的不同,但它是值得比较的东西

Intent syncIntent = new Intent(this, SyncIntentService.class);
       syncIntent.putExtra(EXTRA_REQUEST_KEY,   
       SyncRequestTypes.REQUEST_BACKGROUND_SYNC.name());

    PendingIntent pi = PendingIntent.getService(this, 0, syncIntent,
      Intent.FLAG_ACTIVITY_NO_USER_ACTION);

    // Register first run and then interval for repeated cycles.


   alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + DEFAULT_INITIAL_RUN_TEST,
            DEFAULT_RUN_INTERVAL_TEST, pi);

答案 1 :(得分:2)

我在AndroidManifest.xml中遇到错误。 NotificationPublisher需要注册为接收者,如下所示:

<receiver android:name="de.goodpoint_heidelberg.NotificationPublisher" android:enabled="true"/>