我的应用程序崩溃只是因为wakelock" DoNotSleep"

时间:2014-05-14 21:51:50

标签: android

我的应用程序有效但是当它停止服务它只是崩溃因为唤醒“DoNotSleep”状态所以我很乐意听到一些建议。 多数民众赞成我的logcat:

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

在我的代码中,服务中的wakelock存在:

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(); 
}

唤醒锁的崩溃怎么办?感谢。

2 个答案:

答案 0 :(得分:5)

您需要获取唤醒锁才能释放它:

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();
}

答案 1 :(得分:3)

在你释放Wakelock之前,你必须首先获得它

public void onCreate() {
    super.onCreate();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");

    // ADD THIS LINE
    wakeLock.acquire();
}