2012-11-05 89 views
1

我正在開發一個基於鬧鐘的應用程序,在這個應用程序中,我必須根據用戶輸入重複每週工作日的警報(如每週一,週二,週三)。我用這個片段android每週一或每週二發出重複鬧鐘

Intent intent = new Intent(context, AlarmReceiver.class); 
PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP, alarmToSetInMilliSeconds, sender); 

如果用戶選擇每週一指,我發現了毫秒的時間下週一的日期和我設置鬧鐘,它的做工精細,我怎麼能重複用於其他下週一,我想一些想法來實現它,任何幫助表示讚賞。謝謝。

回答

1

這裏是我的代碼每月設置鬧鐘重複在第四

public void init(Context context) { 

    Intent intent = new Intent(context, AlarmReceiver.class); 
    pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    // Get the AlarmManager service 
    alarmManager = (AlarmManager) context 
      .getSystemService(Context.ALARM_SERVICE); 
} 
public void createAlarmControler(Context context) { 

    init(context); 
    Calendar cal = Calendar.getInstance(); 

    cal.set(Calendar.DAY_OF_MONTH, 4); 
    cal.set(Calendar.HOUR_OF_DAY, 10); 
    cal.set(Calendar.MINUTE, 0); 
    cal.set(Calendar.SECOND, 0); 
    Date now = new Date(System.currentTimeMillis()); 
    if (cal.getTime().before(now)) { 
     cal.add(Calendar.MONTH, 1); 
    } 
    long firstTime = cal.getTime().getTime(); 

    alarmManager.cancel(pendingIntent); 
    Log.e("", "firstTime: " + firstTime); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 1000 * 60 
      * 60 * 24 * 30, pendingIntent); 
} 

public void cancelAlarmControler(Context context) { 

    init(context); 

    alarmManager.cancel(pendingIntent); 
}