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.
But on a Huawey P9 (API level 23) it crashes:
Any idea ?
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 ?