我正在尝试使用我的应用设置多个闹钟,并根据请求取消它们。
我已经阅读了很多关于相同的主题,并浏览了文档,但似乎它不起作用。
文档说,取消将尝试找到与我提供的“ filterEquals ”匹配的意图('确定两个意图是否相同,以达到意图解析(过滤)的目的。是,如果他们的行为,数据,类型,类和类别是相同的。这不会比较意图中包含的任何额外数据。')
我提供相同的操作,数据,类型,类和类别,为什么它不起作用?
创建闹钟:
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 30000;
Intent intent = new Intent(getApplicationContext(), Broadcast.class);
intent.putExtra("alarmTime",alarmTime);
intent.putExtra("reminder1",reminder1);
intent.putExtra("reminder2",reminder2);
intent.putExtra("title",title);
intent.putExtra("message",message);
intent.putExtra("vibrate",vibrate);
intent.putExtra("sound",sound);
intent.putExtra("name",name);
//Create _id from data input. is unique
int _id = 0;
for (char i: name.toCharArray()){
_id += Character.getNumericValue(i);
}
for (char i:title.toCharArray()){
_id += Character.getNumericValue(i);
}
for (char i:message.toCharArray()){
_id += Character.getNumericValue(i);
}
_id += (int) alarmTime.getTime();
_id += (int) reminder1.getTime();
_id += (int) reminder2.getTime();
Log.e("ALARMS", "Creating alarm with id: "+_id);
intent.setData(Uri.parse("custom://" + _id));
intent.setAction(String.valueOf(_id));
PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(getApplicationContext(), _id, intent, 0);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + interval, pendingUpdateIntent);
取消闹钟
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), Broadcast.class);
intent.putExtra("alarmTime",alarmTime);
intent.putExtra("reminder1",reminder1);
intent.putExtra("reminder2",reminder2);
intent.putExtra("title",title);
intent.putExtra("message",message);
intent.putExtra("vibrate",vibrate);
intent.putExtra("sound",sound);
intent.putExtra("name",name);
int _id = 0;
for (char i:name.toCharArray()){
_id += Character.getNumericValue(i);
}
for (char i:title.toCharArray()){
_id += Character.getNumericValue(i);
}
for (char i:message.toCharArray()){
_id += Character.getNumericValue(i);
}
_id += (int) alarmTime.getTime();
_id += (int) reminder1.getTime();
_id += (int) reminder2.getTime();
intent.setData(Uri.parse("custom://" + _id));
intent.setAction(String.valueOf(_id));
PendingIntent pendingUpdateIntent = PendingIntent.getBroadcast(getApplicationContext(), _id, intent, 0);
Log.e("ALARMS", "Cancelling alarm with id: "+_id);
// Cancel alarms
try {
manager.cancel(pendingUpdateIntent);
} catch (Exception e) {
Log.e("ALARMS", "AlarmManager update was not canceled. " + e.toString());
}
所以我用不同的参数调用3次创建闹钟,所以我的_id每次都不同。
然后我拨打3次取消警报,并在创建和取消时为每个警报生成匹配。
所以取消运行但我的警报仍然被解雇。
我的闹钟会触发广播接收器,每个闹钟都会输出一个通知。
我忘了提到我尝试使用 PendingIntent 的不同标志但没有工作(FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT)