2014-09-11 107 views
3

目前我正在開發一個應用程序來設置每月的提醒。我無法爲警報管理員提供正確的重複間隔。請提供相同的信息。 這是我的代碼,但這不會引發2月份或30天的警報。 也請提供代碼來設置每年重複alaram。如何設置每月重複的鬧鐘

repeatTime=(AlarmManager.INTERVAL_DAY*31); 
mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, when.getTimeInMillis(), repeatTime, pi); 

感謝, 沙拉斯

+0

檢查該鏈接,每月重複報警[這裏] [1] [1]:http://stackoverflow.com/questions/26645749/android-repeating-alarm-should-repeat-on-monthly-basis-and-on-same-day-for-each/ 26646069#26646069 – mushahid 2014-10-30 06:08:32

回答

5

你這是怎麼在extacly1個月今天之間計算時間間隔後,用這個邏輯,一旦觸發復位報警每次。即將警報設置爲您想要啓動的時間點,提供一些待處理的意圖,一旦警報觸發器使用下面的代碼獲取下一個觸發時間,並再次設置警報以在此時觸發。

private long getDuration(){ 
    // get todays date 
    Calendar cal = Calendar.getInstance(); 
    // get current month 
    int currentMonth = cal.get(Calendar.MONTH); 

    // move month ahead 
    currentMonth++; 
    // check if has not exceeded threshold of december 

    if(currentMonth > Calendar.DECEMBER){ 
     // alright, reset month to jan and forward year by 1 e.g fro 2013 to 2014 
     currentMonth = Calendar.JANUARY; 
     // Move year ahead as well 
     cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)+1); 
    } 

    // reset calendar to next month 
    cal.set(Calendar.MONTH, currentMonth); 
    // get the maximum possible days in this month 
    int maximumDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 

    // set the calendar to maximum day (e.g in case of fEB 28th, or leap 29th) 
    cal.set(Calendar.DAY_OF_MONTH, maximumDay); 
    long thenTime = cal.getTimeInMillis(); // this is time one month ahead 



    return (thenTime); // this is what you set as trigger point time i.e one month after 

} 
+0

嗨,感謝您的回覆,我通過將日期更改爲下個月進行了測試,並且它正在工作,無論何時我們重新啓動模擬器,它會爲所有錯過的alaram提升alaram,我都會遇到更多問題。你能告訴我如何實施如何提高只有沒有提出的alaarams?謝謝你,Sharath – 2014-09-16 07:18:34

+0

你可以提供一些意圖的數據,比如觸發器的時間戳,一旦你的警報提出,你就會收到同樣的意圖,這樣你就可以編寫你的邏輯檢查天氣了,它以前的alrams錯過了比較時間。 – Techfist 2014-09-16 08:44:34

+0

目前我正在用db存儲提醒。但不知道如何避免提出重複的alarams,可以使用任何代碼片段來執行此操作。 謝謝, Sharath – 2014-09-18 07:08:04

0

爲每年重複報警

GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); 
    if(cal.isLeapYear(year)){ 
     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 366, alarmIntent); 
    }else{ 
      alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 365, alarmIntent); 
    } 
0

感謝Techfist ..例如:DATEVALUE = 「30/01/2017 11:02」 ......從數據庫中獲取ID和日期等

private void getMonthlyDuration(Context context,int id,String dateValue) { 
    SharedPreferences sharedPreferences = context.getSharedPreferences("ModernDiary", Context.MODE_PRIVATE); 
    String dateOfMonth = sharedPreferences.getString("day"+id,DateFormat.format("dd", Calendar.getInstance()).toString()); 
    Calendar calendar = Calendar.getInstance(); 
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.getDefault()); 
    try { 
     Date dateMain = simpleDateFormat.parse(dateValue); 
     calendar.setTime(dateMain); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    Boolean isSetDate = false; 
    if (sharedPreferences.getInt("monthInc"+id,-1) != -1) { 
     calendar.set(Calendar.MONTH,sharedPreferences.getInt("monthInc"+id,calendar.get(Calendar.MONTH))); 
     calendar.set(Calendar.YEAR,sharedPreferences.getInt("yearInc"+id,calendar.get(Calendar.YEAR))); 
     if (calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < Integer.parseInt(dateOfMonth)) { 
      calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); 
      Log.i("timeDay",dateOfMonth+" "+calendar.getTime()+" max"); 
     } else { 
      calendar.set(Calendar.DATE,Integer.parseInt(dateOfMonth)); 
      Log.i("timeDay",dateOfMonth+" "+calendar.getTime()+"min"); 
     } 
     if (sharedPreferences.getInt("monthInc"+id,calendar.get(Calendar.MONTH)) < calendar.get(Calendar.MONTH)){ 
      calendar.add(Calendar.MONTH, -1); 
      isSetDate = true; 
      Log.i("timeMonth","Increment "+calendar.getTime()); 
     } else { 
      isSetDate = false; 
      Log.i("timeMonth","No Change"); 
     } 
    } 
    calendar.add(Calendar.MONTH, 1); 
    if (isSetDate){ 
     if (calendar.getActualMaximum(Calendar.DAY_OF_MONTH) < Integer.parseInt(dateOfMonth)) { 
      calendar.set(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); 
     } else { 
      calendar.set(Calendar.DATE,Integer.parseInt(dateOfMonth)); 
     } 
    } 
    Log.i("timeAf",calendar.getTime()+""); 

    sharedPreferences.edit().putInt("monthInc"+id, calendar.get(Calendar.MONTH)).apply(); 
    sharedPreferences.edit().putInt("yearInc"+id, calendar.get(Calendar.YEAR)).apply(); 
    Intent notificationIntent = new Intent(context,AlarmBroadcastReceiver.class); 
    Bundle bundle = new Bundle(); 
    bundle.putSerializable("alarm", id); 
    notificationIntent.putExtra("bundle", bundle); 
    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    broadcast = PendingIntent.getBroadcast(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast); 
} 

報警觸發時間: Tue Feb 28 11:02:00 GMT+05:30 2017 Thu Mar 30 11:02:00 GMT+05:30 2017 Sun Apr 30 11:02:00 GMT+05:30 2017 Tue May 30 11:02:00 GMT+05:30 2017 Fri Jun 30 11:02:00 GMT+05:30 2017 Sun Jul 30 11:02:00 GMT+05:30 2017 Wed Aug 30 11:02:00 GMT+05:30 2017 Sat Sep 30 11:02:00 GMT+05:30 2017 Mon Oct 30 11:02:00 GMT+05:30 2017 Thu Nov 30 11:02:00 GMT+05:30 2017 Sat Dec 30 11:02:00 GMT+05:30 2017 Tue Jan 30 11:02:00 GMT+05:30 2018 Wed Feb 28 11:02:00 GMT+05:30 2018 ...