我需要每天重复闹钟。这就是我单次触发报警的方式。
public void clicker (View view)
{
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(AddAlarm.this,new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker timePicker,
int selectedHour, int selectedMinute)
{
calendar.set(Calendar.HOUR_OF_DAY, selectedHour);
calendar.set(Calendar.MINUTE, selectedMinute);
int c_hour,c_min;
String format;
c_hour=selectedHour;
c_min=selectedMinute;
if (c_hour == 0) {
c_hour += 12;
format = "AM";
} else if (c_hour == 12) {
format = "PM";
} else if (c_hour > 12) {
c_hour -= 12;
format = "PM";
} else {
format = "AM";
}
String formatTime = String.format("%02d : %02d %s", c_hour, c_min, format);
TextView dimple = (TextView)findViewById(R.id.timeText);
dimple.setText(formatTime);
setAlarm(calendar);
}
}, hour, minute, false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
private void setAlarm(Calendar targetCal)
{
Intent alarmintent = new Intent(AddAlarm.this, AlarmReceiver.class);
alarmintent.putExtra("ALARM_NO", tempx);
alarmintent.setAction("my.action.string");
PendingIntent sender = PendingIntent.getBroadcast(AddAlarm.this, pen, alarmintent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), sender);
}
现在我应该如何更改我的代码以每天重复触发警报?我怎么能杀死警报呢?谢谢,
答案 0 :(得分:0)
使警报重复使用alarmManager.setRepeating()
例如:
Intent myIntent = new Intent(MainActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 60 * 12, pendingIntent); // format : nanosec*sec*min*hr
答案 1 :(得分:0)
使用AlarmManager.setRepeating()方法代替AlarmManager.setExact()
。
例如:
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
Here您可以找到有关如何安排重复闹铃以及如何取消的详细信息。
答案 2 :(得分:0)
如果您想要唤醒手机,则无法使用alarmManager.setRepeating()
,因为它不会{19}来自API 19的手机。您必须每次重新创建闹钟时间。
这就是我创建事件的方式:
PendingIntent i = PendingIntent.getBroadcast(this, 1, alarmIntent, 0);
TimePickerDialog tp = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// set the TexView's text.
((TextView) v).setText(String.format("%02d:%02d", hourOfDay, minute));
// cancel previous alarm
manager.cancel(i);
// set alarm if the time is not 00:00
if(hourOfDay != 0 || minute != 0) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
// if the time is before now then add one day to it
if(calendar.getTimeInMillis()<System.currentTimeMillis())
calendar.setTimeInMillis(calendar.getTimeInMillis()+86400000);
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), i);
}
}
}, 12, 0, true);
tp.setTitle(getResources().getString(R.string.time_picker_title));
tp.show();
这就是我每次都重新设置的方式,我已经对它进行了测试,效果很好。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Setting up the next notification
PendingIntent alarmPIntent = PendingIntent.getBroadcast(context, 0,
new Intent(context, AlarmReceiver.class), 0);
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
int interval = 86400000; //one day
Calendar calendar = Calendar.getInstance();
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + interval, alarmPIntent);
Log.i(TAG, "Next alarm set to one day after now");
}
}
您可以在收到警报时执行任何操作,只需将接收器添加到清单文件的应用程序标记中:<receiver android:name="hu.bendaf.vivago.datatransmission.communication.AlarmReceiver"/>
就是这样。