[英]Android: Notification cancels it's repeating alarm
I want my notification to cancel it's own repeating alarm. 我希望我的通知取消其自身的重复警报。 However, I don't know how to pass pendingIntent (to cancel it later) before it is even created. 但是,我什至不知道在创建之前如何传递未决的Intent(稍后将其取消)。
Alarms sets here: 警报在这里设置:
public class Schedule extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wakeLock.acquire();
//next is notification code. //
...
//create intent.
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFY_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
...
////
wakeLock.release();
}
public void setAlarm(Context context) {
...
Intent intent = new Intent(context, Schedule.class);
intent.putExtra("DELAY", delay);
intent.putExtra("NOTIFICATION_ID", id);
intent.putExtra("TITLE_TEXT", titleText);
intent.putExtra("BIG_TEXT", bigText);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 60 * delay, 1000 * 60 * delay, pendingIntent);
}
}
The key problem is that I want to cancel the repeating alarm on notification click (intent in onRecieve method), but canceling alarms requires pendingIntent, which notification is a part of. 关键问题是,我想取消通知单击时的重复警报(onRecieve方法中的intent),但是取消警报需要未决的intent,这是通知的一部分。 Any way to cheat the system? 有什么办法欺骗系统吗?
Use AlarmManager.cancel()
to cancel
the specific alarm
and use same PendingIntent
that was used to create the alarm. 使用AlarmManager.cancel()
cancel
特定的alarm
并使用与创建警报相同的PendingIntent
。 Of course, you should use same requestCode
( NOTIFY_ID
) that was used before. 当然,您应该使用与之前相同的requestCode
( NOTIFY_ID
)。
Try this: 尝试这个:
Intent intent = new Intent(this, Schedule.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Add this code portion into your MainActivty's onCreate()
method as your notification
will intent to MainActivity
when click
on it from notification panel. 将此代码部分添加到MainActivty的onCreate()
方法中,因为当从通知面板中click
它时, notification
将发给MainActivity
。
Hope this will help~ 希望这会有所帮助〜
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.