How can I prevent Galaxy S3 from stopping my app when turning idle?

1.1k views Asked by At

I am currently updating my Android app with Samsung Galaxy S3 and was shocked that I couldn't stop the phone pausing my app when turning idle. With the Galaxy S2 of our department there doesn't occur this particular problem, if the screen goes black the app still streams data to the sd-card and over the wifi-network. The S3 stops any data-stream.

I tried now fiddling with the energy- and display-settings but I have no solution to the problem so far. My Internet-search was not succesfull either.

Possible solutions are rooting the new phone and thus making advanced settings visible or increasing the time-out (which i dont like so much as a solution).

Do you have any ideas how to solve the issue or general input that might enlighten me? Thnx!

BTW: here is the app in question (no ad): Google Play Link

1

There are 1 answers

1
Matt Gibson On BEST ANSWER

I have an app which needs to do something similar (it's a running trainer, so it needs to keep talking while the user keeps their phone in their pocket for half an hour or so.)

First off, a caveat for other people reading: don't do this if you don't have to. If you only need to do something periodically, rather than continuously, consider using AlarmManager, which can wake the phone up from sleep every now and again to do something, so won't hit the user's battery so hard.

But, if you're sure you need to keep the phone awake, you need to use a WakeLock. Here's roughly what I do in my service's onStartCommand:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);       
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK;, "RunClockService");
mWakeLock.acquire();

...where mWakeLock is an instance variable of type PowerManager.WakeLock. PARTIAL_WAKE_LOCK keeps the CPU running, but doesn't keep the screen on. The "RunClockService" tag is just used for debugging, according to the documentation. Change it to your class name.

Then, when I finish needing to keep the phone awake:

mWakeLock.release();

You'll also need to add WAKE_LOCK permission to your AndroidManifest.xml:

    <uses-permission android:name="android.permission.WAKE_LOCK"/>