PendingIntent cancel() API文档说:
取消当前活动的PendingIntent。只有拥有PendingIntent的原始应用程序才能取消它。
我不知道这是什么意思。如果我将活动AlarmManager事件设置为以下x:
PendingIntent pendingIntent;
Intent myIntent = new Intent(x.this, AlarmReciever.class);
myIntent.putExtra("task_uuid", task_uuid);
pendingIntent = PendingIntent.getBroadcast(x.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, dateTime.getTimeInMillis(), pendingIntent);我的问题是:我是否可以使用以下方法取消活动y的待定意图:
PendingIntent pendingIntent;
Intent myIntent = new Intent(y.this, AlarmReciever.class);
myIntent.putExtra("task_uuid", task_uuid);
pendingIntent = PendingIntent.getBroadcast(y.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);发布于 2014-01-10 12:36:36
当您设置告警时,您需要在PendingIntent中传递一个键值,它将区分没有警报,应该是这样
pendingIntent = PendingIntent.getBroadcast(x.this, key_value, myIntent,0);
SharedPreferences settings = context.getSharedPreferences("alarm", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, false);
editor.commit(); 要取消相同的警报,您需要将该key_values保存在某个位置,您可以使用共享首选项。得到相同的键,然后像这样取消警报
SharedPreferences settings = context.getSharedPreferences("alarm", 0);
Map<String,?> allNotifyIdsMap = settings.getAll();
if(allNotifyIdsMap!=null&&allNotifyIdsMap.isEmpty()==false)
{
for(String notifyId: allNotifyIdsMap.keySet())
{
boolean isCleared = settings.getBoolean(notifyId, false);
if(isCleared==false)
{
pendingIntent = PendingIntent.getBroadcast(y.this, key_value, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}
}
}发布于 2018-05-05 11:20:49
概述
AlarmManager基本上通过在预定时间调用PendingIntent来调度基于时间的操作。因此,为了取消预定的警报,您需要访问该PendingIntent。
始终注意这两个参数,同时创建挂起的意图-请求代码和国旗
PendingIntent.getBroadcast(context,REQUEST_CODE,intent, PendingIntent.FLAG_UPDATE_CURRENT);这个简短的概述不足以理解AlarmManager。阅读有关AlarmManager和PendingIntent 这里工作的更多信息
溶液
您可以在应用程序中的任何地方取消预定的警报,即使在调度警报时没有访问同一个PendingIntent对象的权限。您可以使用相同的请求代码和FLAG_NO_CREATE创建一个新的挂起的意图,它将返回相同的PendingIntent对象。
/*
With FLAG_NO_CREATE it will return null if the PendingIntent doesnt already
exist. If it already exists it returns
reference to the existing PendingIntent
*/
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,REQUEST_CODE,intent,PendingIntent.FLAG_NO_CREATE);
if (pendingIntent!=null)
alarmManager.cancel(pendingIntent);https://stackoverflow.com/questions/21044441
复制相似问题