【问题标题】:“Rebooting receiver” not working android [Xamarin.Android]“重新启动接收器”无法正常工作 android [Xamarin.Android]
【发布时间】:2020-02-05 13:07:18
【问题描述】:

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

广播接收器:

    [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"/>

希望帮助,谢谢

【问题讨论】:

    标签: android xamarin android-intent xamarin.android toast


    【解决方案1】:

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

    为启动权限添加清单条目

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

    如果您有任何疑问,请随时回复

    【讨论】:

    • 我已经按照您建立的方式完成了,但我的错误仍然是一样的,我已经一次又一次地重新启动我的设备并且没有向我显示任何内容,否则,现在,当我重新启动时的应用程序我的设备向我弹出一条警报:“AppName 已停止”,有什么建议吗?
    【解决方案2】:

    我已经解决了这个问题,@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 上注册 Service 和 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);
    

    【讨论】:

    • @FreakyAli 需要等待一天才能将我的答案标记为它
    • 我知道我的意思是只要你可以!