未触发计划通知
我有一个应用程序,我希望它每 x 天启动一次通知(由用户预定义)。为此,我使用这段代码: 类扩展了广播接收器:
public class OnAlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Votre vidange approche";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "Notification";
CharSequence contentText = "Vérifier votre kilométrage";
Intent notificationIntent = new Intent(context, Acceuil.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
}
另一个类中的此函数用于启动上一个类:
public void repeating() {
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1800000, pi);
}
我将其设置为每半小时启动一次通知,但我从未收到过它。 这段代码是怎么回事?
非常感谢。
I have an application that i want it to launch a notification every x days(predefined by user). For that I'm using this code:
Class extends broadcast receiver:
public class OnAlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Votre vidange approche";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "Notification";
CharSequence contentText = "Vérifier votre kilométrage";
Intent notificationIntent = new Intent(context, Acceuil.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
}
And this function in the other class to launch this previous class:
public void repeating() {
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1800000, pi);
}
I set it to launch every half hour the notification but i never had it.
What's going on with this code?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SystemClock.elapsedRealtime() 表示“现在”,精确到毫秒,因此当它有机会运行时,triggerAtTime 可能已经过去(不确定 AlarmManager 如何处理这种情况) )。尝试延迟几秒钟,看看它是否正常运行:
SystemClock.elapsedRealtime() + 10 * 1000
。另一方面,大间隔(例如几天)重复警报是不可靠的:如果您的进程被终止,或引发异常,或者设备重新启动,那么它的警报将被清除。一种替代方案(尽管不是 100% 万无一失)是安排前一个警报的下一个警报,并将其运行的时间节省到共享首选项等。然后,每次应用程序启动时,检查下一次运行时间是否有效(即将来),如果不有效则安排新的警报。
SystemClock.elapsedRealtime()
means 'now' up to the millisecond, so there is a possibility that by the time it gets a chance to run, the triggerAtTime will have passed (not sure how AlarmManager handles this case). Try delaying it a few seconds to see if it runs properly:SystemClock.elapsedRealtime() + 10 * 1000
.On another note, repeating alarms with large intervals (such as days) are unreliable: if you process is killed, or throws an exception, or the device is rebooted, then it's alarms will be cleared. One alternative (although not 100% bulletproof) would be to schedule each next alarm from the previous one and save the time it will run to shared preferences for example. Then each time your application starts, check if the next run time is valid (i.e., in the future), and if not schedule a new alarm.