xamarin.android 接收器出现 BOOT_COMPLETED 错误 [英] xamarin.android Receiver on BOOT_COMPLETED error

查看:45
本文介绍了xamarin.android 接收器出现 BOOT_COMPLETED 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个从设备启动开始的简单服务.问题是设备返回消息不幸的是,[app_name] 已停止."

I am trying to make a simple service which starts with device boot. Thing is that device return message "Unfortunately, [app_name] has stopped."

我在几个小时内一直在努力解决这个问题,寻找错误,但它太简单了..希望你们能帮助我解决这个问题.

I am struggling with this problem from few hours, with looking for mistake, but it is too simple.. Hope, you guys can help me with this problem.

这是我的代码:

AndroidManifest.xml

AndroidManifest.xml

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

<application android:allowBackup="true" android:label="@string/app_name">


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

  <service android:name=".PService" />
</application>

StartReceiver.cs

StartReceiver.cs

[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class StartReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Intent startIntent = new Intent(context, typeof(PService));
        context.StartService(startIntent);
    }
}

最后是 PService.cs

and lastly PService.cs

[Service]
    public class PService : 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, "Start", ToastLength.Short).Show();
            return StartCommandResult.Sticky;
        }

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

            Toast.MakeText(this, "Stop", ToastLength.Short).Show();
        }
    }

此外,此服务应用程序针对 API 19 (4.4.2 KitKat) Android 版本.

Additional this service application is targetted to API 19 (4.4.2 KitKat) Android version.

我认为我会犯一些非常小的错误,但我确实找不到.在此先感谢您的帮助.

I think there will be really small mistake, made by me but truly I cant find it out.. Thanks in advance for any help.

推荐答案

通过在清单中添加接收器并通过 BroadcastReceiverAttribute 在清单中你有两个接收器.此外,您的清单中的名称将不起作用,因为它不是 Xamarin 默认创建的基于 MD5 的 Java 名称.

By adding the receiver in the manifest and via the BroadcastReceiverAttribute you have two receivers in your manifest. Plus the one in your manifest will not work since it is not the MD5-based Java name that Xamarin creates by default.

1) 从清单中删除接收器和启动权限

1) Remove the receiver and boot permission from your manifest

2) 通过属性添加启动权限)

2) Add your boot permissions via an attribute)

[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]

3) 通过属性添加清单条目:

3) Add the manifest entry via attributes:

[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]    
public class BootBroadcastReceiver : BroadcastReceiver

通过清单

1) 添加启动权限的清单条目

Via manifest

1) Add the manifest entry for the boot permission

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

2) 添加接收器并使用完全限定的 Java 类名:

2) Add the receiver and use a full qualify Java class name:

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

3) 将 Name 参数添加到 BroadcastReceiverAttribute 以获取您在清单中使用的完全限定 Java 类名

3) Add a Name parameter to the BroadcastReceiverAttribute for the fully qualified Java class name that you used in the manifest

[BroadcastReceiver(Name = "com.yourpackagename.app.BootBroadcastReceiver", Enabled = true)]
[IntentFilter(new[] { Intent.ActionBootCompleted })]    
public class BootBroadcastReceiver : BroadcastReceiver

这篇关于xamarin.android 接收器出现 BOOT_COMPLETED 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆