【问题标题】:Stop the setrepeat of Android alarmmanager停止Android alarmmanager的setrepeat
【发布时间】:2011-09-17 08:43:57
【问题描述】:

我已经创建了如下所示的警报

Intent intent = new Intent(this, Areceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, timenow, 3000, sender);

我创建了一个按钮来停止警报。在 onclick 方法中,我编写了以下代码

Intent intentstop = new Intent(this, Areceiver.class);
PendingIntent senderstop = PendingIntent.getBroadcast(this,
            0, intentstop, 0);
AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManagerstop.cancel(senderstop);

但工作并没有停止。可能是什么问题?

【问题讨论】:

    标签: android alarmmanager android-pendingintent


    【解决方案1】:

    您在启动警报时为您的待处理意图提供了 request_code = 1234567。但是当您尝试停止它时,您使用的是 0。尝试停止时使用它并且应该可以工作

    Intent intentstop = new Intent(this, Areceiver.class);
    PendingIntent senderstop = PendingIntent.getBroadcast(this,
                1234567, intentstop, 0);
    AlarmManager alarmManagerstop = (AlarmManager) getSystemService(ALARM_SERVICE);
    
    alarmManagerstop.cancel(senderstop);
    

    【讨论】:

    • 这对我也有帮助。虽然,我的互联网启动了一项服务,所以我没有使用 PendingIntent.getBroadcast,而是使用了 PendingIntent.getService。谢谢!
    • 是否有保证在服务上调用的方法?不幸的是 onDestroy 在我的情况下没有被调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多