我想告诉用户他每隔5分钟就要做一些事情。我每隔5分钟收到一次通知。但我的问题是我不知道在哪里可以阻止它。 (我想在用户点击通知时停止它。)
以下是我的活动中的代码:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, heure);
cal.add(Calendar.MINUTE, minute);
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), minute*60*1000+heure*3600*1000, sender);
这是我的BroadcastReceiver的代码:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("BroadcastReceiver", "debut receive");
Intent resultIntent = new Intent(context,
PiecesPasEnvoyesActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder mBuilder = new Notification.Builder(context)
.setSmallIcon(R.drawable.courier_blanc)
.setContentTitle("Messages à envoyer")
.setContentText("Vous avez des messages à envoyer");
mBuilder.setAutoCancel(true);
mBuilder.setPriority(Notification.PRIORITY_MAX);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
NotificationManager mNotifyMgr = (NotificationManager) context
.getSystemService(Application.NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
答案 0 :(得分:1)
在您的通知中添加待处理的意图并让此意图停止接收者。
Intent notificationIntent = new Intent(this, ActivityToStopReciever.class);
notificationIntent.setFlags(Notification.FLAG_AUTO_CANCEL);
PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0);
Notification.Builder mBuilder = new Notification.Builder(context)
.setSmallIcon(R.drawable.courier_blanc)
.setContentTitle("Messages à envoyer")
.setContentIntent(intent)
.setContentText("Vous avez des messages à envoyer");
在ActivityToStopReciever.class中,请查看此链接以取消通知
http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int
并在此处查看删除警报
Android AlarmManager problem with setting & resetting an alarm