当我设置 AlarmManager 时立即触发警报我在下面的代码中做错了什么
Alarm Triggered Immediately when I set AlarmManager what Im doing wrong in below code
Alaram 经理 Class
我使用 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);
Alaram 经理 Class
我使用 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);