删除旧警报并使用警报管理器设置新警报

时间:2017-06-01 15:22:24

标签: android alarmmanager alarm

我遇到了警报管理员的问题。我需要将旧的闹钟设置移除到特定时间并将闹钟重置为新时间。我尝试了很多场景,没有人为我工作。任何帮助都应该受到高度赞赏。enter code here

//删除闹钟

    am = (AlarmManager)  getContext().getSystemService( ALARM_SERVICE);
    Intent i = new Intent( getContext(),AlarmReceiver.class);
    PendingIntent p = PendingIntent.getBroadcast( getContext(), profileFragmentRequestCode, i, 0);
    am.cancel(p);
    p.cancel();

//设置新闹钟

    am = (AlarmManager) getContext().getSystemService(ALARM_SERVICE);

       /* Retrieve a PendingIntent that will perform a broadcast */
    Intent alarmIntent = new Intent(getContext(), AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), profileFragmentRequestCode, alarmIntent,  0);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 14);
    calendar.set(Calendar.MINUTE, 55);
    if (android.os.Build.VERSION.SDK_INT >= 19) {
        am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

1 个答案:

答案 0 :(得分:0)

确保您使用REQUEST_CODE PendingIntent setting同时cancelingAlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); Intent alarmIntent = new Intent(getContext(), AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent) 警报。

设置警报:

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

Intent alarmIntent = new Intent(getContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getContext(), REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.cancel(pendingIntent);

CANCEL ALARM:

n