Change battery optimization programmatically on Android
Asked Answered
C

0

7

My app plays streamed audio in the background using Exoplayer but the system is shutting it down if the user doesn't whitelist us manually in the battery optimization settings.

There are other apps also running streamed audio in the background successfully without interruptions and without whitelisting in the battery optimization.

This is the code to take the user to settings app, with some instructions on how to do it. But each system has its own way / path to disable the battery optimizations, making our instructions not always accurate:

           Intent myIntent = new Intent();  
           myIntent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
                            startActivity(myIntent); 

Is There a way to whitelist us without prompting the user and be accepted in the review process? Or is there another way to avoid the system shutting down our streaming?

Thank you!

Canaday answered 5/12, 2018 at 21:22 Comment(14)
There is no way to be whitelisted automatically or programatically except by the OEM who installed the system. That's the purpose of the whitelist.Trevor
@GabeSechan I think the whitelist Winston's referring to is the "battery optimization" settings screen (described here) where you can "exempt" individual apps.Ximenez
@Ximenez yes. And you can't add yourself to it programmatically, on purpose. Because then every app would do itTrevor
@Winston you didn't mention a foreground service... are you sure that isn't the problem?Ximenez
@Ximenez I'm using a foreground service. It runs fine with older versions of Android that doesn't have the battery optimization feature. Thanks for pointing that out.Canaday
@Winston I think that the real question you should be asking is, "why doesn't exoplayer work consistently on Android 8.0+, without having to guide the user through the manual whitelisting process?" It seems to me that the exoplayer community would be howling if this problem didn't already have a solution.Ximenez
@Ximenez You're right. Searching about this audio issue on Exoplayer docs I found that it's necessary to use Wakelock and Wifilock. I'm testing it now and it seems to solve the problem.Canaday
@Ximenez as you pointed out, my problem was with foreground service.Canaday
@Winston could you specify what you mean by the problem with foreground service. I understand you were already using one - did you misconfigure something. How did you resolve the issue on Android 8.0 and higher? Thanks.Treatise
@Treatise what I meant was that at that time I didn't have a clear understanding of the difference between an audio background task/service and a foreground service. I did have my audio working on a background task/service, which was enough for systems without the doze feature, but not enough for later systems with the doze feature. I hope this helps you somehow also.Canaday
@Winston Oh, I see. In theory foreground service should not be interrupted by any battery optimizations, but I read reports that some of the manufactures do not respect foreground service and kill the process anyway. Just exploring the matter. Thank you for taking time to clarify what you meant.Treatise
@Treatise No problem, glad to communicate!Canaday
Were you able to disable battery optimisation work across all manufactures with the code snippet you shared above? I am running a foreground service, but as @Treatise mentioned I found that not all manufactures (Especially Samsung and Xiaomi) don't respect the same. And they also seem to have different settings for disabling battery optimisation. Any clue on how to make it work across all manufactures?Scrummage
I never found an universal solution for the battery optimization problem. But in my case just running the foreground service solved the problem.Canaday

© 2022 - 2024 — McMap. All rights reserved.