Hi all,
this code works fine in B4A but give a compile error in B4XPages
The error is
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