我有一个在方法中运行TimerTask的服务,该服务必须每秒计数并在一段时间后执行某些操作。我的服务正常工作,当我启动服务中的方法时,计时器开始每隔一秒(1000毫秒)打勾。问题是,当设备屏幕关闭时,我的计时器有时停止并且计数不正确。我可以在日志文件中看到计时器停止15秒,然后运行5秒,然后再次停止...当设备连接到计算机时,计时器始终正常工作,当我从计算机断开设备时会出现此问题。 / p>
这是我的服务:
public class MyService extends Service {
private String TAG = getClass().getName();
public void count() {
Log.i(TAG, "Service Count Method Start");
int counter;
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Log.i(TAG, "Timer count value : " + counter);
if (!someCondition) {
counter++;
if (DefaultApplication.notOnDisplayTime >= 180) {
Log.d(TAG, "APPLICATION REACHED TIME LIMIT!");
someCondition = true;
} else
someCondition = false;
} else {
counter = 0;
}
}
}, 0, 1000);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
public class MyServiceBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
private final IBinder mBinder = new MyServiceBinder();
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "MyService Unbound");
return super.onUnbind(intent);
}
}
这是logcat输出:
04-25 14:35:22.951 I/com.mypackagename.MyService( 6745): Timer count value : 73
04-25 14:35:23.949 I/com.mypackagename.MyService( 6745): Timer count value : 74
04-25 14:35:24.019 D/dalvikvm( 6832): GC_FOR_MALLOC freed 6656 objects / 495856 bytes in 61ms
04-25 14:35:24.951 I/com.mypackagename.MyService( 6745): Timer count value : 75
04-25 14:35:25.951 I/com.mypackagename.MyService( 6745): Timer count value : 76
####屏幕被锁定在这里####
04-25 14:35:26.929 I/power ( 96): *** set_screen_state 0]
04-25 14:35:26.959 V/KeyguardViewMediator( 96): password timeout now
04-25 14:35:26.959 D/KeyguardViewManager( 96): show()
04-25 14:35:26.999 D/Sensors ( 96): close_akm, fd=138
04-25 14:35:26.999 I/com.mypackagename.MyService( 6745): Timer count value : 77
04-25 14:35:27.039 D/HtcLockScreen( 96): onScreenRestart
04-25 14:35:27.039 I/HtcLockScreen( 96): updateStatusViewByPriority, mIsSimCheckView = false, mIsBatteryInfo = false, mIsMusicPlaying = false, mIsAirPlaneMode = false
04-25 14:35:27.049 I/HtcLockScreen( 96): HtcLockScreen:onResume
04-25 14:35:27.069 D/SurfaceFlinger( 96): Layer::setBuffers(this=0x785580), pid=96, w=480, h=762
04-25 14:35:27.079 D/SurfaceFlinger( 96): Layer::setBuffers(this=0x785580), pid=96, w=480, h=762
04-25 14:35:27.109 D/SurfaceFlinger( 96): Layer::requestBuffer(this=0x785580), index=0, pid=96, w=480, h=762 success
04-25 14:35:27.339 D/alogcat ( 6832): stopping ...
04-25 14:35:27.339 D/alogcat ( 6832): paused
04-25 14:35:27.609 D/SurfaceFlinger( 96): About to give-up screen, flinger = 0xb4e28
04-25 14:35:27.669 D/AK8973 ( 72): Compass CLOSE
####屏幕锁定####
04-25 14:35:27.949 I/com.mypackagename.MyService( 6745): Timer count value : 78
04-25 14:35:28.949 I/com.mypackagename.MyService( 6745): Timer count value : 79
04-25 14:35:44.602 I/com.mypackagename.MyService( 6745): Timer count value : 80
04-25 14:35:45.603 I/com.mypackagename.MyService( 6745): Timer count value : 81
04-25 14:35:45.784 I/wpa_supplicant( 256): CTRL-EVENT-SCAN-RESULTS Ready
04-25 14:35:45.799 D/LocationMasfClient( 96): getNetworkLocation(): Returning cache location with accuracy 75.0
04-25 14:35:46.603 I/com.mypackagename.MyService( 6745): Timer count value : 82
04-25 14:35:47.603 I/com.mypackagename.MyService( 6745): Timer count value : 83
04-25 14:35:48.604 I/com.mypackagename.MyService( 6745): Timer count value : 84
04-25 14:35:49.604 I/com.mypackagename.MyService( 6745): Timer count value : 85
04-25 14:36:10.558 D/SurfaceFlinger( 96): Layer::requestBuffer(this=0x785580), index=1, pid=96, w=480, h=762 success
04-25 14:36:11.033 I/com.mypackagename.MyService( 6745): Timer count value : 86
04-25 14:36:13.269 I/com.mypackagename.MyService( 6745): Timer count value : 87
04-25 14:36:13.289 D/com.mypackagename.utils.XUtil( 6745): return 0 char $
04-25 14:36:14.039 D/com.mypackagename.utils.XUtil( 6745): return 0 char $
04-25 14:36:14.269 I/com.mypackagename.MyService( 6745): Timer count value : 88
04-25 14:36:17.009 I/com.mypackagename.MyService( 6745): Timer count value : 89
04-25 14:36:29.512 I/com.mypackagename.MyService( 6745): Timer count value : 90
你可以看到屏幕锁定后,计时器似乎每秒都没有打勾。它停止15 - 20秒,然后正常工作5秒......
有关如何在设备屏幕锁定时阻止计时器停止的任何意见?