我在Android上的省电模式遇到了严重问题。启用省电模式时,我的应用程序崩溃。该问题在Samsung Galaxy手机(或任何带有AOSP或LineageOS的手机/仿真器)上显现出来,但在魅族或小米上却从未发生过(我猜是由于自己的电池优化;下面的代码始终返回false,不会)是否在这些手机上打开省电模式无关。
第一次测试:已禁用省电模式,应用程序运行正常。
第二项测试:启用了省电模式,应用未列入白名单,应用在启动时崩溃。
第三次测试:省电模式已禁用,应用已启动并已最小化;当我启用省电模式时,应用程式会要求我将自己加入白名单;即使我将应用添加到白名单,应用仍然可能崩溃。
第4次测试:启用了省电模式,应用已列入白名单;应用程序只能部分运行,崩溃或随机冻结。
我了解打ze模式,并在AndroidManifest.xml中启用了REQUEST_IGNORE_BATTERY_OPTIMIZATIONS权限。这是我的代码,用于检查是否启用了省电模式以及app是否在白名单中。但是我的应用程序在到达此代码之前崩溃了(我所描述的一个问题除外)。
PowerWorker.cs
[assembly: Dependency(typeof(BSWApp.Droid.PowerWorker))]
namespace MyApp.Droid
{
class PowerWorker : IPowerWorker
{
public bool IsInPowerSavingMode()
{
PowerManager powerManager = (PowerManager)Application.Context.GetSystemService(Context.PowerService);
bool isPowerSaveMode = powerManager.IsPowerSaveMode;
return isPowerSaveMode;
}
public bool IsInWhiteList(string packageName)
{
PowerManager powerManager = (PowerManager)Application.Context.GetSystemService(Context.PowerService);
bool isInWhiteList = powerManager.IsIgnoringBatteryOptimizations(packageName);
return isInWhiteList;
}
public void RequestWhiteList(string packageName)
{
Application.Context.StartActivity(new Intent(Provider.Settings.ActionRequestIgnoreBatteryOptimizations, Net.Uri.Parse("package:"+packageName)));
}
}
}
崩溃时刻的Logcat:
01-31 02:31:58.015 1511 1511 E AndroidRuntime: FATAL EXCEPTION: main
01-31 02:31:58.015 1511 1511 E AndroidRuntime: Process: [hidden], PID: 1511
01-31 02:31:58.015 1511 1511 E AndroidRuntime: android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at MyApp.Handlers.MessageHandler+<DisplayConfirmation>d__3.MoveNext () [0x00014] in <bf9ac05a02a44989a618d26fad48d12d>:0
01-31 02:31:58.015 1511 1511 E AndroidRuntime: --- End of stack trace from previous location where exception was thrown ---
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at MyApp.ViewModels.PowerWorkerViewModel+<LaunchPowerSettings>d__1.MoveNext () [0x0006e] in <bf9ac05a02a44989a618d26fad48d12d>:0
01-31 02:31:58.015 1511 1511 E AndroidRuntime: --- End of stack trace from previous location where exception was thrown ---
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in <fe08c003e91342eb83df1ca48302ddbb>:0
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <ad2f15102b3a4d36b40e9b0cbc11c376>:0
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <ad2f15102b3a4d36b40e9b0cbc11c376>:0
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in <ad2f15102b3a4d36b40e9b0cbc11c376>:0
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at (wrapper dynamic-method) System.Object.17(intptr,intptr)
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at mono.java.lang.RunnableImplementor.n_run(Native Method)
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:751)
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at android.os.Looper.loop(Looper.java:154)
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6077)
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
01-31 02:31:58.015 1511 1511 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
01-31 02:31:58.016 534 546 W ActivityManager: Force finishing activity [hidden]/md5be088b8fe6dd8127a24806968ac5b197.MainActivity
无论我尝试在哪里运行(我从App.cs(共享代码)和MainActivity.cs(Android本地代码)尝试运行),我都仍然得到相同的结果。我希望该应用程序会要求用户将应用程序添加到白名单中,但实际上即使用户这样做了也没关系,因为即使将白名单应用程序也可能崩溃。所以我完全不知道该怎么办。