skip to Main Content

I want to make sure AlarmManager is triggered even when my app is manually closed, the same way a messaging app still displayed messages even when closed (swipe or press the "X"). This is my code:

class MainActivity : ComponentActivity() {

    private lateinit var alarmManager: AlarmManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val receiver = object : BroadcastReceiver() {
                override fun onReceive(context: Context, intent: Intent) {

                    val mp = MediaPlayer.create(context,R.raw.music)
                    mp.start()

                }
            }

            this.registerReceiver(receiver, IntentFilter("SET_ALARM"))

            alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager

            var calendar = Calendar.getInstance()
            calendar.add(Calendar.MINUTE,1)
            val alarmIntent = Intent()
            alarmIntent.action = "SET_ALARM"
            var pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0)

            Column( Modifier
                .fillMaxWidth(),
                horizontalAlignment = Alignment.CenterHorizontally) {


                Button(onClick = { alarmManager.setAndAllowWhileIdle(
                    AlarmManager.RTC_WAKEUP,
                    calendar.timeInMillis,
                    pendingIntent)}) {Text("play song in 1 minute")
                }
                
            }
        }
}

}

It works fine when the app is open, but not when I close manually it . What do I need to add?

(It is not a battery management problem, as it does not work in the emulator either)

2

Answers


  1. In your AndroidManifest.xml, make sure you have a receiver definition within the tags, something like:

    <receiver android:name="AlarmReceiver"

    Login or Signup to reply.
  2. You said in a comment that you’re registering your broadcast receiver through an activity, and not the manifest. That will only receive broadcasts while your app is actually running – specifically as long as the Context you used to register the receiver is valid. If you used an Activity as that context, once that activity is destroyed the receiver won’t get broadcasts (even if the app is running, e.g. with another activity). Even if you use the application context, once the app is destroyed, that’s cleared.

    From the docs:

    Manifest-declared receivers

    If you declare a broadcast receiver in your manifest, the system launches your app (if the app is not already running) when the broadcast is sent.

    Context-registered receivers

    Context-registered receivers receive broadcasts as long as their registering context is valid. For an example, if you register within an Activity context, you receive broadcasts as long as the activity is not destroyed. If you register with the Application context, you receive broadcasts as long as the app is running.

    If you need your app to launch in the background (which you do if you’re relying on alarms, which implies your app isn’t in the foreground and could be destroyed when the alarm runs) you need to register the receiver in your manifest, so the system knows it’s something your app handles

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search