gpt4 book ai didi

java - 当手机屏幕锁定时,Android 计时器会延迟

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:01 25 4
gpt4 key购买 nike

我有一个在方法中运行 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

#### 屏幕在此处被锁定 ####

    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

可以看到锁屏后,timer好像不是每秒都在滴答滴答。它会停止 15 - 20 秒,然后正常工作 5 秒...

关于如何防止设备屏幕锁定时计时器停止的任何意见?

最佳答案

听起来你需要一个 Partial Wake Lock这将阻止 CPU 在屏幕关闭时进入休眠状态。不过请注意,误用会导调用池问题,您需要获得许可才能使用唤醒锁。

关于java - 当手机屏幕锁定时,Android 计时器会延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5778409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com