Search code examples
androidalarmmanager

How to debug an AlarmManager-activated service


I am implementing a widget that checks on-line train departure times between every minute and every hour, depending on the time of day. Calling the service with

 manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime() - 10000, 60000, pendingIntent)

works fine, but for debugging I would like to reduce the interval to about 10 seconds which cannot be done because of the 1-minute limit in more recent versions of Android. Clearly, I don't care about battery life in the emulator.

As far as I understand, using an Hander/Timer is not an option, because it required the task to be in the foreground. Is a visible widget "in the forground"? What is the recommended practice in this case?


Solution

  • you actually have to tasks

    • configure the alarmmanager to add/remove trigger events via intents
    • interprete the events with intents in a service

    If you seperate both you can easily create a very simple gui/activity that does the same as the alarmmanager would do when being triggerd and that you can debug:

    * onSendButtonClick: create and send pendingIntent
    

    for the alarmmanager-handling i would implement logging into a text file each time alarmmanager is added/removed/triggered.

    Be prepared that newer android versions may postpone alarmmanager events to save energy until the device is already active and that intervals less than 15 minutes may not work.

    you may also need on_boot_complete to reconfigure alarmmanager after device-shutdown