alaram manager class
im使用alarammanager.setrepeating在某些间隔后每次重复
public class AlarmService {
Long time;
AlarmManager alarmManager;
PendingIntent pendingIntent;
Context context;
public AlarmService(Context context){
this.context = context;
}
public AlarmService(Context context, Long time) {
this.time = time * 60000;
Log.e("time",this.time+" AlaramService");
this.context = context;
}
public void setAlarm() {
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, ReminderBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), time, pendingIntent);
Log.e("time",time+" setAlarm");
}
public void cancelAlarm() {
Intent intent = new Intent(context, ReminderBroadcastReceiver.class);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent,0);
alarmManager.cancel(sender);
}
}
设置AlarmManager在特定时间之后致电
Long time = Long.valueOf(settingsData.getInterval().split(" ")[0]);
AlarmService alarmService = new AlarmService(Setting.this,time);
alarmService.cancelAlarm();
alarmService.setAlarm();
提示通知的ReminderBroadcastService
public class ReminderBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
Toast.makeText(context, "Received", Toast.LENGTH_SHORT).show();
NotificationService notificationService = new NotificationService(context);
notificationService.callNotification();
}
您正在通过System.CurrentTimeMillis()
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(), time, pendingIntent);
对于设置警报功能中的警报时间。这就是为什么它立即触发。将未来的时间转换为与现在的第二天设置警报一样,
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+86400000L, time, pendingIntent);