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

查看:116
本文介绍了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();
        }
    }

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

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)在BroadcastReceiverAttribute中为您在清单中使用的标准Java类名称添加Name参数

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天全站免登陆