0

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

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

4

2 回答 2

1

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

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

这将解决您的问题。

于 2013-05-24T06:23:20.317 回答
-1

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

return PendingIntent.getBroadcast(context, REQUEST_CODE, new Intent(context, 
    LocationReciever.class), PendingIntent.FLAG_UPDATE_CURRENT);
于 2013-05-24T01:44:27.403 回答