0
private void scheduleAlarms(Context context) {
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intentOnAlaramReceiver = new Intent(context, OnAlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, 0);

        String listOfIntervalConnection = Utils.getStringFromProperties(context, Properties.SP_LIST_OF_ENABLE_INTERVAL_CONNECTIONS, Properties.ENABLE_AFTER);
        long enableAfter = DateUtils.MINUTE_IN_MILLIS * Long.parseLong(listOfIntervalConnection);

        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + enableAfter, enableAfter, pendingIntent);
    }

我尝试取消警报

 AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent intentOnAlaramReceiver = new Intent(context, OnAlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, 0);

            alarmManager.cancel(pendingIntent);

但有时会被取消,有时仍然有效。为什么以及如何?我能做些什么?

4

2 回答 2

1

在启动服务和取消服务时,您在 PendingIntent 中使用了不同的 Intent 实例。

将服务意图设置为实例变量,并在服务的启动和取消中使用它。

于 2013-04-04T08:03:41.753 回答
0

尝试使用PendingIntent.FLAG_UPDATE_CURRENT而不是0;像这样:

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentOnAlaramReceiver, PendingIntent.FLAG_UPDATE_CURRENT);
于 2013-12-22T14:07:28.830 回答