Home > Software design >  Launch Activity on AlarmManager event from broadcast receiver
Launch Activity on AlarmManager event from broadcast receiver

Time:10-10

I have am alarm manager through which i set a time to trigger on it by providing a broadcast receiver, onReceive method of broadcast receiver calls successfully but any idea how to launch app from onReceive if app is not running in foreground?

Menifest

<receiver
    android:exported="true"
    android:name=".AlarmReceiver" />

BroadcastReceiver

  @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent scheduledIntent = new Intent(context, MainActivity.class);
        scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(scheduledIntent);
    }

MainActivity

        AlarmManager alarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent=new Intent(this,AlarmReceiver.class);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(this,1000,intent,0);
        alarmManager.set(AlarmManager.RTC_WAKEUP,timeInMillis,pendingIntent);

Kindly help Thanks

CodePudding user response:

First, you should use workManager Also, Brodcastreciver has limits on different android versions doc

CodePudding user response:

It is basically a radio music stream using exoplayer , what we need is user set a time on which he want it to play , and when that time comes stream start playing automatically .

A background service will only run for one minute on Android 8.0 . For a music player, you will want a foreground service, where the associated Notification gives the user playback controls (e.g., pause, resume, stop).

  • Related