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

incendio

Well-Known Member
Licensed User
Hi guys,

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

Sub Activity_Resume
    Wait For(RequestPowerMngt) Complete(Rsl As Boolean)
    Log(1)
    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:
Top