是否有可能设置一个Android通知或以后日期和时间时,应用程序没有运行,将火? [英] Is it possible to set an Android Notification or a later date and time that will fire when the app is not running?
问题描述
从我读过似乎code这样将要求应用程序在一个线程中运行,直到通知火灾。我需要通知消防在稍后的日期和时间,以便用户可以看到通知就像任何其他通知,然后点击它,它会打开一个活动,在一些数据传递这样的应用程序知道该怎么做。
我怎样才能让这个通知消防天后不运行的整个时间的应用程序?
难道我用等待来做到这一点?
长米利斯= 60000;
myNotification.wait(millis)来;
下面是我的code这将立即触发
NotificationCompat.Builder mBuilder =
新NotificationCompat.Builder(getActivity())
.setSmallIcon(R.drawable.star)
.setContentTitle(怎么样+ me.getString(EVENTTITLE)+?)
.setContentText(点击这里留下您的评论);意图resultIntent =新意图(getActivity(),SetupActivity.class);的PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getActivity(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
); mBuilder.setContentIntent(resultPendingIntent); INT mNotificationId = me.getInt(事件ID);
//获取NotificationManager服务的一个实例
NotificationManager mNotifyMgr =
(NotificationManager)getActivity()getSystemService(getActivity()NOTIFICATION_SERVICE);
//构建通知和问题了。
mNotifyMgr.notify(mNotificationId,mBuilder.build());
随着A - C写,用 AlarmManager
来安排的PendingIntent
在您需要的时候被调用。你可能会使用 RTC_WAKEUP
报警,意思是时间是根据 System.currentTimeMillis的()
,如日历
用途和让我们唤醒设备退出睡眠模式。使用广播的PendingIntent
和您的通知
code可以进入的onReceive( )
方法,如果你不需要做任何磁盘或网络I / O去用于通知
本身的信息。
请注意,在Android 4.4系统的 AlarmManager
规则改了一下。您将要使用 setExact()
在Android 4.4+和设置()
早期的Android版本。
From what I have read it seems that code like this would require the app to be running in a thread until the notification fires. I need the notification to fire at a later date and time so the user sees the notification just like any other notification and then clicks it and it opens of an activity, passing in some data so the app knows what to do.
How can I make this notification fire days later without the app running the whole time?
Do I use wait to accomplish this?
long millis = 60000;
myNotification.wait(millis);
Here is my code which fires immediately
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());
As A--C wrote, use AlarmManager
to schedule a PendingIntent
to be invoked at your desired time. You will probably use an RTC_WAKEUP
alarm, meaning "the time is based on System.currentTimeMillis()
, like Calendar
uses" and "let's wake up the device out of sleep mode". Use a broadcast PendingIntent
, and your Notification
code can go into the onReceive()
method, if you do not need to do any disk or network I/O to get the information to use for the Notification
itself.
Note that in Android 4.4 the rules for AlarmManager
changed a bit. You will want to use setExact()
on Android 4.4+ and set()
on earlier Android versions.
这篇关于是否有可能设置一个Android通知或以后日期和时间时,应用程序没有运行,将火?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!