Reputation: 81
From my last question : My app crashes just because wakelock "DoNotSleep"
I added the line : wakeLock.acquire();
But my problem continue... :
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)
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();
}
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.
Upvotes: 0
Views: 342
Reputation: 137282
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);
}
}
}
You acquire the lock only once (in onCreate
) but release it multiple times (in 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.
Upvotes: 1