为什么我关闭时总是响起警报 activity
Why alarm fire always when i close activity
在 Android studio 我希望我的应用程序发送 notification
给用户,如果他不使用它的话。
Notification
很火,但总是在我离开 activity 和 8:00
之后
@Override
protected void onStop() {
super.onStop();
System.out.println("STARTED!");
Intent intent = new Intent(this, Alarm.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 1);
alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
当我离开 activity 并且时间大于 8:00;
时,警报总是触发一次
因为不准确。
文档说 "Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact."
https://developer.android.com/training/scheduling/alarms
如果你想要准确的时间,你必须改变你的
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
方法。您应该考虑使用不重复警报(alarmManager.setExact method)
。如果您仍想安排重复闹钟,可以在 Alarm on receive 方法中重新安排。
在 Android studio 我希望我的应用程序发送 notification
给用户,如果他不使用它的话。
Notification
很火,但总是在我离开 activity 和 8:00
@Override
protected void onStop() {
super.onStop();
System.out.println("STARTED!");
Intent intent = new Intent(this, Alarm.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 1);
alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
当我离开 activity 并且时间大于 8:00;
时,警报总是触发一次因为不准确。
文档说 "Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact."
https://developer.android.com/training/scheduling/alarms
如果你想要准确的时间,你必须改变你的
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
方法。您应该考虑使用不重复警报(alarmManager.setExact method)
。如果您仍想安排重复闹钟,可以在 Alarm on receive 方法中重新安排。