detect default alarm clock application alarms
Asked Answered
S

5

10

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

Sloatman answered 4/2, 2014 at 12:56 Comment(0)
B
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

Benjie answered 16/2, 2014 at 12:4 Comment(2)
thanks for your answer, you probably right.. for now I've upvoted your answer but I'm not excepting it yet because maybe someone still can tell us something new we didn't excpected possibleSloatman
@TalKanel Thanks for the up-vote. I agree that we should wait for a more positive answer.Benjie
Y
5

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

Yerga answered 13/3, 2014 at 6:53 Comment(2)
thanks for your answer, I've upvoted it for the effort, but - as you wrote yourself - this strategy would require different handling for each phone vendor (at minumum), and I'm looking for official android solution that would be generic. seems like there's no such..Sloatman
thanks I highly doubt a universal solution can be found with . The DB uri query method will work for most samsung devices (JBP) since A CP is exposed by the app im sure it is done so that other application can use this info. Also the same URI based DB query worked on S3 and Note 3 . Just check for similar default package name of nexus/htc devices , it might just work :)Yerga
K
2

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.

Kaitlinkaitlyn answered 22/2, 2014 at 3:29 Comment(0)
B
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

Benjie answered 16/2, 2014 at 12:4 Comment(2)
thanks for your answer, you probably right.. for now I've upvoted your answer but I'm not excepting it yet because maybe someone still can tell us something new we didn't excpected possibleSloatman
@TalKanel Thanks for the up-vote. I agree that we should wait for a more positive answer.Benjie
V
1

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.

Verse answered 21/2, 2014 at 10:37 Comment(0)
D
1

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:

enter image description here

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:

enter image description here

This might be of interest as well

Deliver answered 22/2, 2014 at 14:49 Comment(3)
thanks for your answer. I up voted it , but honestly I'm not sure that sniffing continuously the logcat messages is such a good idea. my process would be alive all the time, and consume a lot of resources, what eventually would drain battery fast.. even if I'll ignore that issue - there are still some problems: com.android.desklock is not exists on phones that are not with vanilla android firmewares, so it won't work for that..Sloatman
Your question has invoked curiosity, May be we could use a broadcast somehow would be better than a handler to sniff, I will look for more details about it and post it here if I find any worthy or relevance. Thanks :)Deliver
You can't sniff the logcat for other apps anymore.Votary

© 2022 - 2024 — McMap. All rights reserved.