Greg
Greg

Reputation: 93

Wakelock does not always keep my app alive

I made a simple Android GPS tracking service app to log the device locations in passive mode. To keep the CPU going I use a wake lock, but it’s not reliable. Sometimes the app runs for 3 hours. Sometimes it can do a full 24 hour cycle. I don’t get any crash report on screen – the app just stops without any indication. The battery life is fine.

I declare the wakelock as the main activity class variable

static WakeLock wakeLock;

And set the wakelock once right before starting the GPS service

PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock((PowerManager.PARTIAL_WAKE_LOCK),"MyAppNameWakelock");
wakeLock.setReferenceCounted(true);
wakeLock.acquire();

For screen changing reasons I also updated the BackButton function to run moveTaskToBack(true); instead of its normal operations. Could this have something to do with it?

Are there any mechanical interactions that can kill the wakelock in use, like attaching a power cord or USB cord?

Subsequently, is there a log of any sorts in Android that I can check to see why my app has stopped?

Upvotes: 3

Views: 1828

Answers (1)

VAndrei
VAndrei

Reputation: 5590

Theoretically if your app does not die from any other reason and has wakelock permissions (see here: <uses-permission android:name="android.permission.WAKE_LOCK" /> ) it should stay alive.

Whether wakelocks are removed by external events, it may be kernel dependent & device dependent. Some kernels may have very aggressive power management features that could take decisions to kill wakelocks. Did you ran your app on more devices ?

However I would investigate if another event killed my app (for ex. increasing memory usage).

Another idea would be overriding the "finalize" method and print a message when the wakelock is not reachable by the garbage collector to determine when it died.

Upvotes: 1

Related Questions