title: How can I make C# Xamarin.Android app run at phone start-up?
body: <p>I have been trying to get an app to run when phone starts, and eventually run app when I press <code>KeyCode.CameraButton</code>. I am using a class called <code>BootReceiver</code>, inherited from <code>BroadcastReceiver</code>. Here is my class:</p>
  <pre class="lang-cs prettyprint-override"><code>namespace ColorPoint.Xamarin.XAndroid
  {
      [BroadcastReceiver]
      [IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority)]
      public class BootReceiver : BroadcastReceiver
      { 
          public override void OnReceive(Context context, Intent intent)
          {
              Intent serviceStart = new Intent(context, typeof(MainActivity));
              context.StartActivity(serviceStart);                
          }
      }
  }
  </code></pre>
  <p>At the moment the app restarts when broadcast received. I run this from <code>adb</code> command prompt to emulate boot up and phone restarts!</p>
  <pre class="lang-sh prettyprint-override"><code>adb -s device-or-emulator-id shell am broadcast -a android.intent.action.BOOT_COMPLETED
  </code></pre>
  <p>Here is my manifest file, not sure if its correct at all!</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; package=&quot;ColorPoint.Xamarin.XAndroid&quot; android:installLocation=&quot;auto&quot; android:versionCode=&quot;1&quot; android:versionName=&quot;1.0&quot;&gt;
      &lt;uses-sdk android:minSdkVersion=&quot;19&quot; android:targetSdkVersion=&quot;19&quot; /&gt;
      &lt;application android:label=&quot;Rexson Barcode Scanner&quot; android:icon=&quot;@drawable/icon&quot;&gt;
          &lt;receiver android:name=&quot;.BootReceiver&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.BOOT_COMPLETED&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
      &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.CAMERA&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_INPUT_STATE&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.RECEIVE_BOOT_COMPLETED&quot; /&gt;
      &lt;uses-feature android:name=&quot;android.hardware.camera&quot; /&gt;
      &lt;uses-feature android:name=&quot;android.hardware.camera.autofocus&quot; /&gt;
      &lt;activity android:name=&quot;MainActivity&quot; /&gt;
      &lt;activity android:name=&quot;com.google.zxing.client.android.CaptureActivity&quot; android:exported=&quot;false&quot; android:windowSoftInputMode=&quot;stateAlwaysHidden&quot;&gt;
          &lt;intent-filter&gt;
              &lt;action android:name=&quot;com.phonegap.plugins.barcodescanner.SCAN&quot; /&gt;
              &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/manifest&gt;
  </code></pre>
  <p>Need to go through this step by step but can't debug it very well, just so weird how phone resets when I emulate boot up!</p>
  <p>I just tried a manual reboot and app tried to start but said <code>unfortunately [myapp] has stopped working</code> then phone reboots again.</p>
  <p>Seems like it tries to open but crashes immediatley, then reboots phone.</p>
  <p>Found this from another example but same thing, just reboots phone..</p>
  <pre class="lang-cs prettyprint-override"><code>public override void OnReceive(Context context, Intent intent)
  {
      if (intent.Action == Intent.ActionBootCompleted)
      {
          bool autoRestart = false;
          var sp = context.GetSharedPreferences(&quot;preferences&quot;, FileCreationMode.Private);
          autoRestart = sp.GetBoolean(&quot;autoRestart&quot;, false);
          if (autoRestart)
          {
              Intent serviceStart = new Intent(context, typeof(MainActivity));
              serviceStart.AddFlags(ActivityFlags.NewTask);
              context.StartActivity(serviceStart);
          }
      }
  }
  </code></pre>