hi B4X Community
i'm trying to use this code but doesn't work and appears an error in log
and also inline java code
and added a permission in manifest
it's crashes and an error message appears
java.lang.RuntimeException: Method: isIgnoringBatteryOptimizations not found in: android.app.Application
my device is Huawey P20 light
any help please
i'm trying to use this code but doesn't work and appears an error in log
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim JavaObject1 As JavaObject
Private Api As Int
Private r As Reflector
End Sub
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
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
MsgboxAsync("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
End Sub
and also inline java 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
and added a permission in manifest
B4X:
AddPermission(android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
it's crashes and an error message appears
java.lang.RuntimeException: Method: isIgnoringBatteryOptimizations not found in: android.app.Application
my device is Huawey P20 light
any help please