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.
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.