Search code examples
androidalarmmanagerandroid-alarms

detect default alarm clock application alarms


I would like to know if there is a way (probably a system broadcast) to know that the alarm clock default application is now start ringing.

if not - I'll be satisfied also if I could get progrematically list of all active alarms been set by the user(which I could extract from each alarm the exact time it would ring..)

what I tried so far:

I know there is a way to get formatted string to next alarm:

  String nextAlarm = Settings.System.getString(context.getContentResolver(), Settings.System.NEXT_ALARM_FORMATTED);

this method returns for certain devices (such all Samsung Galaxy Series..) an empty string, even if the alarm was set (by Samsung native alarm clock app..) . I bet it works only on nexus devices with the default alarms app.

I would like to get a generic solution that would work either way.

TIA

UPDATE

I will try to make my question clearer:

I'm not interested (directly) to know all PendingIntent been held by the AlarmManager

I'm interested only to know about alarms set implicitly by the user, especially about the ones he activated for waking up.

my final goal is to get hint that the user waking up. that's it..


Solution

  • I highly doubt that you'll find a solution for this.

    Alarms are maintained by AlarmManagerService. Since it is not included in the SDK, reflection might be the only way to get something out of it. But from the looks of it, even reflection cannot help you here:

    • Need access to AlarmManagerService $ Batch # alarms <--- ArrayList< Alarm >
    • Need access to AlarmManagerService # mAlarmBatches <--- ArrayList< Batch >
    • Use reflection:
      • Class< ? > ams = Class.forName("com.android.server.AlarmManagerService")
      • Field mAlarmBatches = ams.getDeclaredField("mAlarmBatches")
      • Object listOfBatches = mAlarmBatches.get(????)
      • Stuck

    Seems like a dead end to me. You cannot instantiate AlarmManagerService - not even by accessing and invoking its constructor (because it calls a native method).

    Another line of reasoning: Here's a look at AlarmManagerService$Alarm class:

    public static class Alarm {
        public int type;
        public int count;
        public long when;
        public long windowLength;
        public long whenElapsed;    // 'when' in the elapsed time base
        public long maxWhen;        // also in the elapsed time base
        public long repeatInterval;
        public PendingIntent operation;  <<<<<<============Problem==============
        public WorkSource workSource;
    
        ....
        ....
    }
    

    If one can gain access to the PendingIntent, what's stopping them from cancelling alarms at will - alarms that have been set by other apps?

    Still, I hope someone here can help you out.

    Link: AlarmManagerService