每周为选定日期设置重复警报
问题描述:
我正在创建具有多天设置警报的警报应用程序。我已经设置了闹钟一天。但不知道如何在选定日期设置相同的闹钟。要选择多天,我使用checkboxes
。每周为选定日期设置重复警报
在这里,我得到了用户选择的天数:
//set alarm repeat days method
public void showDialogAlarmdays() {
setRepeatTxt.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
selections="";
ad.show();
}
}
);
final String[] items=getResources().getStringArray(R.array.my_date_choose);
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
// Set the dialog title
builder.setTitle("Choose your days");
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
builder.setMultiChoiceItems(R.array.my_date_choose, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(items[which]);
} else if (mSelectedItems.contains(items[which])) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(items[which]);
}
}
});
// Set the action buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
selections="";
for (String ms:mSelectedItems) {
if(selections==""){
selections=ms;
}else{
selections=selections+","+ms;
}
}
// Toast.makeText(addTimeSlot.this,selections, Toast.LENGTH_LONG).show();
if(selections.equals("")){
showRepeatTxt.setText("Choose your days");
}else{
showRepeatTxt.setText(selections);
}
//Toast.makeText(setAlarm.this,selections, Toast.LENGTH_LONG).show();
}
});
builder .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
ad = builder.create();
}
举个例子,如果用户选择周一,周二和周五,随即报警应该重复选定的日子。
以下方法设置报警:
alarm_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(setAlarm.this,hour+" : "+min, Toast.LENGTH_SHORT).show();
//celender set time
calendar.set(Calendar.HOUR_OF_DAY,Cal_hour);
calendar.set(Calendar.MINUTE,Cal_minute);
//put extra string into Alarm_intent
//tells the clock that you pressed the 'OK' button
Alarm_intent.putExtra("extra","on");
//put extra int into Alarm_intent
//tells the clock that you want to certain value from spinner
Alarm_intent.putExtra("ringtoneChoice",choose_ringtone);
Log.e("Ringtone id : ", String.valueOf(choose_ringtone));
//create a pending intent that delay the intent until the specified calendar time
pending_intent = PendingIntent.getBroadcast(setAlarm.this.getApplicationContext(),0,
Alarm_intent,pending_intent.FLAG_UPDATE_CURRENT);
//set the alarm manager
alarm_Manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),
pending_intent);
Toast.makeText(setAlarm.this,"Alarm is set...!", Toast.LENGTH_SHORT).show();
finish();
}
});
在这里calendar.set(Calendar.HOUR_OF_DAY,Cal_hour)和calendar.set(Calendar.MINUTE,Cal_minute)值从时间选择器获得。
答
Intent myIntent = new Intent(AndroidAlarmService.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(AndroidAlarmService.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
//alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);