我希望它计划每天重复运行每10分钟一次。
以下是执行为其设置的挂起意图的代码。

问题是这每天运行10分钟,而不是在特定的时间窗口(上午9点到下午4点)内运行。

关于AlarmManager在特定时间窗口内重复触发的任何帮助。

下面是我当前的代码:

        int MILLISECOND_PER_MINUTE = 60 * 1000;
        int frequencyInMinutes = 10;// 10 mins interval
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), (frequencyInMinutes * MILLISECOND_PER_MINUTE), pendingIntent);

最佳答案

您可以使用设定的时间尝试一下并根据需要进行检查吗?在上午9点开始闹钟,然后在4点取消。
在您的接收器中检查时间,如果有4点,则取消警报。

openTimePickerDialog(false);


private void openTimePickerDialog(boolean is24r){
Calendar calendar = Calendar.getInstance();
timePickerDialog = new TimePickerDialog(
AndroidTimeActivity.this,
onTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
is24r);
timePickerDialog.setTitle("Set Alarm Time");

timePickerDialog.show();

}

OnTimeSetListener onTimeSetListener
= new OnTimeSetListener(){

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();

calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);

if(calSet.compareTo(calNow) <= 0){
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}

setAlarm(calSet);
}};

 private void setAlarm(Calendar targetCal){

 textAlarmPrompt.setText(
 "\n\n***\n"
 + "Alarm is set@ " + targetCal.getTime() + "\n"
 + "***\n");

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),   RQS_1, intent, 0);
 AlarmManager alarmManager =    (AlarmManager)getSystemService(Context.ALARM_SERVICE);
 alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

}


希望这会帮助你。

关于android - AlarmManager计划从上午9点至下午4点每10分钟运行一次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39242147/

10-10 18:02