title: Intermittent "RECEIVE_BOOT_COMPLETED" failure
body: <p>I am trying to create an (android 4.3 and later) app  that starts when the phone boots up.  Using other posts I found on StackOverflow I was able to manage this a few times, but it seems that it is not reliably starting every time I reboot the phone.</p>
  
  <p>My code uses an "ongoing" Notification to prove the app is being started by the OS, but it is not happening every time.  I have rebooted several times and it only seems to get started automatically if the app had been running BEFORE the reboot happened.</p>
  
  <p>I really need this app to be started 100% of the time when the phone boots (regardless of if the app was previously running or not).</p>
  
  <p>How can I make this is always guaranteed to run at system start up?  I can't find any documentation that indicates that the app needs to be running before the phone rebooted in order for this to work.</p>
  
  <p>The method I'm using now is just the standard BroadcastReceiver.onReceive responding to the "RECEIVE_BOOT_COMPLETED" signal from the OS.  It fires off the notification and then starts an activity.  When it does work it can take up to 2 minutes to start (which is totally fine).</p>
  
  <p>Can anyone suggest how I can fix this issue without installing any other 3rd party apps?</p>
  
  <p>Here's the entire project...</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="uk.co.xxxxxxxxxxx.autostart"&gt;
  
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
  
          &lt;receiver android:name="uk.co.xxxxxxxxxxx.autostart.StartMyServiceAtBootReceiver"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
          &lt;activity android:name=".MainActivity"
              android:enabled="true"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>    package uk.co.xxxxxxxxxxxxx.autostart;
  
  import android.app.Notification;
  import android.app.NotificationManager;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  
  public class StartMyServiceAtBootReceiver extends BroadcastReceiver
  {
      @Override
      public void onReceive(Context context, Intent intent) {
          if (
                  Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())
             )
          {
              NotificationManager nm
                      = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  
              Notification n = new Notification.Builder(context)
                      .setContentText("In BroadcastReceiver!")
                      .setSmallIcon(R.mipmap.ic_launcher)
                      .setContentTitle("AutoStart")
                      .setOngoing(true)
                      .setDefaults(0)
                      .build();
  
              nm.notify(1, n);
  
              Intent i = new Intent(context, MainActivity.class);
  
              i.putExtra("STARTER","BroadcastReceiver");
              i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(i);
          }
      }
  }
  </code></pre>
  
  <p>and...</p>
  
  <pre><code>package uk.co.xxxxxxxxxxx.autostart;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.widget.TextView;
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Intent i = getIntent();
          String starter = i.getStringExtra("STARTER");
  
          if(starter != null) {
              TextView tv = (TextView) findViewById(R.id.textView);
              tv.setText("Started by " + starter);
          }
      }
  }
  </code></pre>