2017-06-01 126 views
-1

我遇到了闹钟管理员的问题。我需要将旧闹钟设置删除到特定时间,并将闹钟重置为新时间。我尝试了很多场景,但都没有为我工作。任何帮助应该非常感激。 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); 
    } 
+0

@snachmsm是的,它是一个唯一的号码,我使用 –

回答

0

确保您使用相同的REQUEST_CODEPendingIntent既为settingcanceling报警。

SET ALARM:

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) 

CANCEL ALARM:

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); 
+0

是的,我用于两个相同的请求的代码。我发现了解决方案 calendar.set(Calendar.HOUR_OF_DAY,14); calendar.set(Calendar.MINUTE,55); 上面的代码行是使问题..感谢您提供的解决方案。 –