I created an application which uses camera and during the appplication execution the screen is always on.
In the onCreate() method I added the lock:
It sounds as though your application is never calling release on the Wakelock
- is it possible that it's throwing an exception earlier in onStop
or onPause
? I'd include some logging where you're calling release to confirm it's being executed.
In any case, you'll definitely want to move the acquire
and release
methods into onResume
and onPause
respectively. The code you've got will only acquire the WakeLock the first time your application is started.
Thanks the Android's background processing your code has a potential imbalance. If the user presses the home key to switch applications the lock will be released. If they switch back to your app onCreate won't be called, so the lock will never be acquired.
Your best bet is to construct the WakeLock in onCreate (as you have), acquire the lock in onResume and release it in onPause (including the call to isHeld). This will guarantee that the lock will be held whenever your application is in the foreground.
I use mWakelock.setReferenceCounted(false), still dunno what it means, but it resolves my "Wakelock under-locked" problem.
Init: OnStart() acquire: onResume() release: onStop()
Where are you calling finish()?
You should be releasing your resources in onPause and not on onStop. There is no guarantee that onStop will always be called before your activity is killed. If the system needs resources then it may kill the activity before onStop is called.
Try to put debug logs and trace the execution path when you think your application is being terminated.
I would move the lock from OnCreate()
to OnResume()
. You want the lock during the visible lifetime of the Activity, not the entire lifetime of the Activity. You Activity could definetly still be running with another Activity running in front of it.
I would move the release to OnPause()
. OnPause()
is the earliest point your application should normally be killed by the OS.
Additionally, I wouldn't check to see if I have the lock before releasing. If you use OnResume()
to acquire the lock; isHeld
should always be true in OnPause()
.
I had troubles to release data when I was using OpenGL. So now, with android.os.Process.killProcess(android.os.Process.myPid()); it works just as I want !!
Thanks Art