设置和取消与AlarmManager报警器(Android版) [英] Setting and canceling alarms with AlarmManager (Android)

查看:278
本文介绍了设置和取消与AlarmManager报警器(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是要求什么,我相信随着Android的报警器被确认发生(或反驳)任职。这个主题在博客和帖子计算器一定的频率发生,但我还没有发现这两个点(下图)。

This is a post requesting confirmation (or rebuttal) of what I believe to be happening with alarms in Android. This subject occurs with some frequency in blogs and StackOverflow posts, but I have not found a clear explanation of these two points (below).

下面的code旨在设定和报警,然后来取消它。这是行不通的。

The code below is intended to set and alarm and then to unset it. It does not work.

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.setAction(AlarmReceiver.ALERT_GUARD_CHECKIN);
    PendingIntent sender = PendingIntent.getBroadcast(context, _id, intent,
            PendingIntent.FLAG_ONE_SHOT);

    AlarmManager am = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, now.getTime().getTime(), sender);

    Intent toCancel = new Intent(context, AlarmReceiver.class);
    toCancel.setAction(AlarmReceiver.ALERT_GUARD_CHECKIN);
    PendingIntent pendingToCancel = PendingIntent.getBroadcast(context,
            _id, toCancel, PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(pendingToCancel);

如果它不工作,而不是通过PendingIntent.FLAG_ONE_SHOT到PendingIntent.getBroadast()的第一个电话,我通过零来代替。我不认为这种行为被记录。

It does work if, instead of passing PendingIntent.FLAG_ONE_SHOT to the first call of PendingIntent.getBroadast(), I pass zero instead. I do not believe this behavior is documented.

的code也如PendingIntent.getBroadast的第二个参数()(以上,_id)是不相同的不起作用。我不相信这是记录在案。在Android文档的第二个参数被记录为私人请求code发件人(目前未使用)。

The code also does not work if second argument of PendingIntent.getBroadast() (above, _id) are not identical. I do not believe this is documented. In the Android docs the second argument is documented as, "Private request code for the sender (currently not used)."

问题是我是否已经正确地形容这种行为,或者,如果这些概括是ephiphenomena的。

The question is whether I've properly described this behavior, or if these generalizations are of ephiphenomena.

推荐答案

您必须通过的PendingIntent 取消()这是等同于您用于注册报警的人。这意味着使用同样的意图,旗帜,并要求code。相反的是该文档可能导致您认为,它实际上检查请求code价值。

You have to pass a PendingIntent to cancel() that is equivalent to the one you used to register the alarm. This means using the same Intent, flags, and request code. Contrary to what the documentation might lead you to believe, it actually checks the request code value as well.

这篇关于设置和取消与AlarmManager报警器(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆