“正在重启接收器”不工作 android [Xamarin.Android]

“Rebooting receiver” not working android [Xamarin.Android]

我正在尝试使用以下代码实现一个广播接收器,该接收器在设备重新启动时接收广播,但不工作(它应该在设备重新启动时向我发送祝酒词):

广播接收者:

    [BroadcastReceiver]
    public class RebootReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            if (Intent.ActionBootCompleted.Equals(intent.Action))
            {
                Toast.MakeText(
                    context,
                    "Your app has been rebooted!",
                    ToastLength.Long).Show();
            }
        }
    }

Manifest.xml

<receiver android:name=".RebootReceiver">
        <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
      </receiver>

以及清单中的权限

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

希望得到帮助,谢谢

我认为您的广播接收器有问题,请按照以下步骤查看是否有效:

添加启动权限的清单条目

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

添加接收者并使用完整的限定名称:

<receiver android:name="com.yourpackagename.app.BootBroadcastReceiver">
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED"/>
      </intent-filter>
</receiver>

Name 参数添加到 BroadcastReceiverAttribute 以获取您在清单中使用的完全限定名称

[BroadcastReceiver(Name = "com.yourpackagename.app.BootBroadcastReceiver", Enabled = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]    
public class BootBroadcastReceiver : BroadcastReceiver
 {
    public override void OnReceive(Context context, Intent intent)
    {
        if (Intent.ActionBootCompleted.Equals(intent.Action))
        {
            Toast.MakeText(
                context,
                "Your app has been rebooted!",
                ToastLength.Long).Show();
        }
    }
}

祝你好运,如有疑问,请随时回来

我已经解决了问题,@FreakyAli 的回答实际上也帮助解决了问题

创建服务:

[Service(Name = "com.companyname.app.RebootService")]
    public class RebootService : Service
    {
        public override void OnCreate()
        {
            base.OnCreate();
        }

        public override IBinder OnBind(Intent intent)
        {
            return null;
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            Toast.MakeText(this, "Service STARTED!", ToastLength.Long).Show();
            return StartCommandResult.Sticky;
        }

        public override void OnDestroy()
        {
            base.OnDestroy();

            Toast.MakeText(this, "Service STOPED", ToastLength.Long).Show();
        }
    }
}

创建广播接收者:

 [BroadcastReceiver(Enabled =true, Name ="com.companyname.Sortex.RebootReceiver")]
        [IntentFilter(new[] { Intent.ActionBootCompleted })]
        public class RebootReceiver : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
            }
        }

在 AndroidManifest.xml

上注册服务和 BroadcastReceiver
<service android:name="com.companyname.app.RebootService"/>
<receiver android:name="com.companyname.app.RebootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

在广播接收器的 OnReceive 方法上调用服务:

Intent serviceIntent = new Intent(context, typeof(RebootService));
context.StartService(serviceIntent);