Alarm manager not triggered when using WIFI in redmi Note 8?

Viewed 112

I have implemented alarm manager to trigger every 5 minutes in my application. But when using it on my Redmi note 8 device, alarm manager not triggered in wifi enabled mode and at the same time it works for mobile data enabled mode. For other device's it works fine in both wifi and mobile data.

I know that wifi or mobile-data is not related to alarm manager triggering process. But, I'm facing this weird issue. Could anyone help me out ?

This is my alarm triggering code.

private var alarmManager: AlarmManager? = null
private lateinit var pendingIntent: PendingIntent

override fun startAlarm(url: String, status: String) {
    alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val intent = Intent(this, AlarmReceiver::class.java).apply {
        action = Constants.ACTION_ALARM_MANAGER
        putExtra(KeyConstants.STATUS, status)
        putExtra(KeyConstants.URL, url)
    }
    pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    val timeInterval = TimeUnit.MINUTES.toMillis(5)
    alarmManager?.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), timeInterval, pendingIntent)
}

override fun cancelAlarm() {
    alarmManager?.cancel(pendingIntent)
}

class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
    // Is triggered when alarm goes off, i.e. receiving a system broadcast
    if (intent.action == Constants.ACTION_ALARM_MANAGER) {
        val tripStatus = intent.getStringExtra(KeyConstants.STATUS)
        val directionUrl = intent.getStringExtra(KeyConstants.URL)

        // my logic here
    }
}
}

and also added receiver in Manifest.xml

<receiver android:name=".AlarmReceiver" />
1 Answers

Every OEM has made it worse to use Alarm manager, mostly it's their battery saving app which kills the app in the background thus no triggering of alarm events.

Android's system makes sure that no alarm should trigger very often, for saving battery and minimizing wakeups, android shifts these alarms for future for this.

As your alarm goes off every 5 minutes, most probably android is shifting your alarm too.

You can check all the scheduled alarm events of the device using following command adb shell dumpsys alarm

Related