1

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?

MainActivity.java

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 >= 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()

}

amReciever.java

public class amReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, launcherService.class);
        context.startService(i);
    }
}

launcherService.java

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);
         }
     }
}

AndroidManifest.xml

        <receiver android:name=".amReceiver"
            android:process=":remote">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name=".launcherService" />
CSG01
  • 48
  • 1
  • 4
  • Since you cannot start a service from the background on Android 8.0 and higher, perhaps your alarm is working fine, but your code in response to that alarm is not. – CommonsWare Mar 18 '20 at 23:58
  • @CommonsWare I see. Thanks for the clarification. – CSG01 Mar 19 '20 at 17:23
  • @CommonsWare Do you know how I would get this to work, then? – CSG01 Mar 19 '20 at 17:51
  • On Android 8.0 and higher, you need to use `startForegroundService()` instead of `startService()`, and your service needs to immediately call `startForeground()` with a valid `Notification` to display. – CommonsWare Mar 19 '20 at 17:57
  • @CommonsWare I see. Thank you! – CSG01 Mar 19 '20 at 18:04

0 Answers0