Android 警报 运行 但不是在秒为零时?
Android alarm running but not at when seconds are zero?
我正在制作报警应用程序。警报 运行 正常。例如,如果我将闹钟设置为 06:00 PM,它会在 06:00 PM 执行。问题是当秒数恰好为零时警报不会执行。有时 运行 在 32 秒 (06:00:32 PM) 有时在其他秒等
在我的主activity中,我设置了闹钟保存按钮,当我点击它时执行以下代码:
buttonSetAlarm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
00);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(getApplicationContext(),
"Invalid Date/Time",
Toast.LENGTH_LONG).show();
}else{
setAlarm(cal);
}
}});
main activity 中的 SetAlarm 方法是:
private void setAlarm(Calendar targetCal){
info.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.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 1000*60, pendingIntent);
}
在我的 AlarmReceiver BroadCastReceiver 中,我有以下 onReceive 方法:
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
}
关于我的问题应该出在哪里的想法?
从 API 19 (KitKat) 开始,警报默认为 不准确。如果您的 targetSdkVersion
设置为 19 或更高,将使用此行为。请参阅文档中有关 AlarmManager.setRepeating()
方法的注释:
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.
所以最好的办法是使用精确的一次性警报,并且每次警报到期时都会相应地重新设置警报。
Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.
我正在制作报警应用程序。警报 运行 正常。例如,如果我将闹钟设置为 06:00 PM,它会在 06:00 PM 执行。问题是当秒数恰好为零时警报不会执行。有时 运行 在 32 秒 (06:00:32 PM) 有时在其他秒等
在我的主activity中,我设置了闹钟保存按钮,当我点击它时执行以下代码:
buttonSetAlarm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
00);
if(cal.compareTo(current) <= 0){
//The set Date/Time already passed
Toast.makeText(getApplicationContext(),
"Invalid Date/Time",
Toast.LENGTH_LONG).show();
}else{
setAlarm(cal);
}
}});
main activity 中的 SetAlarm 方法是:
private void setAlarm(Calendar targetCal){
info.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.setRepeating(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), 1000*60, pendingIntent);
}
在我的 AlarmReceiver BroadCastReceiver 中,我有以下 onReceive 方法:
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
}
关于我的问题应该出在哪里的想法?
从 API 19 (KitKat) 开始,警报默认为 不准确。如果您的 targetSdkVersion
设置为 19 或更高,将使用此行为。请参阅文档中有关 AlarmManager.setRepeating()
方法的注释:
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.
所以最好的办法是使用精确的一次性警报,并且每次警报到期时都会相应地重新设置警报。
Note: Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.