“重新启动接收器”无法正常工作 android [Xamarin.Android]

2023-12-31

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

广播接收器:

    [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();
            }
        }
    }

清单.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>

Add a 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();
        }
    }
}

祝您好运,如有疑问请随时回复

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“重新启动接收器”无法正常工作 android [Xamarin.Android] 的相关文章

随机推荐