gpt4 book ai didi

android - 从 AlarmManager 中删除特定的警报

转载 作者:行者123 更新时间:2023-11-29 17:58:04 24 4
gpt4 key购买 nike

我想删除我在 Broadcastreceiver 中设置的警报。这是它应该如何执行:

我收到一条 GCM 消息,其中包含一个 user_state 字段。

if user_state is 0 no notification is triggeredif user_state is 1 a notification is triggeredif user_state is 2 an Alarm is set and the notification is triggered 20 Minutes later.

This all works well.

Now I want to delete an specific Alarm, depending on the Data received from the GCM-intent.Unfortunately AlarmManager.cancel() does not compare Extra fields.I also tried to use intent.setType("data to compare") but if I set this field the Broadcast is not received...

I have now tried to register a new BroadcastReceiver with the Data which should be compared in the Action field. As I understand, this should work fine, BUT it is not possible to register a BroadcastReceiver from within a BroadcastReceiver.

Also it is not possible to get a list of Pendingintents from the AlarmManager.So all i can do now is delete ALL alarms - which is not an option,

The only other idea I have is using my own Service instead of the AlarmManager... I would like to avoid that one. I don't want to use extra Resources when there is an buildin Option.

Here is my set Alarm Method:

private void setTimed(Intent intent, Notify notify) {
Bundle bundle = intent.getExtras();
bundle.remove("userstate");
bundle.putString("userstate", "3");

Intent ringintent = new Intent("de.pluetzner.waveobserver.TRECEIVE");
ringintent.setType(notify.getHost()+"&&&"+notify.getService());
ringintent.putExtras(bundle);
AlarmManager am = (AlarmManager) this.context.getSystemService(this.context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(this.context, 0, ringintent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (this.waittime * 60 * 1000), pi);
Log.d("timer", "time set");
}

ringintent.setType() 应该是警报的标识符,但它不起作用。没有这个,就会收到警报

这里是删除报警的方法。

private void deleteAlarm(Intent intent, Notify notify) {
Intent ringintent = new Intent("de.pluetzner.waveobserver.TRECEIVE");
ringintent.setType(notify.getHost() + "&&&" + notify.getService());
PendingIntent pi = PendingIntent.getBroadcast(this.context, 0, ringintent, 0);
AlarmManager am = (AlarmManager) this.context.getSystemService(this.context.ALARM_SERVICE);
am.cancel(pi);
Log.d("timer", "time removed");
}

这里是一样的 - setType 设置不起作用。没有它,它会删除所有警报。

我也知道这会以某种方式滥用 setType - 但我没有想法。你有吗?

最佳答案

getBroadcast的RequestCode参数方法唯一标识每个 PendingIntent。

您可以为每个 PendingIntent 分配一个唯一的整数代码。

报警1:

      PendingIntent pi = PendingIntent.getBroadcast(this.context, 1, ringintent, 0); 

报警2:

      PendingIntent pi = PendingIntent.getBroadcast(this.context, 2, ringintent, 0); 

现在如果你想单独杀死 Alarm2,重新创建具有相同标识符的 PendingIntent,然后调用 AlarmManager 的取消方法

      PendingIntent sender = PendingIntent.getBroadcast(this, 2, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);

关于android - 从 AlarmManager 中删除特定的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17858799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com