当手机屏幕锁定时,Android 计时器会延迟计时
我有一个在方法中运行 TimerTask 的服务,该方法必须每秒计数并在一段时间后执行某些操作。我的服务工作正常,当我在服务中启动该方法时,计时器开始每秒计时(1000 毫秒)。问题是,当设备的屏幕关闭时,我的计时器有时会停止并且无法正确计数。我可以在日志文件中看到计时器停止 15 秒,然后运行 5 秒,然后再次停止...当设备连接到计算机时,计时器始终正常工作,当我断开设备与计算机的连接时会出现此问题。
这是我的服务:
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
#### Screen is getting Locked here ####
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
#### Screen Locked ####
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 秒...
关于如何防止计时器在设备屏幕锁定时停止有什么意见吗?
I have a service that runs a TimerTask in a method, which must count every second and do something after some time. My service works properly and when i start the method in service, timer starts to tick every second (1000 milliseconds). The problem is, when device's screen turns off, my timer stops sometimes and doesn't count properly. I can see in the log file that timer stops for 15 seconds, then runs 5 seconds, then stops again... When the device is connected to computer, timer always works normally, this problem occurs when i disconnect the device from computer.
Here's my service:
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);
}
}
And here's the logcat output:
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
#### Screen is getting locked here ####
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
#### Screen Locked ####
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
You can see that after the screen locked, timer doesn't seem to tick every second. It stops for 15 - 20 seconds periods then works normally for 5 seconds...
Any opinion about how to prevent timer to stop when device screen locked?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你需要一个 部分唤醒锁定
这将阻止 CPU 在屏幕关闭时进入睡眠状态。但请注意,误用可能会导致电池问题,并且您需要获得使用唤醒锁的许可。
Sounds like you need a Partial Wake Lock
This will stop the CPU from going into a sleep state when the screen is off. Be warned though, misuse can cause battery issues and you need a permission to use wake locks.