AlarmReceiver 中的 Android 通知
我希望能够创建一个警报,每 2 分钟向用户发出一次启动应用程序的通知。一切正常,但当我手动启动应用程序时会出现通知。这是我的代码:
接收器:
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "myactivity";
CharSequence message = "click to start activity";
Intent scheduledIntent = new Intent(context, AmslerTestActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
scheduledIntent, 0);
Notification notif = new Notification(R.drawable.icon,
message, System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
nm.notify(1, notif);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
调度程序:
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 01);
cal.set(Calendar.SECOND, 0);
Intent intent = new Intent(myactivity.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// Get the AlarmManager service
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2 * 60 * 1000, sender);
我希望通知仅在代码未运行时响起。
I want to be able to create an alarm to ring a notification to the user to start the application every 2 minutes. Everything works fine, but the notification appears when i start my application manually. Here are my code :
Receiver:
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "myactivity";
CharSequence message = "click to start activity";
Intent scheduledIntent = new Intent(context, AmslerTestActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
scheduledIntent, 0);
Notification notif = new Notification(R.drawable.icon,
message, System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
nm.notify(1, notif);
scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scheduledIntent);
}
Scheduler:
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 01);
cal.set(Calendar.SECOND, 0);
Intent intent = new Intent(myactivity.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// Get the AlarmManager service
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2 * 60 * 1000, sender);
I want the notification to only ring when the code is not running.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将代码的“调度程序”部分放入
onPause
、onStop
或onDestroy
中,以便仅在Activity< 一次时设置闹钟/code> 停止。请阅读此处来决定哪个最适合您的目的。然后,在执行
am.setRepeating
时,对triggerAtTime
使用System.currentTimeMillis() + 2 * 60 * 1000
,使闹钟触发2从Activity
停止的时间算起的分钟数。然后,它将根据第三个参数“间隔”每 2 分钟重复一次,您当前已将其正确设置为 2 分钟。Put the "scheduler" part of your code in
onPause
,onStop
oronDestroy
in order to set the alarm only once yourActivity
stops. Read here to decide which is best for your purposes. Then, when doingam.setRepeating
, useSystem.currentTimeMillis() + 2 * 60 * 1000
for thetriggerAtTime
to make the alarm trigger 2 min from the time theActivity
stops. Then it will repeat every 2 min based on the 3rd parameter, theinterval
, which you currently have set correctly to 2 min.