xamarin.android Receiver on BOOT_COMPLETED error

前端 未结 2 1818
日久生厌
日久生厌 2020-12-06 14:25

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 wi

相关标签:
2条回答
  • 2020-12-06 14:41

    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.

    Via Attributes

    1) Remove the receiver and boot permission from your manifest

    2) Add your boot permissions via an attribute)

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

    3) Add the manifest entry via attributes:

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

    Via manifest

    1) Add the manifest entry for the boot permission

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

    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) 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
    
    0 讨论(0)
  • 2020-12-06 14:53

    I found another solution for this problem,

    Please, be sure your BootBroadcastReceiver is present on the Yourproject.Android.csproj file

    <ItemGroup>
        <Compile Include="MainActivity.cs" />
        <Compile Include="Provider\AppVersionProvider.cs" />
        <Compile Include="Resources\Resource.Designer.cs" />
        <Compile Include="Properties\BootBroadcastReceiver.cs" />
        <Compile Include="others_staf" />
    

    And see SushiHangover's answer.

    0 讨论(0)
提交回复
热议问题