1

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

2 Answers2

0
const val TEST_TIME = 1647915300L //2022-03-22 10:15:00

Is specified as seconds.

You should change this to 1647915300000L

See

    println(java.util.Date(1647915300L)) //Tue Jan 20 01:45:15 UTC 1970
Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69
  • Hello! thank you for trying to answer. It should be at millisecond see this as reference public void setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) from: https://developer.android.com/reference/android/app/AlarmManager#public-methods – Jan Kennu Paz Mar 22 '22 at 09:08
  • Just to add. that scheduling of alarm works if i will not restart the wear os device. it will trigger the alarm. – Jan Kennu Paz Mar 22 '22 at 09:10
  • OK, but that 1647915300L time looks wrong. Are you sure about it? – Yuri Schimke Mar 22 '22 at 11:54
  • Hi yes I am sure, like I said it fires the alarm on any given time as long as i dont restart the device. here is the converter I use https://timestamp.online/ . i just put static time temporarily for this post so everything is much simpler to check. – Jan Kennu Paz Mar 22 '22 at 12:05
  • The page you linked to even points this out. `long ts = System.currentTimeMillis()/1000;` – Yuri Schimke Mar 22 '22 at 20:47
  • But if you aren't using a static value then it makes sense, it's just your post that has the wrong value. – Yuri Schimke Mar 22 '22 at 20:48
  • yes i am not i only use System.currentMillis() + 60000. to schedule an alarm once the device is rebooted. I have adjusted my post according to your suggestion to make it in millis. but regardless if the my value was in seconds. it should fire on reboot since the time (the one in seconds which is 1970 in year) i was using has passed already. so regardless. it should have worked. – Jan Kennu Paz Mar 22 '22 at 22:47
0

Using this link as a reference, the solution using accessibility works.

BUT, this is not advisable as it accessibility is for disabled users. So for now the real solution I can think of is communicate with your WearOS device manufacturer and ask for whitelisting. Other than that we will need to continue resorting to tricks listed in the link I have shared.