I am trying to reboot my device and relaunch my alarm following this https://developer.android.com/training/scheduling/alarms#boot.
But unfortunately only the BOOT_COMPLETED action is received. Other intents from Alarm is not received.
During on BOOT_COMPLETED . I schedule my task for the alarm once again.
I also have confirmed using the command below
adb shell dumpsys alarm
That I still have the alarm after rebooting. but the when the alarm fires Broadcast receiver does not receive it.
Does anyone have an idea what I might missing? Thank you.
NOTE: If I use the same code without restarting the device. The alarm will be received by the Broadcast receiver. Issue only happens if I restart.
Here is the code for the Playground App im testing it on.
MainActivity
class MainActivity : Activity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val receiver = ComponentName(applicationContext, BootReceiver::class.java)
applicationContext.packageManager.setComponentEnabledSetting(
receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
val prefs = getSharedPreferences("TEST_PREF", MODE_PRIVATE)
binding.text.text = "Hello!! " + prefs.getString("message", "No message defined") //"No name defined" is the default value.
Log.d("TESTER", "app loaded")
}
}
BroadcastReceiver
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if(context == null) return
if(intent == null) return
if (intent.action == "android.intent.action.BOOT_COMPLETED") {
// Set the alarm here.
ScheduleManager.scheduleAlarm(context, TEST_TIME)
Log.d("TESTER", "android.intent.action.BOOT_COMPLETED")
} else {
val message = intent.getStringExtra(MESSAGE_KEY)
val editor: SharedPreferences.Editor = context.applicationContext.getSharedPreferences("TEST_PREF", MODE_PRIVATE).edit()
editor.putString("message", message)
editor.apply()
Log.d("TESTER", "message from alarm received")
}
}
}
Alarm Manager
object ScheduleManager {
const val MESSAGE_KEY = "message"
const val TEST_TIME = 1647915300000L //2022-03-22 10:15:00
fun scheduleAlarm(context: Context, time: Long) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(context, BootReceiver::class.java)
intent.putExtra(MESSAGE_KEY, "This is from the alarm $time")
val pendingIntent = PendingIntent.getBroadcast(
context,
1111,
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
time,
pendingIntent
)
}
}
Log Results
Launched the app at 2022-03-22 10:11:17.414
2022-03-22 10:11:17.414 4360-4360/com.wearosplayground D/TESTER: app loaded
proceeds rebooting WearOs device
Boot complete action received at 2022-03-22 10:13:49.586
2022-03-22 10:13:49.586 3576-3576/com.ausom.wearosplayground D/TESTER: android.intent.action.BOOT_COMPLETED
I have set the alarm at this time to be at 2022-03-22 10:15:00
but nothing happens