Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 报警服务未取消_Java_Android_Android Alarms - Fatal编程技术网

Java 报警服务未取消

Java 报警服务未取消,java,android,android-alarms,Java,Android,Android Alarms,我注册了一个重复报警,它可以正常工作。但当我试图取消它时,它不会取消。我在这些帖子中尝试过解决方案: 活动的oncreate(): 和我的LocationAlarmManager类: public class LocatorAlarmManager { private static final String TAG = LocatorAlarmManager.class.getSimpleName(); public static final int REQUEST_CODE =

我注册了一个重复报警,它可以正常工作。但当我试图取消它时,它不会取消。我在这些帖子中尝试过解决方案: 活动的oncreate():

和我的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);

    }
日志:


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

当您创建
pendingent
时,使用
pendingent.FLAG\u UPDATE\u CURRENT
作为最后一个参数

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

取消警报后,您还必须取消挂起的帐篷

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

这将解决您的问题。

这没有帮助。同样的日志,同样的结果。
return PendingIntent.getBroadcast(context, REQUEST_CODE, new Intent(context, 
    LocationReciever.class), PendingIntent.FLAG_UPDATE_CURRENT);
try {
    alarmManagerstop.cancel(getSendLocationIntent(context, 0));
    getSendLocationIntent(context, 0).cancel();
} catch (Exception e) {
    e.printStackTrace();
}