3

In one of the exemptions of the "cannot start foreground service from background" restriction, the doc mentions:

Your app invokes an exact alarm to complete an action that the user requests.

Does this mean that the usage scenario below can work?

  1. Use AlarmManager.setAlarmClock to schedule an exact alarm to trigger at time A. The alarm carries a pendingIntent that targets a registered broadcast receiver.

  2. Time A hits, the receiver gets the intent.

  3. In the receiver OnCreate method, we attempt to startForegroundServicewhich involves displaying a sticky notification and playing custom music with MediaPlayer.

thankyoussd
  • 1,875
  • 1
  • 18
  • 39

1 Answers1

1

I have implemented and tested this and it appears to be working, so I assume this is a valid use case.

thankyoussd
  • 1,875
  • 1
  • 18
  • 39
  • 1
    Could you share an example? I keep getting the crash even when I am doing the same as you asked in the question. – theimpulson Jun 27 '22 at 10:54
  • 2
    @theimpulson I can't paste all the codes here (and I'm using Xamarin). But some things to check: 1) You have declared the `SCHEDULE_EXACT_ALARM` and `FOREGROUND_SERVICE` permissions in manifest 2). You have registered your alarm trigger broadcast receiver 3) You have registered your foreground service 4) In your foreground service's `onStartCommand`, you must call `startForeground` asap to display a notification before performing other tasks. If you still have issues, you can paste your exact errors/exceptions and your codes here and maybe we can take a look. – thankyoussd Jun 27 '22 at 16:45