我在Android中几乎完成了一个小闹钟应用程序,它工作正常,但是,现在它只能设置一天的闹钟,但设计已经选择不同的日子,例如(星期一,星期三,星期五)。这是我设置闹钟的方式:
Calendar cal = Calendar.getInstance();
//tPicker is a TimePicker
cal.set(Calendar.HOUR_OF_DAY, tPicker.getCurrentHour());
cal.set(Calendar.MINUTE, tPicker.getCurrentMinute());
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent(this));
//pIntent method
public static PendingIntent pIntent(Context context){
Intent intent = new Intent();
intent.setAction("myAlarm.intent.action.CLOCK");
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
我说的很棒,但我不知道如何在应用程序中选择的每一天设置闹钟,感谢您的帮助,谢谢!
答案 0 :(得分:1)
您需要设置警报管理器3次(如果选择星期一,星期三和星期五)。
代码从https://stackoverflow.com/a/19298186/2829009被盗:
cal1.set(Calendar.DAY_OF_WEEK,2); //monday
cal2.set(Calendar.DAY_OF_WEEK,4); //wednesday
cal3.set(Calendar.DAY_OF_WEEK,6); //friday
然后每天设置一个警报管理器:
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), pIntent(this));
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), pIntent(this));
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal3.getTimeInMillis(), pIntent(this));
修改强>
private static final MONDAY = 2;
private static final WEDNESDAY = 4;
private static final FRIDAY = 6;
Calendar calMonday = Calendar.getInstance();
//tPicker is a TimePicker
calMonday.set(Calendar.HOUR_OF_DAY, tPicker.getCurrentHour());
calMonday.set(Calendar.MINUTE, tPicker.getCurrentMinute());
calMonday.set(Calendar.SECOND, 0);
calMonday.set(Calendar.MILLISECOND, 0);
//now copy you calendar for the other days
Calendar calWednesday = calMonday.clone();
Calendar calFriday = calMonday.clone();
//set week days
calMonday.set(Calendar.DAY_OF_WEEK, MONDAY);
calWednesday.set(Calendar.DAY_OF_WEEK, WEDNESDAY);
calFriday.set(Calendar.DAY_OF_WEEK, FRIDAY);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calMonday.getTimeInMillis(), pIntent(this, MONDAY));
alarmMgr.set(AlarmManager.RTC_WAKEUP, calWednesday.getTimeInMillis(), pIntent(this, WEDNESDAY));
alarmMgr.set(AlarmManager.RTC_WAKEUP, calFriday.getTimeInMillis(), pIntent(this, FRIDAY));
//pIntent method
public static PendingIntent pIntent(Context context, int id){
Intent intent = new Intent();
intent.setAction("myAlarm.intent.action.CLOCK");
return PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
答案 1 :(得分:0)
您需要为每个警报设置不同的PendingIntents。来自文档:
如果您确实需要多个不同的PendingIntent对象,请执行此操作 同时(例如使用两个显示的通知 在同一时间),那么你需要确保有一些东西 将他们与不同的人联系起来是不同的 PendingIntents。这可以是所考虑的任何Intent属性 Intent.filterEquals,或提供给的不同请求代码整数 getActivity(Context,int,Intent,int),getActivities(Context,int, Intent [],int),getBroadcast(Context,int,Intent,int)或 getService(Context,int,Intent,int)。
http://developer.android.com/reference/android/app/PendingIntent.html
例如,更改此行:
return PendingIntent.getBroadcast(context, MONDAY, intent, PendingIntent.FLAG_UPDATE_CURRENT);
其中MONDAY是一个静态整数。