title: How to get an AlarmManager to run when the app is closed?
body: <p>I am trying to set up an AlarmManager in my app that opens another app at a specific time. It works when my app is open and on the screen, but if I press the home button and send my app to the background, it does not work. How do I get an AlarmManager to run if my app is closed/in the background?</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      public static final int REQUEST_CODE=101;
      public static int aHour;
      public static int aMinute;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      //Some code that sets aHour and aMinute
  
      public void setAlarm() {
          AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
          Intent intent = new Intent(this, amReceiver.class);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
          Calendar calendar = Calendar.getInstance();
          calendar.setTimeInMillis(System.currentTimeMillis());
          calendar.set(Calendar.HOUR_OF_DAY, aHour);
          calendar.set(Calendar.MINUTE, aMinute);
          if (am != null) {
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                  am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
              }
              else {
                  am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
              }
          }
      }
  
      //Some code that triggers setAlarm()
  
  }
  </code></pre>
  
  <p><strong>amReciever.java</strong></p>
  
  <pre><code>public class amReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          Intent i = new Intent(context, launcherService.class);
          context.startService(i);
      }
  }
  </code></pre>
  
  <p><strong>launcherService.java</strong></p>
  
  <pre><code>public class launcherService extends IntentService {
      public launcherService() {
          super("LauncherService");
      }
       protected void onHandleIntent(Intent intent) {
           Intent launcher = getPackageManager().getLaunchIntentForPackage("com.example.app");
           if (launcher != null) {
               startActivity(launcher);
           }
       }
  }
  </code></pre>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>        &lt;receiver android:name=".amReceiver"
              android:process=":remote"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
          &lt;service android:name=".launcherService" /&gt;
  </code></pre>