是否可以设置Android通知或以后在应用未运行时触发的日期和时间?

mas*_*ary 3 java notifications android

根据我的阅读,似乎这样的代码需要应用程序在线程中运行,直到通知触发.我需要在稍后的日期和时间触发通知,以便用户像任何其他通知一样看到通知,然后单击它并打开一个活动,传入一些数据,以便应用知道该做什么.

如何在没有应用程序一直运行的情况下几天后发出此通知?

我是否等待完成此操作?

long millis = 60000;
myNotification.wait(millis);
Run Code Online (Sandbox Code Playgroud)

这是我的代码立即触发

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.drawable.star)
.setContentTitle("How was " + me.getString("EventTitle") + "?")
.setContentText("Click here to leave your review");

Intent resultIntent = new Intent(getActivity(), SetupActivity.class);

PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getActivity(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);

    mBuilder.setContentIntent(resultPendingIntent);

    int mNotificationId = me.getInt("EventID");
    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr = 
            (NotificationManager) getActivity().getSystemService(getActivity().NOTIFICATION_SERVICE);


    // Builds the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

Com*_*are 5

正如A - C写的那样,AlarmManager用来安排PendingIntent在你想要的时间调用一个.您可能会使用RTC_WAKEUP警报,意思是"时间基于System.currentTimeMillis(),如Calendar使用"和"让我们将设备唤醒进入睡眠模式".如果您不需要执行任何磁盘或网络I/O来获取用于自身的信息,则可以使用广播PendingIntent,并且您的Notification代码可以进入该onReceive()方法Notification.

请注意,在Android 4.4中,规则AlarmManager有所改变.您将需要setExact()在Android 4.4及set()更早版本的Android上使用.