Xamarin Form: Android doesn't received the bro

2019-09-11 19:26发布

Having problem receiving broadcast after reboot?

I have a class receiving the broadcast after reboot like this.

[BroadcastReceiver(Enabled = true, Exported = true, Permission = "RECEIVE_BOOT_COMPLETED")]
[IntentFilter(new string[] { Android.Content.Intent.ActionBootCompleted })]
public class StartupBootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        var startupIntent = new Intent(Application.Context, typeof(StartupService));
        Application.Context.StartService(startupIntent);
    }
}

The permission in the manifest has set with Boot_Completed

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

When I used the adb command to send the boot broadcast, the receiver didnt' call the receiver.

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED  com.jet.pro

Am I got something missing here?

1条回答
三岁会撩人
2楼-- · 2019-09-11 19:47

If you have force stopped the app, your app will not receive anymore ActionBootCompleted intents until a user runs your app again or the device is rebooted.

This is a malware respawn prevention to allow users and/or Anti-malware services to disable and uninstall the app without chasing a never ending chain of process starts.

Thus if you are debugging and hit "Stop" in the debugger, the app is killed (forced closed).

shell1> adb shell reboot
shell2> adb logcat | grep FOOBAR

~~~~~~~~~~~~~~~~~ I FOOBAR  : ActionBootCompleted

Without starting your app manually after that reboot:

shell1> adb shell am broadcast -a android.intent.action.BOOT_COMPLETED com.sushihangover.notifysound
shell2> adb logcat | grep FOOBAR
Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED pkg=com.sushihangover.notifysound }
Broadcast completed: result=0

~~~~~~~~~~~~~~~~~ I FOOBAR  : ActionBootCompleted

Assuming some log output like this:

[BroadcastReceiver(Name = "com.sushihangover.notifysound.StartUpBootReceiver", Enabled = true, Exported = true, Permission = "RECEIVE_BOOT_COMPLETED")]
[IntentFilter(new string[] { Android.Content.Intent.ActionBootCompleted })]
public class StartupBootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Log.Info("FOOBAR", "ActionBootCompleted");
    }
}

Update: Debug Manifest review:

The only thing that I am manually setting is the ReceiveBootCompleted under the required permissions section. The rest is auto-generated based upon the class attributes:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.sushihangover.notifysound">
  <!--suppress UsesMinSdkAttributes-->
  <uses-sdk android:minSdkVersion="16" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:name="android.app.Application" android:debuggable="true">
    <activity android:icon="@mipmap/icon" android:label="NotifySound" android:name="md548aa2626c31e1cf4d8bbaaddb36911dd.MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <receiver android:enabled="true" android:exported="true" android:name="com.sushihangover.notifysound.StartUpBootReceiver" android:permission="RECEIVE_BOOT_COMPLETED">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </receiver>
    <provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:initOrder="2147483647" android:authorities="com.sushihangover.notifysound.mono.MonoRuntimeProvider.__mono_init__" />
    <!--suppress ExportedReceiver-->
    <receiver android:name="mono.android.Seppuku">
      <intent-filter>
        <action android:name="mono.android.intent.action.SEPPUKU" />
        <category android:name="mono.android.intent.category.SEPPUKU.com.sushihangover.notifysound" />
      </intent-filter>
    </receiver>
  </application>
</manifest>
查看更多
登录 后发表回答