Android Question [Solved] - App crashes when passing "isIgnoringBatteryOptimizations" to JavaObjectOpt.RunMethod with not found error

rleiman

Well-Known Member
Licensed User
Longtime User
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.

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)
 

OliverA

Expert
Licensed User
Longtime User
try
B4X:
   Dim p As Phone
    If p.SdkVersion >= 23 Then
        Dim jo As JavaObject
        jo.InitializeStatic("android.content.Context")
        Dim jo2 As JavaObject
        jo2.InitializeContext
        jo2 = jo2.RunMethodJO("getSystemService", Array(jo.GetField("POWER_SERVICE")))
        Dim batteryIgnore As Boolean = jo2.RunMethod("isIgnoringBatteryOptimizations", Array(Application.PackageName))
        Log ("Battery optimization ignored: " & batteryIgnore) ' should be TRUE if app is excluded from optimization
    End If
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
try
B4X:
   Dim p As Phone
    If p.SdkVersion >= 23 Then
        Dim jo As JavaObject
        jo.InitializeStatic("android.content.Context")
        Dim jo2 As JavaObject
        jo2.InitializeContext
        jo2 = jo2.RunMethodJO("getSystemService", Array(jo.GetField("POWER_SERVICE")))
        Dim batteryIgnore As Boolean = jo2.RunMethod("isIgnoringBatteryOptimizations", Array(Application.PackageName))
        Log ("Battery optimization ignored: " & batteryIgnore) ' should be TRUE if app is excluded from optimization
    End If
Thanks for coming to the rescue! ;) It works perfectly.

Under curiosity, why did the coding I used previously not work? I got the previous code from this thread: https://www.b4x.com/android/forum/t...ems-with-android-9-and-10.115982/#post-725041
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top