警报服务未取消

时间:2013-05-24 01:39:16

标签: java android android-alarms

我已经注册了重复警报,但它确实有效。但是当我试图取消它时,它不会取消。我在这些帖子中尝试过解决方案:post1 post2 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

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

2 个答案:

答案 0 :(得分:1)

取消闹钟后,您还必须取消PendingIntent。

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

这将解决您的问题。

答案 1 :(得分:-1)

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

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