什么是定期进行任务的正确方法是什么? 我的实现似乎并不正常:当屏幕上,我使用的服务方法是完全在时间跑了电话。 然而,当手机被锁定的服务在非常大的和随机的时间间隔运行(如:10:30 10:32 10:45 10:46 10:49 11:00 ...)下面是代码:
主要服务类:
@Override
public void onCreate() {
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Display a notification about us starting. We put an icon in the status bar.
showNotification();
mHandler.postDelayed(periodicTask, ONE_MINUTE);
}
private Handler mHandler = new Handler();
private static final int ONE_MINUTE = 60000;
private Runnable periodicTask = new Runnable() {
public void run() {
try{
wakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wakelock.acquire();
Log.v("PeriodicTimerService","Awake");
getValues();
writeDB();
writeLog();
mHandler.postDelayed(periodicTask, ONE_MINUTE);
}
finally{
wakelock.release();
}
}
};