detect default alarm clock application alarms

2019-04-05 14:34发布

问题:

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..

回答1:

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



回答2:

Hi looks like I am little late to the party but anyways here is what i could dig.

1.) A system broadcast to know that the alarm clock default application is now start ringing.

I checked ADB logs from my Note3 , i could see this log whenever default alarm rings "I/SecExternalDisplayIntents_Java(2797): Intent Recieved .. - com.samsung.sec.android.clockpackage.alarm.ALARM_STARTED_IN_ALERT BroadCast Map value - 7"

I tried catching the intent with action name "com.samsung.sec.android.clockpackage.alarm.ALARM_STARTED_IN_ALERT" successfully. Though I highly doubt if this intent be avialable across all android devices.

The package name of intent action gave me further hints and i found the answer to

2.)I'll be satisfied also if I could get progrematically list of all active alarms been set by the user

seems the clockpackage exposes a Content provider at "com.samsung.sec.android.clockpackage/alarm" location i could query all the alarms set by user from this DB( enabled /disabled/name/snooze details etc ).

By toggling the enable/disable button i figured the value for a the alarm been on/off is in column no 1(columns start from 0 index). Incase you want more data i would suggest pulling the DB and viewing the table structure in SQlite DB browser (device may have to be rooted to pull the entire DB).

Similar DB must exist on other android devices as well (I broke my nexus else could have tested on that as well ) cheers



回答3:

The default clock app - com.android.deskclock - has a ClockProvider that supports queries,
but unfortunately, it's not exported so it cannot be used by third party apps.



回答4:

You can't query AlarmManager for a list of commands which is what you would need to do to achieve this.

The closest you can get to finding a list of alarms would be to use dumpsys adb shell dumpsys alarm, but obviously you can't do that in code.



回答5:

If I understand you right, you want to check which applications use the Alarm Manager class, If that is the case then you can do it by allowing your application to monitor the log cat, once done you will definitely get to know which applications are using alarm. At-least this should work at boot time. You can create a new thread to running the logcat(without the option -d) in the background. Also make sure that you are adding the required permissions in the Manifest for this to work.

Screen-shot of boot time:

Update: LogCat after setting an Alarm for 9:56pm using the default system clock

After coming to know of OP's requirements through an update in the original question, I tried to set an alarm for a specific time and checked LogCat for the same time and the result is logged below with time details and events in chronological order:

This might be of interest as well