我一定没有正确实现唤醒锁。我就是这样做的。这个对吗?
private static final String LOCK_NAME_STATIC="domination.shredAssistant.locationService";
private static volatile PowerManager.WakeLock lockStatic=null;
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic==null) {
PowerManager mgr=(PowerManager)context.getSystemService(Context.POWER_SERVICE);
lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
LOCK_NAME_STATIC);
lockStatic.setReferenceCounted(true);
}
return(lockStatic);
}
和 onStart 方法
//onStart
public void onStart( Intent intent, int startId ) {
super.onStart( intent, startId );
if (running == false) {
Context ctxt=getApplicationContext();
getLock(ctxt).acquire();
running = true;
readResorts();
serviceHandler = new Handler();
serviceHandler.postDelayed( new RunTask(),1000L );
runtest=true;
locupdate(0,0);
}
}
即使我使用 getLock(ctxt).acquire(); ,我似乎也无法弄清楚为什么服务在一段时间不活动后仍然被终止;谢谢你的帮助!-Dom