2011-12-01 79 views
3

我想在特定日期通知鬧鐘。 然後,我現在正在使用帶有NotificationManager的AmarmManager。 當我從dateDialog中設置選定日期時,鬧鐘正在工作。 如何將固定時間的鬧鐘設置爲日曆值? 我想在固定時間每天早上9點重複鬧鐘。 目前,警報忽略了特定日期的時間。 你能幫我嗎?非常感謝。具有特定日期的Android鬧鐘設置

confirmButton.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) { 
      //set alarm with expiration date     
      am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
      setOneTimeAlarm(); 
      Toast.makeText(fridgeDetails.this, "Alarm automatic set", Toast.LENGTH_SHORT).show(); 
      setResult(RESULT_OK); 
      finish(); 
     } 
     public void setOneTimeAlarm() { 
     c.set(Calendar.HOUR_OF_DAY, 10); 
     c.set(Calendar.MINUTE, 0); 
     c.set(expiredYear, expiredMonth, expiredDay); 
     Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this, 
         0, myIntent, PendingIntent.FLAG_ONE_SHOT); 
     am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent); 

     } 
    }); 


private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() 
{ 
    public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     expiredYear = year; 
     expiredMonth = monthOfYear; 
     expiredDay = dayOfMonth; 
     displayDate(); 
    } 
}; 

public class AlarmService extends BroadcastReceiver{ 
NotificationManager nm; 
@Override 
public void onReceive(Context context, Intent intent) { 
    nm = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
      CharSequence from = "Check your fridge"; 
      CharSequence message = "It's time to eat!"; 
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      new Intent(), 0); 
      Notification notif = new Notification(R.drawable.ic_launcher, 
      "Keep Fridge", System.currentTimeMillis()); 
      notif.setLatestEventInfo(context, from, message, contentIntent); 
      nm.notify(1, notif); 
     } 
} 

回答

2

你應該叫public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)see here)重複報警。例如,你想火每天上午9:00鬧鐘,你可以這樣做:

Calendar c=Calendar.getInstance(); 
c.set(Calendar.HOUR_OF_DAY, 9); 
c.set(Calendar.MINUTE, 0); 
setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent); 

此外,initilazing的的PendingIntent當最後一個參數設置爲0。

PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this, 
         0, myIntent, 0); 
+0

謝謝你的回覆!我想知道setRepeating。如果我想在特定日期之後設置重複鬧鐘,是否正確?例如,設置鬧鐘日期爲01/12/2011。如果我在上面的代碼中設置了重複,那麼在01/12/2011之後它是否正在工作警報間隔天,而不是之前?只是我想知道setRepeating。這是真的嗎? – wholee1

+0

第二個參數是應該觸發報警的第一個參數。如果您想在特定日期後重復鬧鐘,可以將日曆設置爲該日期,並將日曆時間設置爲第二個參數。第三個參數是兩個鬧鐘之間的時間間隔,所以,您的情況應該始終是AlarmManager.INTERVAL_DAY。 – Huang

0
cal_alarm.setTime(dat); 
     cal_alarm.set(Calendar.MONTH,3); 
     cal_alarm.set(Calendar.YEAR,2012); 
     cal_alarm.set(Calendar.DAY_OF_MONTH,4); 
     cal_alarm.set(Calendar.HOUR_OF_DAY,19);//set the alarm time 
     cal_alarm.set(Calendar.MINUTE, 15); 
     cal_alarm.set(Calendar.SECOND,0); 


     if(cal_alarm.before(cal_now)){//if its in the past increment 
      cal_alarm.add(Calendar.DATE,1); 
     } 

     Intent intent = new Intent(AlarmManagerTestActivity.this, 
       AlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
      AlarmManagerTestActivity.this, 0, intent, 0); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent); 
} 

這可能會幫助你

9

如果你要玩的報警數月的特定日期假設你想在6月11日2012年起到報警,那麼,你應該指定這樣的,但六月正是6個月,那麼你應該在Calender.Month中指定5。

Calendar cal=Calendar.getInstance(); 
cal.set(Calendar.MONTH,5); 
cal.set(Calendar.YEAR,2012); 
cal.set(Calendar.DAY_OF_MONTH,11); 

cal.set(Calendar.HOUR_OF_DAY,16); 
cal.set(Calendar.MINUTE,10); 
cal.set(Calendar.SECOND,0); 

Intent _myIntent = new Intent(getApplicationContext(), ReceiverClass.class); 
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA); 
AlarmManager _myAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
//_myAlarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), _myPendingIntent); 
_myAlarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent); 
+2

歡迎來到Stack Overflow!這是設置鬧鐘的一個很好的第一個答案,您提供了一個很好的描述和代碼來備份它。但是請再驗證一下你的代碼:你設置了三次Calendar對象,並且有很多不必要的間距。另外,如果月份的索引很容易混淆,可以使用:cal.set(Calendar.MONTH,Calendar.JUNE);' – Sam

2

嘿,這就是如何在Android AlarmManager告警設置成spesific日期 (上日期的android alarmmanager設定的報警) 我一直在尋遍了這一點。 注意一個月的價值!

Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault()); 
//cal.add(Calendar.SECOND, 10); 

cal.set(Calendar.DATE,19); //1-31 
cal.set(Calendar.MONTH,Calendar.DECEMBER); //first month is 0!!! January is zero!!! 
cal.set(Calendar.YEAR,2012);//year... 

cal.set(Calendar.HOUR_OF_DAY, 16); //HOUR 
cal.set(Calendar.MINUTE, 39);  //MIN 
cal.set(Calendar.SECOND, 10);  //SEC 


// Create a new PendingIntent and add it to the AlarmManager 
Intent intent = new Intent(MainActivity.this, alarmAct.class); 
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0); 

//or if you start an Activity 
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0); 

AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent); 
相關問題