2

如何每天在特定时间使用警报管理器启动和结束功能

例如开始时间:07:00 结束时间:15:00

 Calendar cur_cal = new GregorianCalendar();
 cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

 Calendar cal = new GregorianCalendar();
 cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
 cal.set(Calendar.HOUR_OF_DAY, 07);
 cal.set(Calendar.MINUTE, 00);
 cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
 cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
 cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
 cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(this, MyBroadcastReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(
            this.getApplicationContext(), 234324243, intent, 0);
 AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
 alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pendingIntent );
4

1 回答 1

0

设置一个在 7:00 开始的警报和一个在 15:00 停止的警报,使用 Intent-Extras 指定正确的操作(启动或停止)。

另外,比较Using Alarmmanager to start a service at specific time

于 2012-11-01T12:42:53.840 回答