Android Question REQUEST_IGNORE_BATTERY_OPTIMIZATIONS problem

marcick

Well-Known Member
Licensed User
Longtime User
Hi all.
The code below works fine on a lot (tenth) of devices. It shows the settings panel to prompt the user to exclude the app from the Android battery optimization.

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

But on a Huawey P9 (API level 23) it crashes:

B4X:
~i:** Service (starter) Create **
starter service create
~i:** Service (starter) Start **
starter service start
reading settings
Manutenzione completata
~i:** Activity (main) Create, isFirst = true **
1080 x 1740 pixel, 428.625 dpi
~i:** Activity (main) Resume **
~i:isIgnoringBatteryOptimizations FALSE
~e:main_activity_resume (java line: 936)
~e:java.lang.reflect.InvocationTargetException
~e:    at java.lang.reflect.Method.invoke(Native Method)
~e:    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
~e:    at it.elettronicablancato.bmap4aw.main._activity_resume(main.java:936)
~e:    at java.lang.reflect.Method.invoke(Native Method)
~e:    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
~e:    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
~e:    at it.elettronicablancato.bmap4aw.main.afterFirstLayout(main.java:114)
~e:    at it.elettronicablancato.bmap4aw.main.access$000(main.java:23)
~e:    at it.elettronicablancato.bmap4aw.main$WaitForLayout.run(main.java:86)
~e:    at android.os.Handler.handleCallback(Handler.java:743)
~e:    at android.os.Handler.dispatchMessage(Handler.java:95)
~e:    at android.os.Looper.loop(Looper.java:150)
~e:    at android.app.ActivityThread.main(ActivityThread.java:5639)
~e:    at java.lang.reflect.Method.invoke(Native Method)
~e:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
~e:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
~e:Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:it.elettronicablancato.bmap4aw }

Any idea ?
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hello,

I'm trying to run this code but I've already problems when compiling:

B4A version: 6.31
Parsing code. (0.31s)
Compiling code. (0.79s)
Compiling layouts code. (0.08s)
Organizing libraries. (0.00s)
Generating R file. (0.92s)
Compiling generated Java code. Error
B4A line: 554
End Sub
javac 1.8.0_65
src\com\securecommunications\messenger\setup.java:1236: error: cannot find symbol
return pm.isIgnoringBatteryOptimizations(packageName);
^
symbol: method isIgnoringBatteryOptimizations(String)
location: variable pm of type PowerManager

any suggestions?
 
Upvote 0

FabioG

Active Member
Licensed User
Longtime User
any suggestions ?

I copied and pasted this code, starting the function in Activity_Create
nothing happens, I display the MsgBox but pressing OK does not show the dialog to ignore battery optimization
nothing happens, no errors

I have tired Huawey P10 Plus and Nexus 5X
 
Upvote 0

FabioG

Active Member
Licensed User
Longtime User
added
AddPermission(android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
on manifest editor
and it work great

Thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I tried to implement the code however it seems as I dont have the required library for GetAndroidApiLevel.
How do I fix this?
For the future.
- ALWAYS create a new thread for ANY question you have
- NEVER post to existing threads to add a question.

B4X:
    Dim p As Phone
    Log(p.SdkVersion)
 
Upvote 0
I Am strongly recommending to all of you to implement The following Java code. It will allow you to recall simple form of ignoring battery optimisation app, not all apps. Only buttons OK or Storno. Code is The part of Termux terminal emulator for Android. Termuxservice.java module.
String packageName = getPackageName();
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
Intent whitelist = new Intent();
whitelist.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
whitelist.setData(Uri.parse("package:" + packageName));
whitelist.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {
startActivity(whitelist);
} catch (ActivityNotFoundException e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to call ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS", e);
}
}
 
Upvote 0
Top