使用警报管理器的服务

发布于 2024-11-07 04:41:27 字数 915 浏览 4 评论 0原文

这是我的代码,但它不起作用。

在设置活动中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱&纷飞 2024-11-14 04:41:27

如果您尝试使用 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 of SystemClock.elapsedRealtime()

Differences are explained here :
http://developer.android.com/reference/android/os/SystemClock.html
AlarmManager.RTC_WAKEUP must be used with currentTimeMillis();

Otherwise you can use the flag :
AlarmManager.ELAPSED_REALTIME_WAKEUP to provide a time calculated with SystemClock.elapsedRealtime()

Flags are explained here : http://developer.android.com/reference/android/app/AlarmManager.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文