激活锁定" DoNotSleep"继续崩溃.. [英] Wakelock "DoNotSleep" Continue to crash..
问题描述
从我的最后一个问题:<一href=\"http://stackoverflow.com/questions/23665791/my-app-crashes-just-because-wakelock-donotsleep\">My应用程序崩溃,只是因为激活锁定&QUOT; DoNotSleep&QUOT;
From my last question : My app crashes just because wakelock "DoNotSleep"
我添加了一行:wakeLock.acquire();
I added the line : wakeLock.acquire();
但我的问题继续...
But my problem continue... :
这就是我的logcat的:
Thats my logcat :
05-15 12:46:29.316: E/AndroidRuntime(2066): FATAL EXCEPTION: main
05-15 12:46:29.316: E/AndroidRuntime(2066): java.lang.RuntimeException: WakeLock under-locked DoNotSleep
05-15 12:46:29.316: E/AndroidRuntime(2066): at android.os.PowerManager$WakeLock.release(PowerManager.java:945)
05-15 12:46:29.316: E/AndroidRuntime(2066): at android.os.PowerManager$WakeLock.release(PowerManager.java:916)
05-15 12:46:29.316: E/AndroidRuntime(2066): at com.example.iw84u.GpsTracker.UpdateWithNewLocation(GpsTracker.java:190)
05-15 12:46:29.316: E/AndroidRuntime(2066): at com.example.iw84u.GpsTracker.access$0(GpsTracker.java:169)
05-15 12:46:29.316: E/AndroidRuntime(2066): at com.example.iw84u.GpsTracker$MyLocationList.onLocationChanged(GpsTracker.java:213)
05-15 12:46:29.316: E/AndroidRuntime(2066): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:261)
05-15 12:46:29.316: E/AndroidRuntime(2066): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:189)
05-15 12:46:29.316: E/AndroidRuntime(2066): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:206)
05-15 12:46:29.316: E/AndroidRuntime(2066): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 12:46:29.316: E/AndroidRuntime(2066): at android.os.Looper.loop(Looper.java:137)
05-15 12:46:29.316: E/AndroidRuntime(2066): at android.app.ActivityThread.main(ActivityThread.java:5293)
05-15 12:46:29.316: E/AndroidRuntime(2066): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 12:46:29.316: E/AndroidRuntime(2066): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 12:46:29.316: E/AndroidRuntime(2066): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-15 12:46:29.316: E/AndroidRuntime(2066): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-15 12:46:29.316: E/AndroidRuntime(2066): at dalvik.system.NativeStart.main(Native Method)
在我的code:
And in my code:
public void onCreate() {
super.onCreate();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");
wakeLock.acquire();
}
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
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) {
wakeLock.release();
//Some commands and then .....
stopSelf();
}
请注意:我改变了wakeLock.release();并提出,在功能UpdateWithNewLocation(最终位置LOC)的启动,因为否则它不停止。
Note: I changed the wakeLock.release(); And put that at the start of the function UpdateWithNewLocation(final Location loc) Because otherwise it doesnt stop.
现在我的问题是,我仍然得到错误信息。
Now my problem is that I still get error message.
推荐答案
如果一个锁被释放更多的时间比被收购,会出现此异常(<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.0_r1/android/os/PowerManager.java#PowerManager.WakeLock.release%28%29\"相对=nofollow>来源):
This exception occurs if a lock is released more times than it was acquired (source):
public void release()
{
synchronized (mToken) {
if (!mRefCounted || --mCount == 0) {
try {
mService.releaseWakeLock(mToken);
} catch (RemoteException e) {
}
mHeld = false;
}
if (mCount < 0) {
throw new RuntimeException("WakeLock under-locked " + mTag);
}
}
}
您获得锁只有一次(在的onCreate
),但它释放多次(在 UpdateWithNewLocation
)
You acquire the lock only once (in onCreate
) but release it multiple times (in UpdateWithNewLocation
)
所以,与其在的onCreate锁定它()
和 UpdateWithNewLocation释放它()
,你应该收购它 UpdateWithNewLocation()
的开始和结束释放。
So, instead of locking it in onCreate()
and releasing it in UpdateWithNewLocation()
, you should probably acquire it in the beginning of UpdateWithNewLocation()
and release it in its end.
这篇关于激活锁定&QUOT; DoNotSleep&QUOT;继续崩溃..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!