title: BroadcastReceiver - Can't start Activity from closed or in-background app
body: <p>I'm trying to launch an activity from a closed app, using the AlarmManager and a BroadcastReceiver. The "received" debug message is shown even when the app is closed or in background, which means that the AlarmManager is working like I want it to. But the CameraActivity is only launched when the app is active.
  Anyone got an idea why it's not working? Here's (some of) my code:</p>
  
  <p>MainActivity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          ...
  
          Intent intent = new Intent(this, AlarmReceiver.class);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  
          AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  
          alarmManager.setRepeating(
                  AlarmManager.RTC_WAKEUP,
                  startTimeStamp,
                  intervalTime,
                  pendingIntent);
  
          Log.i("########################DEBUG", "started");
      }
  }
  </code></pre>
  
  <p>AlarmReceiver</p>
  
  <pre><code>public class AlarmReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.i("########################DEBUG", "received");
  
          Intent launchIntent = new Intent(context, CameraActivity.class);
          launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
          context.startActivity(launchIntent);
  
          /* Tried this too, didn't work
          PackageManager packageManager = context.getPackageManager();
          Intent launchIntent = packageManager.getLaunchIntentForPackage(CameraActivity.class.getName());
          context.startActivity(launchIntent);*/
      }
  }
  </code></pre>
  
  <p>CameraActivity</p>
  
  <pre><code>public class CameraActivity extends AppCompatActivity {
  
      private final int CAMERA_REQUEST = 1;
  
      private ImageView image;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_camera);
  
          image = findViewById(R.id.imageview);
  
          Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
          //startActivityForResult(cameraIntent, CAMERA_REQUEST);
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
          if (resultCode == RESULT_OK &amp;&amp; requestCode == CAMERA_REQUEST) {
              Bitmap photo = (Bitmap) data.getExtras().get("data");
              image.setImageBitmap(photo);
          }
      }
  }
  </code></pre>
  
  <p>manifest.xml</p>
  
  <pre><code>    &lt;application
  
          ...
  
          &lt;activity android:name=".CameraActivity"&gt;
          &lt;/activity&gt;
          &lt;activity android:name=".MainActivity"&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;receiver android:name=".AlarmReceiver" /&gt;
      &lt;/application&gt;
  </code></pre>
  
  <p>Thanks in advance!</p>