Android Question [Solved} - Fighting back against Samsung's battery management?

rleiman

Well-Known Member
Licensed User
Longtime User
Greetings,

I'm using an unusual title to this thread because we are having a problem with Samsung S10 devices running Android 11. We are not sure if the problem is with Samsung or Android 11 or a combination of both, but it's causing many of our customers to complain that audio on existing applications that they have stops playing. This affects only app we made for them.

Here's what I know so far from experimenting with my S10+ phone. After compiling any of our apps and running them, the audio will be ok for a few days, then everything went downhill. The audio will start to stutter, then stop completely. This behaviour only started after I updated the phone to Android 11. I went online and they suggested to disable battery management and add the apps to the "apps that don't sleep" list and wipe the cache partition which I did. It was only a temporary fix. A few days later the audio had the same problem. This also did not affect any of the stock Android apps but did affect my WhatsApp app and some others but not all of them. When the apps are not working like this, the audio will occasionally turn on randomly for about 15 seconds then stop.

I can buy some time, by recompiling and running any of our apps and the audio will be good for about 3 days before it starts to go downhill. We searched online for many days but can't find a way online to combat this problem. All we find is other people complaining on blogs and forums about it.

The only way to keep the audio running on any of our apps was to connect the phone to an external power source or just turn on the screen without opening up any of the apps. That's why we came to the conclusion the Samsung battery management is the cause.

This issue is only happing to S10 phones running Android 11.

All of our apps use the following code in a service module that is set to start up if the phones are restarted. Maybe there is some extra coding that needs to be added?

Inside the service module:
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED
    lock.PartialLock
    lock.KeepAlive(False) ' Don't keep the screen on but keep the app alive.
End Sub
 

rleiman

Well-Known Member
Licensed User
Longtime User
Did you try this:
Thanks so much for the coding. I added the coding and will let you know within a few days if turning off one of my apps in the list made a difference because the issue is definitely caused by my battery optimisation setting being turned off and ignored by the phone.

*** Update ***
My sub routine now looks like this.:
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_ALWAYS
    lock.PartialLock
    lock.KeepAlive(False) ' Don't keep the screen on but keep the app alive.
   
'    Dim p As Phone
'    If p.SdkVersion >= 23 Then
'        Dim JavaObjectOpt As JavaObject
'        JavaObjectOpt.InitializeContext
'        Dim batteryIgnore As Boolean = JavaObjectOpt.RunMethod("isIgnoringBatteryOptimizations", Null)
'        Log ("Battery optimization ignored: " & batteryIgnore) ' should be TRUE if app is excluded from optimization
'        If batteryIgnore = False Then
'            Dim msgOptiBatt As Object = _
'                Msgbox2Async("Do you want to exclude app from battery optimization?" & Chr(13) & _
'                Chr(13) & "Choosing 'Yes' will open up a screen where you will need To Select " & _
'                "'All' from the dropdown. That will allow you To Select this app And disable " & _
'                "it from the optimization. It's a good idea to do this for other apps you don't " & _
'                " want Android messing around with As well.", _
'                "Battery Optimization", "Yes", "", "No", Null, False)
'            Wait For (msgOptiBatt) Msgbox_Result (Result As Int)
'            If Result = DialogResponse.POSITIVE Then
'                'if YES, after starting intent you need to select all application in drop down, then you need to find your application and turn on or tick box after application name.
'                Dim StartIntentBatt As Intent
'                StartIntentBatt.Initialize("android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS", "")
'                StartActivity(StartIntentBatt)
'            End If
'        Else
'            MsgboxAsync("This app is already excluded from battery optimization", _
'                "Battery Optimization")
'        End If
'    End If
   
    Dim StartIntentBatt As Intent
    StartIntentBatt.Initialize("android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS", "")
    StartActivity(StartIntentBatt)
End Sub

I also didn't use all of the code because the app crashed stating that isIgnoringBatteryOptimizations couldn't be found so after my testing of the 3 lines with the intent, I will start a new thread asking why isIgnoringBatteryOptimizations is not found.

So far the app sound has not stopped after 1 day of testing. So far so good. The new coding brought to our attention that the app needed to be included in the settings screen that allows the user to exclude the app from the battery optimisation.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Some good news and some bad news. The good news is I'm marking this post as solved because it brought to my attention on being able to prevent any app from being struck down by the new battery optimisation that Android 11 has. Now we can tell the users if their app audio is stopping randomly how to keep it going.

The bad news is that the app crashes when executing this line of code telling me that isIgnoringBatteryOptimizations is not found.

B4X:
Dim batteryIgnore As Boolean = JavaObjectOpt.RunMethod("isIgnoringBatteryOptimizations", Null)

That means I need to post another thread asking why it can't be found and how to install it.
 
Upvote 0
Top