androidbatteryidle-timerscreen-offmobile-data

How to prevent phone from going to idle mode


I've developed an application that streams music (via internet connection) using service and having trubles streaming content without phone going idle.

While i was developing my application each time i tried case mentioned below the music was reproducing fine.

Use case : search song, select song from results, play song, screen off -> auto play next song from result list

I'm developing using real device - Huawei Mate 20 Lite - OS v8.01 so while debugging it gotta use USB cabel. Like i said following the use case above while hooked on USB the auto play while screen off works good. The case it doesn't work good is when the cable is not connected (only mobile data turned on).

What I've figured out is that phone when connected on USB is probably keeping the device awake and it doesn't go to idle mode while when not connected after around 5mins the device probably shuts down processes that cost energy or it shuts down connection to mobile data i'm not sure and there's where i need you guys.

Also I've tested app using HTC U Play - OS v6.0 and the streaming goes smooth without interrupts while screen off and phone wasn't touched for 10+mins.

Also I've tried to acquire wakelock inside oncreate and without releasing it just to see if it helps and it doesn't.

pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock"); wl.acquire();


Solution

  • This problem you are facing can be due to the fact that after Andriod 6.0, all apps are getting optimized for optimizing the battery usage.

    If you really think, the reason behind the application to go killed is inactivity. Then, probably, its because of battery optimization software itself.

    You can enable another permission while installing the app on the device where you can update the list of unoptimized app by adding an entry for your app.

    Originally, you will be able to do the manual settings by following below instruction.

    . Head for the ‘Settings‘ app and then ‘Battery‘

    . On the ‘three dots‘ menu, top right, you’ll find ‘Battery optimisation‘.

    . Here you’ll see a list of all applications which shouldn’t be ‘optimised‘ (for which read ‘can be handled by Doze and App Standby’) – by default the list is usually very small, with almost all apps enabled for ‘optimisation’. Which is fine for general users, but if, like me, you want a few applications to live outside of the new battery optimisations, then tap on the ‘Not optimised‘ pick list and choose ‘All apps‘

    . As you’d expect, every application on your phone is listed (this may be quite long) – swipe down until you find the application(s) that you particularly want to always keep running. Tap on the application name

    . From the two choices, check the box for ‘Don’t optimise‘.