gpt4 book ai didi

android - 我的应用程序崩溃只是因为唤醒锁 "DoNotSleep"

转载 作者:行者123 更新时间:2023-11-29 14:31:48 25 4
gpt4 key购买 nike

我的应用程序可以运行但是当它进入停止服务时它会崩溃,因为唤醒锁处于“DoNotSleep”状态,所以我很乐意听到一些建议。那是我的日志:

05-15 00:41:06.925: E/AndroidRuntime(4471): FATAL EXCEPTION: main

05-15 00:41:06.925: E/AndroidRuntime(4471): java.lang.RuntimeException: Unable to stop service com.example.iw84u.GpsTracker@40812c40: java.lang.RuntimeException: WakeLock under-locked DoNotSleep

05-15 00:41:06.925: E/AndroidRuntime(4471): at android.app.ActivityThread.handleStopService(ActivityThread.java:2086)

05-15 00:41:06.925: E/AndroidRuntime(4471): at android.app.ActivityThread.access$2900(ActivityThread.java:117)

05-15 00:41:06.925: E/AndroidRuntime(4471): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1001)

05-15 00:41:06.925: E/AndroidRuntime(4471): at android.os.Handler.dispatchMessage(Handler.java:99)

05-15 00:41:06.925: E/AndroidRuntime(4471): at android.os.Looper.loop(Looper.java:130)

05-15 00:41:06.925: E/AndroidRuntime(4471): at android.app.ActivityThread.main(ActivityThread.java:3691)

05-15 00:41:06.925: E/AndroidRuntime(4471): at java.lang.reflect.Method.invokeNative(Native Method)

05-15 00:41:06.925: E/AndroidRuntime(4471): at java.lang.reflect.Method.invoke(Method.java:507)

05-15 00:41:06.925: E/AndroidRuntime(4471): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)

05-15 00:41:06.925: E/AndroidRuntime(4471): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)

05-15 00:41:06.925: E/AndroidRuntime(4471): at dalvik.system.NativeStart.main(Native Method)

05-15 00:41:06.925: E/AndroidRuntime(4471): Caused by: java.lang.RuntimeException: WakeLock under-locked DoNotSleep

05-15 00:41:06.925: E/AndroidRuntime(4471): at android.os.PowerManager$WakeLock.release(PowerManager.java:311)

05-15 00:41:06.925: E/AndroidRuntime(4471): at android.os.PowerManager$WakeLock.release(PowerManager.java:286)

05-15 00:41:06.925: E/AndroidRuntime(4471): at com.example.iw84u.GpsTracker.onDestroy(GpsTracker.java:126)

05-15 00:41:06.925: E/AndroidRuntime(4471): at android.app.ActivityThread.handleStopService(ActivityThread.java:2069)
05-15 00:41:06.925: E/AndroidRuntime(4471): ... 10 more

在我的代码中服务唤醒锁存在:

public void onCreate() {
super.onCreate();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");
}
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

wakeLock.release();

}
public int onStartCommand(Intent intent, int flags, int startId) {
// Here there's some calculations with the intent

return START_REDELIVER_INTENT;

}
private void UpdateWithNewLocation(final Location loc) {
//Some commands and then .....
stopSelf();
}

我能用唤醒锁崩溃做什么?谢谢。

最佳答案

你需要先获得唤醒锁,然后才能释放它:

http://developer.android.com/reference/android/os/PowerManager.WakeLock.html#acquire()

那是因为默认情况下唤醒锁是引用计数的。如果唤醒锁是引用计数的,那么对 acquire() 的每次调用都必须通过对 release() 的相同数量的调用来平衡(反之亦然)。如果唤醒锁是引用计数的,并且您释放它的频率高于获取它的频率(在您的情况下您根本没有获取它),那么将抛出 WakeLock 未锁定异常。

http://developer.android.com/reference/android/os/PowerManager.WakeLock.html#setReferenceCounted(boolean)

您的代码仅创建唤醒锁,但从未获取它。为了平衡计数,在onCreate()中获取:

public void onCreate() {
super.onCreate();

// this only creates the wake lock without acquiring it
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");

// this acquires the wake lock
wakeLock.acquire();
}

关于android - 我的应用程序崩溃只是因为唤醒锁 "DoNotSleep",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23665791/

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