Search code examples
androidandroid-activitybroadcastreceiverboot

What is the correct setting of BroadcastReceiver with BOOT_COMPLETED action in Xamarin project?


I created a simple sample to test the BroadcastReceiver reacting to the action BOOT_COMPLETED see. below, but it is not working. After starting the tablet is no activity / app is running and in the logocat is nothing. I'm probably a mistake in the settings, but I can not find out what

I use a tablet alps 874v3 android 4.4.2 and Visual Studio 2010 with Xamarin to write Android app in .net

On SO I found some additional information: 1 Registration BroadcastReceiver have not be inside AndroidManifest.xml but must use class attributes. 2 Applications must contain BroadcastReceiverand activity otherwise the will not run on later versions of Android (for safety) 3 Once installed the application is in stopped state so i started it (system verifies that the user wants the application) and then kill and then I try to reboot.

[BroadcastReceiver(Enabled = true, Exported = true, Permission = "RECEIVE_BOOT_COMPLETED")]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED"})]
public class BootBroadcastReceiver : BroadcastReceiver
{
    public BootBroadcastReceiver()
    {
    }
    public override void OnReceive(Context context, Intent intent)
    {
        Log.Debug("TestBoot", "BootBroadcastReceiver.OnReceive()");
        context.StartActivity(typeof(UsbMainActivity));
        Log.Debug("TestBoot", "BootBroadcastReceiver.OnReceive() after start activity");
    }
}  

[Activity(Label = "UsbMainActivity", Icon = "@drawable/icon", MainLauncher = true, Permission = "RECEIVE_BOOT_COMPLETED")]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" })]
public class UsbMainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        Utils.MyLog("TestBoot", 1, "UsbMainActivity.OnCreate()");
    }
}

There is a AndroidMainfest.xml which was generated by xamarin :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="TestBoot.TestBoot" android:versionCode="1" android:versionName="1.0">
  <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19" />
  <application android:label="TestBoot" android:icon="@drawable/icon" android:name="mono.android.app.Application" android:debuggable="true">
    <activity android:icon="@drawable/icon" android:label="UsbMainActivity" android:permission="RECEIVE_BOOT_COMPLETED" android:name="md5e98891b9b152ca725e5cab653b1387f3.UsbMainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </activity>
    <receiver android:enabled="true" android:exported="true" android:permission="RECEIVE_BOOT_COMPLETED" android:name="md5e98891b9b152ca725e5cab653b1387f3.BootBroadcastReceiver">
      <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="TestBoot.TestBoot.mono.MonoRuntimeProvider.__mono_init__" />
    <receiver android:name="mono.android.Seppuku">
      <intent-filter>
        <action android:name="mono.android.intent.action.SEPPUKU" />
        <category android:name="mono.android.intent.category.SEPPUKU.TestBoot.TestBoot" />
      </intent-filter>
    </receiver>
  </application>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

Solution

  • The problem was that after installing application i stopped via Settings- "Force Quit" and then tried to reboot. If I'm after installing stopped only a activity, so after the restart boot_completed arrived. So it seems that the application must running before the reboot and then the boot_completed arrived.

    Note: Test reboot via adb console, type : adb shell am broadcast -a android.intent.action.BOOT_COMPLETED