使用警报管理器的服务
这是我的代码,但它不起作用。
在设置活动中:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, OnBootReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+1000,
pendingIntent);
在 BroadcastReceiver 中:
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "xyz";
CharSequence message = "Crazy About Android...";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, Settings.class), 0);
Notification notif = new Notification(R.drawable.icon, "Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
mgr.notify(1, notif);
This is my code, but it does not work.
In settings activtiy:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, OnBootReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+1000,
pendingIntent);
In BroadcastReceiver:
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "xyz";
CharSequence message = "Crazy About Android...";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, Settings.class), 0);
Notification notif = new Notification(R.drawable.icon, "Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
mgr.notify(1, notif);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试使用
AlarmManager.RTC_WAKEUP
单元在 1 秒内启动您的意图,您可能想使用 System.currentTimeMillis() 而不是 SystemClock.elapsedRealtime()
差异解释如下:
http://developer.android.com/reference/android/os/SystemClock.html
AlarmManager.RTC_WAKEUP
必须与currentTimeMillis()
一起使用;否则你可以使用该标志:
AlarmManager.ELAPSED_REALTIME_WAKEUP
提供使用SystemClock.elapsedRealtime()
计算的时间标志解释如下:http://developer.android.com/reference/android/app/AlarmManager.html。
If your are trying to start your intent in 1 second using the
AlarmManager.RTC_WAKEUP
unit,you might want to use
System.currentTimeMillis()
instead ofSystemClock.elapsedRealtime()
Differences are explained here :
http://developer.android.com/reference/android/os/SystemClock.html
AlarmManager.RTC_WAKEUP
must be used withcurrentTimeMillis()
;Otherwise you can use the flag :
AlarmManager.ELAPSED_REALTIME_WAKEUP
to provide a time calculated withSystemClock.elapsedRealtime()
Flags are explained here : http://developer.android.com/reference/android/app/AlarmManager.html.