Xamarin表单应用程序在 Boot 时自动启动后崩溃

eh57zj3b  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(190)

我创建了一个xamarin表单应用程序.我添加了一个广播接收器,以自动启动启动应用程序 Boot .我重新启动手机和应用程序自动启动正确,但约2分钟后应用程序崩溃.请注意,应用程序只崩溃的情况下自动启动启动启动.
试验装置:三星Galaxy S8(Android 9)和一些Android电视设备
已经尝试过的事情。
1.前台服务
1.尝试从电池优化中排除应用程序,但没有任何效果。
AppManifest

<uses-sdk android:targetSdkVersion="29" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.DEVICE_POWER" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-feature android:name="android.hardware.touchscreen" android:required="false" />
    <uses-feature android:name="android.software.leanback" android:required="false" />
    <application android:label="ABC" android:icon="@drawable/icon" android:banner="@drawable/banner">
        <receiver android:name=".MyCustomReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>
    </application>

[BroadcastReceiver(Enabled = true, DirectBootAware = true, Exported = true)]
    [IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)]
    public class MyCustomReceiver : BroadcastReceiver
    {
        

        public override void OnReceive(Context context, Intent intent)
        {
            
            if(Intent.ActionBootCompleted.Equals(intent.Action))
            {
                Intent actIntent = new Intent(context, typeof(MainActivity));
                actIntent.AddFlags(ActivityFlags.NewTask);
                context.StartActivity(actIntent);
            }
        }
    }

崩溃跟踪:

dalvik.system.BaseDexClassLoader.findClass
BaseDexClassLoader.java, line 134
java.lang.ClassNotFoundException: Didn't find class " com.abc.app.MyCustomReceiver" on path: DexPathList[[zip file "/data/app/ com.abc.app-l5EOpz_TN-hTJakX5ZMe5Q==/base.apk"],nativeLibraryDirectories=[/data/app/ com.abc.app-l5EOpz_TN-hTJakX5ZMe5Q==/lib/arm64, /data/app/ com.abc.app-l5EOpz_TN-hTJakX5ZMe5Q==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]

dalvik.system.BaseDexClassLoader.findClass BaseDexClassLoader.java:134
java.lang.ClassLoader.loadClass ClassLoader.java:379
java.lang.ClassLoader.loadClass ClassLoader.java:312
android.app.AppComponentFactory.instantiateReceiver AppComponentFactory.java:84
android.app.ActivityThread.handleReceiver ActivityThread.java:3561
android.app.ActivityThread.access$1300 ActivityThread.java:237
android.app.ActivityThread$H.handleMessage ActivityThread.java:1796
android.os.Handler.dispatchMessage Handler.java:106
android.os.Looper.loop Looper.java:214
android.app.ActivityThread.main ActivityThread.java:7045
java.lang.reflect.Method.invoke Method.java
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run RuntimeInit.java:493
com.android.internal.os.ZygoteInit.main ZygoteInit.java:964

android.app.ActivityThread.handleReceiver ActivityThread.java:3568
android.app.ActivityThread.access$1300 ActivityThread.java:237
android.app.ActivityThread$H.handleMessage ActivityThread.java:1796
android.os.Handler.dispatchMessage Handler.java:106
android.os.Looper.loop Looper.java:214
android.app.ActivityThread.main ActivityThread.java:7045
java.lang.reflect.Method.invoke Method.java
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run RuntimeInit.java:493
com.android.internal.os.ZygoteInit.main ZygoteInit.java:964

--------------------------------

java.lang.RuntimeException: Unable to instantiate receiver  com.abc.app.MyCustomReceiver: java.lang.ClassNotFoundException: Didn't find class " com.abc.app.MyCustomReceiver" on path: DexPathList[[zip file "/data/app/ com.abc.app-l5EOpz_TN-hTJakX5ZMe5Q==/base.apk"],nativeLibraryDirectories=[/data/app/ com.abc.app-l5EOpz_TN-hTJakX5ZMe5Q==/lib/arm64, /data/app/ com.abc.app-l5EOpz_TN-hTJakX5ZMe5Q==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
android.app.ActivityThread.handleReceiver ActivityThread.java:3568
android.app.ActivityThread.access$1300 ActivityThread.java:237
android.app.ActivityThread$H.handleMessage ActivityThread.java:1796
android.os.Handler.dispatchMessage Handler.java:106
android.os.Looper.loop Looper.java:214
android.app.ActivityThread.main ActivityThread.java:7045
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run RuntimeInit.java:493
com.android.internal.os.ZygoteInit.main ZygoteInit.java:964
Caused by: java.lang.ClassNotFoundException: Didn't find class " com.abc.app.MyCustomReceiver" on path: DexPathList[[zip file "/data/app/ com.abc.app-l5EOpz_TN-hTJakX5ZMe5Q==/base.apk"],nativeLibraryDirectories=[/data/app/ com.abc.app-l5EOpz_TN-hTJakX5ZMe5Q==/lib/arm64, /data/app/ com.abc.app-l5EOpz_TN-hTJakX5ZMe5Q==/base.apk!/lib/arm64-v8a, /system/lib64, /system/vendor/lib64]]
dalvik.system.BaseDexClassLoader.findClass BaseDexClassLoader.java:134
java.lang.ClassLoader.loadClass ClassLoader.java:379
java.lang.ClassLoader.loadClass ClassLoader.java:312
android.app.AppComponentFactory.instantiateReceiver AppComponentFactory.java:84
android.app.ActivityThread.handleReceiver ActivityThread.java:3561
pnwntuvh

pnwntuvh1#

关于代码中的问题,您没有为BroadcastReceiver添加标记android:exported
针对Android 12及更高版本的应用需要为android:exported指定显式值。
可以参考以下代码:

[BroadcastReceiver(Enabled =true,Exported =true,DirectBootAware =true)] 
[IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)]
public class MyCustomReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Toast.MakeText(context, Resource.String.test,ToastLength.Long).Show();

        if (Intent.ActionBootCompleted.Equals(intent.Action))
        {
            Intent actIntent = new Intent(context, typeof(MainActivity));
            actIntent.AddFlags(ActivityFlags.NewTask);
            context.StartActivity(actIntent);
        }
    }
}

注:

在Android 8.0(API 26及更高版本)、Google placed limitations关于用户不直接与应用交互时应用可以执行的操作。这些限制会影响后台服务和隐式广播接收器(如Android.Content.Intent.ActionBootCompleted)。由于这些限制,您可能会在较新版本的Android上注册Boot Completed广播接收器时遇到困难。如果是这种情况,请注意,这些限制不适用于前台服务,前台服务可以从广播接收器调用。

相关问题