警报服务未取消

Posted

技术标签:

【中文标题】警报服务未取消【英文标题】:Alarm service not canceling 【发布时间】:2013-05-24 01:39:16 【问题描述】:

我已经注册了一个重复的警报并且它有效。但是当我尝试取消它时,它不会取消。我在这些帖子中尝试过解决方案:post1post2 Activity的oncreate():

private void init() 
        btToggleLocationService = (Button) findViewById(R.id.btToggleLocationService);
        btToggleLocationService.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if(LocatorAlarmManager.isRegistered(MainActivity.this))
                    Log.d(TAG, "onClick() Alarm  registered, stopping...");
                    LocatorAlarmManager.stopAlarm(MainActivity.this);
                    updateServiceButton();
                else
                    Log.d(TAG, "onClick() Alarm not registered, registreing...");
                    LocatorAlarmManager.registerAlarm(MainActivity.this);
                    updateServiceButton();
                

            
        );
        updateServiceButton();
    

还有我的 LocationAlarmManager 类:

public class LocatorAlarmManager 
    private static final String TAG = LocatorAlarmManager.class.getSimpleName();
    public static final int REQUEST_CODE = 99321;

    /**
     * Registers the Location alarm. Will ignore call if its already registerd.
     */
    public static void registerAlarm(Context context) 
        if (isRegistered(context)) 
            Log.i(TAG, "already Registred alarmt");
            return;
        
        long firstTime = SystemClock.elapsedRealtime();
        firstTime += 3 * 1000;

        SharedPreferences sPrefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        AlarmManager am = (AlarmManager) context
                .getSystemService(Activity.ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,
                LocaterSettings.getSendLocationInterval(sPrefs) * 1000L,
                getSendLocationIntent(context, 0));
        Log.i(TAG, "registerAlarm() Registred alarmt");

    

    /**
     * Checks if the Location alarm has been registered or not.
     * 
     * @return true if its registed, false if not.
     */
    public static boolean isRegistered(Context context) 
        boolean alarmUp = getSendLocationIntent(context,
                PendingIntent.FLAG_NO_CREATE) != null;

        if (alarmUp) 
            Log.d(TAG, "isRegistered() Alarm is already active");
            return true;
         else 
            Log.d(TAG, "isRegistered() Alarm is NOT active");
            return false;
        
    

    public static void stopAlarm(Context context) 
        Log.d(TAG, "stopAlarm() Stopping alarm");
        AlarmManager alarmManagerstop = (AlarmManager) context
                .getSystemService(Activity.ALARM_SERVICE);
        try 
            alarmManagerstop.cancel(getSendLocationIntent(context, 0));
         catch (Exception e) 
            e.printStackTrace();
        
        Log.d(TAG, "stopAlarm() Stopped alarm");
    

    private static PendingIntent getSendLocationIntent(Context context, int flag) 
        return PendingIntent.getBroadcast(context, REQUEST_CODE, new Intent(
                context, LocationReciever.class), flag);

    

日志:

Button Click start alarm
05-23 21:35:29.780: D/LocatorAlarmManager(16329): isRegistered() Alarm is NOT active
05-23 21:35:29.780: D/MainActivity(16329): onClick() Alarm not registered, registreing...
05-23 21:35:29.780: D/LocatorAlarmManager(16329): isRegistered() Alarm is NOT active
05-23 21:35:29.790: I/LocatorAlarmManager(16329): registerAlarm() Registred alarmt
05-23 21:35:29.790: D/LocatorAlarmManager(16329): isRegistered() Alarm is already active

Button Click stop alarm
05-23 21:35:59.203: D/LocatorAlarmManager(16329): isRegistered() Alarm is already active
05-23 21:35:59.203: D/MainActivity(16329): onClick() Alarm  registered, stopping...
05-23 21:35:59.203: D/LocatorAlarmManager(16329): stopAlarm() Stopping alarm
05-23 21:35:59.203: D/LocatorAlarmManager(16329): stopAlarm() Stopped alarm
05-23 21:35:59.203: D/LocatorAlarmManager(16329): isRegistered() Alarm is already active

问题是日志中的最后一行,它应该被取消,但是在它被取消之后,它再次检查并且它说它'已经注册',我想知道我的停止警报动作有什么问题。提前致谢。

【问题讨论】:

【参考方案1】:

取消警报后,您还必须取消 PendingIntent。

try 
    alarmManagerstop.cancel(getSendLocationIntent(context, 0));
    getSendLocationIntent(context, 0).cancel();
 catch (Exception e) 
    e.printStackTrace();

这将解决您的问题。

【讨论】:

【参考方案2】:

创建PendingIntents 时,使用PendingIntent.FLAG_UPDATE_CURRENT 作为最后一个参数。

return PendingIntent.getBroadcast(context, REQUEST_CODE, new Intent(context, 
    LocationReciever.class), PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

这没有帮助。相同的日志,相同的结果。

以上是关于警报服务未取消的主要内容,如果未能解决你的问题,请参考以下文章

在日期和时间更改时重置/取消警报时的 AlarmManager 错误

在特定时间取消重复警报

iOS 警报“UI 已被系统取消”

如何取消警报管理器的每日通知?

取消之前设置的警报

Safari 警报框取消按钮的回调