Android Question isIgnoringBatteryOptimizations in B4XPages

marcick

Well-Known Member
Licensed User
Longtime User
Hi all,
this code works fine in B4A but give a compile error in B4XPages

B4X:
   If GetAndroidApiLevel>=23 Then
        Dim JavaObject1 As JavaObject
        JavaObject1.InitializeContext
        Dim Ignoring As Boolean=JavaObject1.RunMethod("isIgnoringBatteryOptimizations", Null)
        If Ignoring=False Then
            Msgbox("Please confirm the following form to exclude the 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


#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

The error is

B4X:
Compiling generated Java code.    Error
src\it\..\b4xmainpage.java:769: error: incompatible types: b4xmainpage cannot be converted to Context
Context context=this;
                ^
1 error

javac 11.0.1
 

teddybear

Well-Known Member
Licensed User
A simple solution is to move JavaObject1.InitializeContext and inline java to Main module, and define public JavaObject1 in Process_Globals.
In B4XPages module you only do this as below
B4X:
    Dim jo As JavaObject
    jo=Main.JavaObject1
        Dim Ignoring As Boolean=jo.RunMethod("isIgnoringBatteryOptimizations", Null)
        If Ignoring=False Then
            Msgbox("Please confirm the following form to exclude the app from battery optimization.", "Battery optimization")
            jo.RunMethod("ShowPermissionDialog", Null)
        End If
 
Last edited:
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
A simple solution is to move JavaObject1.InitializeContext and inline java to Main module, and define public JavaObject1 in Process_Globals.
In B4XPages module you only do this as below
B4X:
    Dim jo As JavaObject
    jo=Main.JavaObject1
        Dim Ignoring As Boolean=jo.RunMethod("isIgnoringBatteryOptimizations", Null)
        If Ignoring=False Then
            Msgbox("Please confirm the following form to exclude the app from battery optimization.", "Battery optimization")
            jo.RunMethod("ShowPermissionDialog", Null)
        End If

Almost working ..... just this error:

B4X:
Error occurred on line: 166 (B4XMainPage)
java.lang.RuntimeException: Object should first be initialized (JavaObject).

Line 166 = Dim Ignoring As Boolean=jo.RunMethod("isIgnoringBatteryOptimizations", Null)
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Do you JavaObject1.InitializeContext in Activity_Create?
I've attached the sample, it seems to work
 

Attachments

  • isignoringbattery.zip
    9.6 KB · Views: 217
Upvote 0
Top