Greetings,
We are trying to add code to our apps that test if battery optimization is being ignored so we can ask the user to disable the app from Android's new battery optimization because that's been causing all of our apps audio to stutter and quit randomly because of the new battery optimization implemented in Android version 11.
We now know that running these 3 lines of code will bring the user to the screen that allows the user to disable the apps from battery optimization.
The issue we are now facing is testing if battery optimization is being ignored. When executing the following highlighted line the logs indicate the following telling us isIgnoringBatteryOptimizations can't be found. Is this the correct coding to make the test to see if battery optimization is being ignored?
We are trying to add code to our apps that test if battery optimization is being ignored so we can ask the user to disable the app from Android's new battery optimization because that's been causing all of our apps audio to stutter and quit randomly because of the new battery optimization implemented in Android version 11.
We now know that running these 3 lines of code will bring the user to the screen that allows the user to disable the apps from battery optimization.
B4X:
Dim StartIntentBatt As Intent
StartIntentBatt.Initialize("android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS", "")
StartActivity(StartIntentBatt)
The issue we are now facing is testing if battery optimization is being ignored. When executing the following highlighted line the logs indicate the following telling us isIgnoringBatteryOptimizations can't be found. Is this the correct coding to make the test to see if battery optimization is being ignored?
Line 5 of this coding crashes the app.:
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
End If
B4X:
servicemodule_service_create (java line: 552)
java.lang.RuntimeException: Method: isIgnoringBatteryOptimizations not found in: b4a.example.servicemodule
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:363)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:120)
at b4a.example.servicemodule._service_create(servicemodule.java:552)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at b4a.example.servicemodule$2.run(servicemodule.java:79)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)