0

我在 Android 上的省电模式有一个严重的问题。启用省电模式后,我的应用程序崩溃。该问题出现在三星 Galaxy 手机(或任何带有 AOSP 或 LineageOS 的手机/模拟器上)上,但在魅族或小米上从未发生过(我猜是由于他们自己的电池优化;下面的代码总是返回 false,不是不管我是否在这些手机上打开了省电模式)。

第一次测试:省电模式禁用,应用程序运行良好。

第二次测试:启用省电模式,应用未列入白名单,应用在启动时崩溃。

第三次测试:禁用省电模式,应用启动并最小化;当我启用省电模式时,应用程序要求我将自己添加到白名单;即使我将应用程序添加到白名单,应用程序仍然可能崩溃。

第四次测试:开启省电模式,应用白名单;应用程序部分工作,随机崩溃或冻结。

我知道打瞌睡模式并在 AndroidManifest.xml 中启用了 REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 权限。这是我的代码,用于检查是否启用了省电模式以及应用程序是否在白名单中。但是我的应用程序在到达此代码之前崩溃(除了我描述的一个问题)。

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 本地代码)),我仍然得到相同的结果。我预计应用会要求用户将应用添加到白名单,但实际上即使用户这样做了也没关系,因为即使被列入白名单,应用仍然可能崩溃。所以我完全不知道该怎么做。

4

0 回答 0