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

标签 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") 没有。那么我是否以某种方式安排了警报不正确?

最佳答案

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

这是警报管理器设置的一个工作示例。是的,我知道它的不同,但它是值得比较的东西

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);

关于android - 无法在 Android 上使用 AlarmManager 安排通知(使用 Qt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33901869/

相关文章:

android - 在查询中将长转换为日期

c++ - Visual Studio 何时生成 32 位 .obj 文件和 64 位 .obj 文件?

javascript - 如何从一个 QtScript .js 文件调用另一个 QtScript .js 文件

android - 本地通知问题

android - 如何同时显示两个单独的通知?

java - 调用notifyItemChanged后,适配器不会收到通知,并且onBindViewHolder不会被调用

java - Hp LaserJet CP1025nw 彩色打印机无法使用连接了 Otg 电缆的 USB 进行打印

java - Android 多个时间选择器

c++ - QGLWidget updateGL() 不使用 QTimer 更新?

android - 如何防止警报在启动时运行耗时