Android Question REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Method: isIgnoringBatteryOptimizations not found

M.LAZ

Active Member
Licensed User
Longtime User
hi B4X Community
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
 

Pendrush

Well-Known Member
Licensed User
Longtime User
B4X:
android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
This is forbidden permission for Google Play Store.
You can use it if you don't have plans to publish your app.
 
Upvote 0

M.LAZ

Active Member
Licensed User
Longtime User
I use this permission in my app because my app doesn't restart at boot. The optimizer exclude it from boot. As you seen in picture i need to manage my app manually not automatically.
 

Attachments

  • Screenshot_20200927_142844_com.huawei.systemmanager.jpg
    Screenshot_20200927_142844_com.huawei.systemmanager.jpg
    323.7 KB · Views: 288
Upvote 0

M.LAZ

Active Member
Licensed User
Longtime User
I solved this error , but my app still doesn't work when device is restarted , my app is an alarm and reminder for prays it's very important to remind users without this my app is nothing.

my device is Huawei P20 L
any help please
 
Upvote 0
Top