In my app, in the onCreate() method of the main activity I am creating a wake lock so that the CPU will keep running if the phone goes on standb/screen turns off.
Also in the onCreate method I have an intent to create a service that uses the accelerometer. This service needs to be continuously running while the app is open and monitoring accelerometer values (I know this isn't good for battery but I need it to do that). Here is my code at the moment and the service starts fine.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Howaya");
wl.acquire();
if (appStart == true)
{
Intent AccelService = new Intent(this, Accelerometer.class);
AccelService.putExtra("unreg", false);
startService(AccelService);
}
appStart = false;
}
I have the following permission set in my manifest -
<uses-permission android:name="android.permission.WAKE_LOCK" />
I have tried this with different locks - dim screen and full brightness to no avail too. My output on logcat is here -
F/PowerManager(15628): android.util.Log$TerribleFailure: WakeLock finalized while still held: Howaya
F/PowerManager(15628): at android.util.Log.wtf(Log.java:260)
F/PowerManager(15628): at android.util.Log.wtf(Log.java:239)
F/PowerManager(15628): at android.os.PowerManager$WakeLock.finalize(PowerManager.java:329)
F/PowerManager(15628): at dalvik.system.NativeStart.run(Native Method)
I have seen people saying that partial wakelocks don't work like they should do, such as this link Google standby error page but this was released and closed last year so I don't know is that the case, can anyone help here please? I have a HTC Desire as regards that last point too, Thanks.
A partial wake lock becomes stuck if it is held for a long time while your app is running in the background (no part of your app is visible to the user). This condition drains the device's battery because it prevents the device from entering lower power states.
Using android:keepScreenOn="true" is equivalent to using FLAG_KEEP_SCREEN_ON . You can use whichever approach is best for your app. The advantage of setting the flag programmatically in your activity is that it gives you the option of programmatically clearing the flag later and thereby allowing the screen to turn off.
*If you hold a partial wake lock, the CPU will continue to run, regardless of any display timeouts or the state of the screen and even after the user presses the power button. In all other wake locks, the CPU will run, but the user can still put the device to sleep using the power button.
A wake lock is a mechanism to indicate that your application needs to have the device stay on. Any application using a WakeLock must request the android. permission. WAKE_LOCK permission in an <uses-permission> element of the application's manifest.
Problem occurs because your WakeLock object is a local scoped variable inside OnCreate method. After method being executed - WakeLock object is no more referenced - thus eligible for garbage collection. If Dalvik GC occurs - object is ready for finalize - and finalizer internal code warns You, that WakeLock is still held - and active - but will be GC'ed. You have to obtain new WakeLock object and assign it to a field of class type WakeLock in Your activity derived class. Read about Object Oriented programming and Garbage Collector - You will understand the issue.
In my app, in the onCreate() method of the main activity I am creating a wake lock so that the CPU will keep running if the phone goes on standb/screen turns off.
Please use android:keepScreenOn
on one of the widgets in your layout instead. That is far, far safer for activities than manually dealing with a WakeLock
. Plus, you don't need the WAKE_LOCK
permission then, IIRC.
My output on logcat is here
That error is because you never release the WakeLock
. Please do not leak WakeLocks
.
Now, you managed to write all of this prose and include all of these listings, and you forgot one little thing: actually telling us what your question is. This is a rather important item for a question-and-answer site like StackOverflow. And, no, "can anyone help here please?" does not count as a question when you never define what "help" it is you are looking to receive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With