Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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
Android 删除旧警报并使用警报管理器设置新警报_Android_Alarmmanager_Alarm - Fatal编程技术网

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

Android 删除旧警报并使用警报管理器设置新警报,android,alarmmanager,alarm,Android,Alarmmanager,Alarm,我与报警管理器有问题。我需要删除设置为特定时间的旧警报,并将警报重置为新时间。我尝试过很多场景,但没有一个适合我。非常感谢您的帮助。在此处输入代码 //解除警报 am = (AlarmManager) getContext().getSystemService( ALARM_SERVICE); Intent i = new Intent( getContext(),AlarmReceiver.class); PendingIntent p = PendingIntent

我与报警管理器有问题。我需要删除设置为特定时间的旧警报,并将警报重置为新时间。我尝试过很多场景,但没有一个适合我。非常感谢您的帮助。
在此处输入代码

//解除警报

    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);
    }

确保您在
设置
取消
报警时使用了相同的
请求代码

设置报警:

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.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);
取消报警:

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.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);

@snachmsm是的,这是一个唯一的号码,我正在使用是的,我使用了相同的请求代码。我找到了解决方案calendar.set(calendar.HOUR\u OF_DAY,14);日历。设置(日历。分钟,55);上面这行代码造成了问题。。感谢您提供的解决方案。