在我的 Xamarin android 项目中切换到 Android API 34 后,出现错误:应该指定 receive_exported 或 receive_not_exported 之一

问题描述 投票:0回答:1

在我的 Xamarin Android 项目中切换到 Api 34 后,我收到错误“当接收器未专门为系统广播注册时,应指定receiver_exported 或receiver_not_exported 之一”。

Xamarin.Essentials nuget 包:1.8.1

在此输入图片描述

 protected override void OnCreate(Bundle savedInstanceState)
        {
       
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;
     
         
            base.OnCreate(savedInstanceState);
            Instance = this;
            //var options = new FirebaseOptions.Builder()
            //  .SetApplicationId("1:865419492128:android:4d581aa626e7df54093545")
            //  .SetApiKey("AIzaSyDpq3kXLbpzGwrtMsvH3hM3GKvGXQJMhvE")
            //  .SetDatabaseUrl("https://pianodentalclinic-839ce.firebaseio.com")
            //  .SetGcmSenderId("865419492128-vdkjet53af3kurcjsv621g00jgbdk5v5.apps.googleusercontent.com")
            //  .SetStorageBucket("pianodentalclinic-839ce.appspot.com")
            //  .Build();
            Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;
            FirebaseApp.InitializeApp(this);
            Rg.Plugins.Popup.Popup.Init(this);
         
            this.Window.AddFlags(WindowManagerFlags.Fullscreen);
            //ZXing.Net.Mobile.Forms.Android.Platform.Init();
            var config = new FFImageLoading.Config.Configuration()
            {
                VerboseLogging = false,
                VerbosePerformanceLogging = false,
                VerboseMemoryCacheLogging = false,
                VerboseLoadingCancelledLogging = false,
                Logger = new CustomLogger(),
            };
            ImageService.Instance.Initialize(config);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            DependencyService.Register<ToastNotification>();
            //if(Build.VERSION.SdkInt > BuildVersionCodes.Tiramisu)
            //{
                //Context context = Android.App.Application.Context;
               // IntentFilter intent;
                // BroadcastReceiver broadcastReceiver;
                //ContextCompat.RegisterReceiver(context, consoleCmdReceiver, intent, ContextCompat.ReceiverExported);
                //Context.RegisterReceiver(consoleCmdReceiver, IntentFilter, ReceiverExported);
            //}
            //RegisterReceiver(consoleCmdReceiver, new IntentFilter(Intent.ActionRun));
            ToastNotification.Init(this);
            CachedImageRenderer.Init(true);
            CachedImageRenderer.InitImageViewHandler();
 
            LoadApplication(new App());

            //string tkn= CrossFirebasePushNotification.Current.Token; 

            //FirebasePushNotificationManager.ProcessIntent(this, Intent);

        }

解决方案之一是安装 xamarin.essentials 1.8.0-pr.2108.1。我安装了,但是警告说包没有恢复,错误依然存在。

xamarin xamarin.android nuget android-api-34 xamarin-essentials
1个回答
0
投票

您需要使用 AndroidX 才能使用

ReceiverNotExported
(推荐)标志 为了使用 AndroidX 标志,您还需要使用 AndroidX 注册接收器,因此您的需要如下所示:

  var flags = AndroidX.Core.Content.ContextCompat.ReceiverNotExported;
  AndroidX.Core.Content.ContextCompat.RegisterReceiver(this, consoleCmdReceiver, new IntentFilter(Intent.ActionRun), flags);
© www.soinside.com 2019 - 2024. All rights reserved.