Android Question About runtime permission

marcick

Well-Known Member
Licensed User
Just a confirmation if I'm doing correctly:
I have declared in "configure paths" the android-23 platform. This because I want to manage a runtime permission in Android 6 and force the user to exclude the App from the battery optimization

B4X:
Sub Activity_Resume
If GetAndroidApiLevel>=23 Then
        Dim JavaObject1 As JavaObject
        JavaObject1.InitializeContext
        Dim Ignoring As Boolean=JavaObject1.RunMethod("isIgnoringBatteryOptimizations", Null)
        If Ignoring=False Then
            Msgbox("it is mandatory to answer YES to the following box to exclude this App from battery optimization.", "Battery optimization")
            RuntimePermissions1.CheckAndRequest("android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS")
        End If
    End If

I thought first that I had also to declare in the manifest the android:targetSdkVersion="23"
And then I discovered I had to manage in runtime all the other dangerous permission, like write to external storage.

The question is: can I have the android-23 platform in "configure paths" and specify android:targetSdkVersion="22" (not 23) in the manifest ?
This way I can use just the runtimepermission I need, without need to modify much other code.
It seems to work, just need confirmation.
 

Erel

Administrator
Staff member
Licensed User
You are confusing different things. Runtime permissions are only relevant if targetSdkVersion is set to 23+. The android version under Tools - Configure Paths is not important in this case.

The code you posted if not related to the runtime permissions feature.

The answer is that you can set the targetSdkVersion to 22.
 
Upvote 0

marcick

Well-Known Member
Licensed User
I got help for that code (I didn't write it) and there is another part in Java to shows a dialog that enables the user to approve whitelisting the app.
But to understand: why you say it is not related to the runtime permission feature ?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Yes, I have the AddPermission in the manifest
B4X:
AddPermission(android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
but this does not mean my app is whitelisted ...
With the manifest entry and thencode posted before and some other Java stuff, my app can programmatically request the user to whitelist the app and exclude it from the battery optimization.
I think my code is related to the runtime permissions feature, right ?

there are a lot of other dangerous permissions and if I switch to target 23 I need to modify much code to manage them. Would avoid now ....
 
Upvote 0

marcick

Well-Known Member
Licensed User
Ahhh.... understand now.
I have removed
B4X:
RuntimePermissions1.CheckAndRequest("android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS")
And just use the java code to open the dialog and ask the user to whitelist the app.
It works fine.
Thank you very much, as usual.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Ahhh.... understand now.
I have removed
B4X:
RuntimePermissions1.CheckAndRequest("android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS")
And just use the java code to open the dialog and ask the user to whitelist the app.
It works fine.
Thank you very much, as usual.

Hi

can you share you code to do this please

J.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Yes:

B4X:
Sub Activity_Resume
    If GetAndroidApiLevel>=23 Then
        Dim JavaObject1 As JavaObject
        JavaObject1.InitializeContext
        Dim Ignoring As Boolean=JavaObject1.RunMethod("isIgnoringBatteryOptimizations", Null)
        If Ignoring=False Then
            Msgbox("Pleas confirm the following to exclude this app from battery optimization.", "Battery Optimization")
            'RuntimePermissions1.CheckAndRequest("android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS")
            Dim JavaObject1 As JavaObject
            JavaObject1.InitializeContext
            JavaObject1.RunMethod("ShowPermissionDialog", Null)
        End If
    End If
End Sub
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Thanks for sharing,

I get the following:

B4X:
Dim Ignoring As Boolean=JavaObject1.RunMet
java.lang.RuntimeException: Method: isIgnoringBatteryOptimizations not found in: com.islesystems.pushtotalk.main
   at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
   at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
   at com.islesystems.pushtotalk.main._dozemode_off(main.java:1233)
   at com.islesystems.pushtotalk.main._activity_resume(main.java:919)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
   at com.islesystems.pushtotalk.main.afterFirstLayout(main.java:112)
   at com.islesystems.pushtotalk.main.access$000(main.java:21)
   at com.islesystems.pushtotalk.main$WaitForLayout.run(main.java:84)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:168)
   at android.app.ActivityThread.main(ActivityThread.java:5845)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
java.lang.RuntimeException: Method: isIgnoringBatteryOptimizations not found in: com.islesystems.pushtotalk.main

Using a HTC M9 Android 6.0, <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="23"/>

Any Ideas ?

Regards

John.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Sorry, add this code :):

B4X:
#If JAVA
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings;
import android.net.Uri;
import anywheresoftware.b4a.BA;

public boolean isIgnoringBatteryOptimizations(){
Context context=this;
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return pm.isIgnoringBatteryOptimizations(packageName);
}

public void ShowPermissionDialog(){
    Intent intent = new Intent();
    Context context=this;
    String packageName = context.getPackageName();
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName)){
        BA.LogInfo("isIgnoringBatteryOptimizations TRUE");
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    } else {
        BA.LogInfo("isIgnoringBatteryOptimizations FALSE");
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    context.startActivity(intent);
}
#End If
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Sorry, add this code :):

B4X:
#If JAVA
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.provider.Settings;
import android.net.Uri;
import anywheresoftware.b4a.BA;

public boolean isIgnoringBatteryOptimizations(){
Context context=this;
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return pm.isIgnoringBatteryOptimizations(packageName);
}

public void ShowPermissionDialog(){
    Intent intent = new Intent();
    Context context=this;
    String packageName = context.getPackageName();
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName)){
        BA.LogInfo("isIgnoringBatteryOptimizations TRUE");
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    } else {
        BA.LogInfo("isIgnoringBatteryOptimizations FALSE");
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    context.startActivity(intent);
}
#End If


thanks for that, seems to be going around in a loop, with the message box poping up, and the following logged:

isIgnoringBatteryOptimizations FALSE

J.
 
Upvote 0

marcick

Well-Known Member
Licensed User
uhm. That's the code I'm using and works fine but sometimes it's hard to collect all the parts needed ......
Looking to the manifest, I see I have added this also, hope it's the last missing part ....

B4X:
' add new permission to ignore doze_mode in Android 6
AddPermission(android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
 
Upvote 0
Top