Android:使用AlarmManager设置所有PendingIntents

时间:2010-11-30 15:50:20

标签: android alarmmanager

我正在设置这样的警报:

alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingEvent);

我有兴趣删除以前设置的所有警报,清除它们。

有没有办法让我这样做或获取当前设置的所有警报,以便我可以手动删除它们?

5 个答案:

答案 0 :(得分:80)

您无需继续参考它。只需定义一个新的PendingIntent,就像你在创建它时定义的那样。

例如:

如果我创建了一个由AlarmManager触发的PendingIntent,如下所示:

   Intent alarmIntent = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class);
    alarmIntent.setData(Uri.parse("custom://" + alarm.ID));
    alarmIntent.setAction(String.valueOf(alarm.ID));
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    PendingIntent displayIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);

    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDateTime, displayIntent);

然后在其他代码(甚至是其他活动)的某处,你可以取消:

Intent alarmIntent = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class);
alarmIntent.setData(Uri.parse("custom://" + alarm.ID));
alarmIntent.setAction(String.valueOf(alarm.ID));
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

PendingIntent displayIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);

alarmManager.cancel(displayIntent);

这里重要的是将PendingIntent设置为完全相同的数据和操作,以及此处所述的其他标准http://developer.android.com/reference/android/app/AlarmManager.html#cancel%28android.app.PendingIntent%29

答案 1 :(得分:64)

您需要创建待处理的意图,然后取消它

 AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent updateServiceIntent = new Intent(context, MyPendingIntentService.class);
    PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0);

    // Cancel alarms
    try {
        alarmManager.cancel(pendingUpdateIntent);
    } catch (Exception e) {
        Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
    }

答案 2 :(得分:15)

要取消闹钟,您需要重新创建相同的PendingIntent并传递相同的请求代码。

所以,我创建了一个AlarmUtils来帮助我保存首选项中的所有请求代码,以便将来取消它时如果需要的话。您只需使用以下类并调用以下方法:

  • addAlarm添加新警报并传递请求代码。
  • cancelAlarm要删除警报,您需要重新创建相同的信息 意图并传递相同的请求代码。
  • hasAlarm验证是否添加了此警报,您需要重新创建 相同的Intent并传递相同的请求代码。
  • cancelAllAlarms删除所有已设置的闹钟。

My AlarmUtils

public class AlarmUtils {

    private static final String sTagAlarms = ":alarms";

    public static void addAlarm(Context context, Intent intent, int notificationId, Calendar calendar) {

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }

        saveAlarmId(context, notificationId);
    }

    public static void cancelAlarm(Context context, Intent intent, int notificationId) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.cancel(pendingIntent);
        pendingIntent.cancel();

        removeAlarmId(context, notificationId);
    }

    public static void cancelAllAlarms(Context context, Intent intent) {
        for (int idAlarm : getAlarmIds(context)) {
            cancelAlarm(context, intent, idAlarm);
        }
    }

    public static boolean hasAlarm(Context context, Intent intent, int notificationId) {
        return PendingIntent.getBroadcast(context, notificationId, intent, PendingIntent.FLAG_NO_CREATE) != null;
    }

    private static void saveAlarmId(Context context, int id) {
        List<Integer> idsAlarms = getAlarmIds(context);

        if (idsAlarms.contains(id)) {
            return;
        }

        idsAlarms.add(id);

        saveIdsInPreferences(context, idsAlarms);
    }

    private static void removeAlarmId(Context context, int id) {
        List<Integer> idsAlarms = getAlarmIds(context);

        for (int i = 0; i < idsAlarms.size(); i++) {
            if (idsAlarms.get(i) == id)
                idsAlarms.remove(i);
        }

        saveIdsInPreferences(context, idsAlarms);
    }

    private static List<Integer> getAlarmIds(Context context) {
        List<Integer> ids = new ArrayList<>();
        try {
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            JSONArray jsonArray2 = new JSONArray(prefs.getString(context.getPackageName() + sTagAlarms, "[]"));

            for (int i = 0; i < jsonArray2.length(); i++) {
                ids.add(jsonArray2.getInt(i));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return ids;
    }

    private static void saveIdsInPreferences(Context context, List<Integer> lstIds) {
        JSONArray jsonArray = new JSONArray();
        for (Integer idAlarm : lstIds) {
            jsonArray.put(idAlarm);
        }

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(context.getPackageName() + sTagAlarms, jsonArray.toString());

        editor.apply();
    }
}

答案 3 :(得分:0)

如果将PendingIntent ID保存在SharedPreference或db中,当 Android重新启动您的程序包强制停止

时,您将获得错误的值

您可以将以前的所有PendingIntent保存到另一个新的PendingIntent中,然后再取消所有

例如

    void createSomeAlarmAndSave() {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, MyReceiver.class).setAction(ACTION_XXX), 0);
        getSystemService(AlarmManager.class).setInexactRepeating(AlarmManager.RTC_WAKEUP, 1000 * 60 * 15, 1000 * 60 * 15, pendingIntent);

        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 1, new Intent(this, MyReceiver.class).setAction(ACTION_XXX), 0);
        getSystemService(AlarmManager.class).setInexactRepeating(AlarmManager.RTC_WAKEUP, 1000 * 60 * 20, 1000 * 60 * 20, pendingIntent1);

        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 2, new Intent(this, MyReceiver.class).setAction(ACTION_XXX), 0);
        getSystemService(AlarmManager.class).setInexactRepeating(AlarmManager.RTC_WAKEUP, 1000 * 60 * 25, 1000 * 60 * 25, pendingIntent2);

        //save all previous PendingIntent to another new PendingIntent
        PendingIntent.getBroadcast(this, 0, new Intent(this, MyReceiver.class).putExtra("previous", new PendingIntent[]{pendingIntent, pendingIntent1, pendingIntent2}), PendingIntent.FLAG_UPDATE_CURRENT);
    }

取消所有先前的PendingIntent

    void cancelAllPreviousAlarm() {
        //acquire the dedicated PendingIntent
        PendingIntent pendingIntentAllPrevious = PendingIntent.getBroadcast(this, 0, new Intent(this, MyReceiver.class), PendingIntent.FLAG_NO_CREATE);
        if (pendingIntentAllPrevious != null) {
            try {
                pendingIntentAllPrevious.send(this, 0, null, new PendingIntent.OnFinished() {
                    @Override
                    public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, String resultData, Bundle resultExtras) {
                        for (Parcelable parcelable : intent.getParcelableArrayExtra("previous")) {
                            //alarm will cancel when the corresponding PendingIntent cancel
                            ((PendingIntent) parcelable).cancel();
                        }
                    }
                }, null);
                pendingIntentAllPrevious.cancel();
            } catch (PendingIntent.CanceledException e) {
                e.printStackTrace();
            }
        }
    }

答案 4 :(得分:0)

如果您重新创建 PendingIntent:

PendingIntent pendingIntent = PendingIntent.getBroadcast(
            context, 0, newIntent, PendingIntent.FLAG_CANCEL_CURRENT);