Android闹钟启动时间设置无效问题的解决方法闹钟启动时间设置无效问题的解决方法
Android开发中,alarmManager在5.0以上系统,启动时间设置无效的问题
做一个app,需要后台保持发送心跳包。由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmManager定时唤
醒Service发送心跳包。
以下是开启alarmManager的代码
//开启轮询服务
public static void startPollingService(Context context, int seconds, Class<?> cls,String action) {
//获取AlarmManager系统服务
AlarmManager manager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
//包装需要执行Service的Intent
Intent intent = new Intent(context, cls);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
//触发服务的起始时间
long triggerAtTime = SystemClock.elapsedRealtime();
//使用AlarmManger的setRepeating方法设置定期执行的时间间隔(seconds秒)和需要执行的Service
manager.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime,
seconds * 1000, pendingIntent);
}
结果遇到了很奇怪的问题
传入的时间是2500,也就是每2.5秒一次
在红米1s (系统是CM12.1 android5.1.1)上,亮屏(非休眠)状态下它要好几十秒才会唤醒一次,锁屏(休眠)就不会唤醒
了
在小米4(系统是MIUI7 android4.4.4)上,亮屏状态下正常,锁屏状态下就不会唤醒了
我尝试了BroadcastReceiver重写onReceive,也试过Service重写onStartCommand,都是一样的情况
原因是因为Android AlarmManagerService里面对于repeating alarm 做了限制。
注意:注意:在19以上版本,setRepeating中设置的频繁只是建议值, 5.0 以上的源码中最小值是60s
class AlarmManagerService extends SystemService {
// Minimum alarm recurrence interval
private static final long MIN_INTERVAL = 60 * 1000; // one minute, in millis
void setImpl(int type, long triggerAtTime, long windowLength, long interval,
PendingIntent operation, boolean isStandalone, WorkSource workSource,
AlarmManager.AlarmClockInfo alarmClock) {
if (operation == null) {
Slog.w(TAG, "set/setRepeating ignored because there is no intent");
return;
}
// Sanity check the window length. This will catch people mistakenly
// trying to pass an end-of-window timestamp rather than a duration.
if (windowLength > AlarmManager.INTERVAL_HALF_DAY) {
Slog.w(TAG, "Window length " + windowLength
+ "ms suspiciously long; limiting to 1 hour");
windowLength = AlarmManager.INTERVAL_HOUR;
}
// Sanity check the recurrence interval. This will catch people who supply
// seconds when the API expects milliseconds.
if (interval > 0 && interval < MIN_INTERVAL) {
Slog.w(TAG, "Suspiciously short interval " + interval
+ " millis; expanding to " + (int)(MIN_INTERVAL/1000)
+ " seconds");
interval = MIN_INTERVAL;
}
评论0