BroadcastReceiver 不适用于 Android 12

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

在我的 Xamarin Forms 项目中,我有:

[BroadcastReceiver(Permission = "RECEIVE_BOOT_COMPLETED",
    Exported = true,
    Enabled = true)]
[IntentFilter(new[] {Intent.ActionBootCompleted})]
public class GeofenceReceiver: BroadcastReceiver

我将它用于 GeofenceTransitionEnter 和 GeofenceTransitionExit 事件。 我还拥有 ACCESS_FINE_LOCATION 和 ACCESS_BACKGROUND_LOCATION 权限。

但是 OnReceive 方法在 API 31 上没有被调用。我在较低的 API 上没有这个问题。

Android 12 targetSDKVersion 31 挑战(广播接收器、待定意图)崩溃问题 - 对我不起作用

xamarin.forms broadcastreceiver android-12
2个回答
0
投票

我做了一个样品来测试。而且效果很好,

OnReceive
方法将在设备重新启动后几分钟调用。

接收者:

[BroadcastReceiver(Permission = "RECEIVE_BOOT_COMPLETED",
    Exported = true,
    Enabled = true)]
    [IntentFilter(new[] { Intent.ActionBootCompleted })]
    public class GeofenceReceiver : BroadcastReceiver
    {

        public override void OnReceive(Context context, Intent intent)
        {
            //launch our activity
            if (intent.Action == "android.intent.action.BOOT_COMPLETED")
            {
                Toast.MakeText(context,"Device Restart",ToastLength.Long).Show();
                Intent intent1 = new Intent(context,typeof(ForegroundServiceDemo));
                intent1.SetFlags(ActivityFlags.NewTask);
                context.StartForegroundService(intent1);
            }
        }
    }

前台服务:

[Service]
    public class ForegroundServiceDemo : Service
    {
        private string NOTIFICATION_CHANNEL_ID = "1000";
        private int NOTIFICATION_ID = 1;
        private string NOTIFICATION_CHANNEL_NAME = "notification";

        private void startForegroundService()
        {
            var notifcationManager = GetSystemService(Context.NotificationService) as NotificationManager;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                createNotificationChannel(notifcationManager);
            }

            var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
            notification.SetAutoCancel(false);
            notification.SetOngoing(true);
            notification.SetSmallIcon(Resource.Mipmap.icon);
            notification.SetContentTitle("ForegroundService");
            notification.SetContentText("Foreground Service is running");
            StartForeground(NOTIFICATION_ID, notification.Build());
        }

        private void createNotificationChannel(NotificationManager notificationMnaManager)
        {
            var channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME,
            NotificationImportance.Low);
            notificationMnaManager.CreateNotificationChannel(channel);
        }

        public override IBinder OnBind(Intent intent)
        {
            return null;
        }

        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            startForegroundService();
            return StartCommandResult.NotSticky;
        }
    }

AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <uses-permission android:name="android.permission.START_FOREGROUND_SERVICES_FROM_BACKGROUND" />
      <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

我将

RECEIVE_BOOT_COMPLETED
添加到AndroidManifes.xml中,因为如果我只是将其添加到
[BroadcastReceiver]
中,接收器将无法工作。所以你也可以尝试在AndroidManifes.xml中添加它。

此外,您可以创建一个新项目来测试我的代码。当我测试它时,它可以在 Android 12.0 模拟器上运行。最后,如果还是不行,能否请您展示一下

OnReceive
方法中的代码?也许调用了
OnReceive
方法,但是其中的代码无法运行后台,例如启动 Activity。


0
投票

我刚刚在 GepfencePendingIntent 中添加了这样的内容:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)
    } else {
        PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    }
      

我在这里找到它:https://www.flybuy.com/android-12-pendingintent-mutability-and-geofences

© www.soinside.com 2019 - 2024. All rights reserved.