2017-06-01 79 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); 上面的代碼行是使問題..感謝您提供的解決方案。 –