【发布时间】:2014-04-23 04:57:01
【问题描述】:
我已将警报设置为在 60 秒后触发。但警报会在 80/90 秒后触发(但不完全是 60 秒后)。如何设置闹钟以在指定时间准确触发?
//calling method to set alarm after 60 seconds
startAlarm(context, 1, System.currentTimeMillis()+60000);
//method defination
public static void startAlarm(Context context, int timerType, long nextScheduledTime) {
EABLog.d(TAG, " ~~~INSIDE START ALARM~~~~~ "+timerType);
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(Constants.KEY_TIMER_TYPE, timerType);
intent.setAction(""+timerType);
PendingIntent pollPendingIntent = PendingIntent.getBroadcast(context,
timerType, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextScheduledTime,
pollPendingIntent);
EABLog.d(TAG, System.currentTimeMillis()+" ALARM IS SET AT "+nextScheduledTime+" TYPE :"+timerType);
}
//permission added in Android Manifest
<uses-permission android:name="android.permission.WAKE_LOCK"/>
【问题讨论】:
-
你是在 kitkat 上开发吗?
-
如果您在 API 19 上对其进行测试,我认为您应该尝试 setInexactRepeating(),因为在 api 19 中所有警报都是不准确的。
-
是的。我正在 kitkat 上开发
-
代码似乎是正确的但也许你可以对广播动作感兴趣ACTION_TIME_TICK
标签: android alarmmanager