Android Question How to properly handle Battery Optimizations permissions from users?


Well-Known Member
Licensed User
Hi guys,

I want to ask user for ignoring Battery Optimizations. Here is my codes :
Sub RequestPowerMngt As ResumableSub
    Private Api As Int
    Private r As Reflector
    Api = r.GetStaticField("android.os.Build$VERSION", "SDK_INT")
    If Api >= 23 Then
        Dim JavaObject1 As JavaObject
        Dim Ignoring As Boolean=JavaObject1.RunMethod("isIgnoringBatteryOptimizations", Null)
        If Ignoring=False Then
            Dim JavaObject1 As JavaObject
            JavaObject1.RunMethod("ShowPermissionDialog", Null)
        End If
        Log(JavaObject1.RunMethod("isIgnoringBatteryOptimizations", Null))
        Return JavaObject1.RunMethod("isIgnoringBatteryOptimizations", Null)  
        Return True  
    End If
End Sub

Sub Activity_Resume
    Wait For(RequestPowerMngt) Complete(Rsl As Boolean)
    If Rsl = False Then
        Log("No access")
    End If
End Sub

Those codes run OK if user allows it, but when user denied it, the line after Wait for in sub Activity_Resume never executed and app runs in a loop, asked user again for permission.

I wan app to terminate/quit If user denied permission.
Anyone care to give me a hint? Thanks.

Note : the code for ask battery permission, got from here :
Last edited: