1

Is it possible to create a JobService to be fired when alarmclock goes off?

I used to have this code in the manifest for a Broadcast receiver:

                <action android:name="com.android.deskclock.ALARM_ALERT" />                    
                <action android:name="com.samsung.sec.android.clockpackage.alarm.ALARM_ALERT" />
                <!-- <action android:name="com.samsung.sec.android.clockpackage.alarm.ALARM_DONE" /> -->
                <action android:name="com.htc.android.worldclock.ALARM_ALERT" />
                <action android:name="com.htc.android.ALARM_ALERT" />
                <action android:name="com.sonyericsson.alarm.ALARM_ALERT" />
                <action android:name="com.sonyericsson.organizer.Organizer_WorldClock" />
                <action android:name="com.sonyericsson.organizer.Organizer" />
                <action android:name="com.lge.clock.AlarmClockActivity" />
                <action android:name="com.lge.clock.DefaultAlarmClockActivity" />
                <action android:name="com.lge.alarm.alarmclocknew" />
                <action android:name="zte.com.cn.alarmclock.ALARM_ALERT" />
                <action android:name="com.motorola.blur.alarmclock.ALARM_ALERT" />
                <!-- Third party Alarms -->
                <action android:name="com.mobitobi.android.gentlealarm.ALARM_INFO" /> <!-- Gentle Alarm -->
                <action android:name="com.urbandroid.sleep.alarmclock.ALARM_ALERT" /> <!-- Sleep As Android -->
                <action android:name="com.splunchy.android.alarmclock.ALARM_ALERT" /> <!-- Alarmdroid (1.13.2) -->
                <action android:name="net.havchr.mr2.services.AlarmNoiser" /> <!-- Morning Routine -->
                <action android:name="net.havchr.mr2.broadcastreceivers.AlarmBroadcastReceiver" /> <!-- Morning Routine -->
                <action android:name="net.havchr.mr2.services.FlicButtonTurnOffAlarmService" /> <!-- Morning Routine -->
                <action android:name="net.havchr.mr2.broadcastreceivers.ALARM_ALERT" /> <!-- Morning Routine -->
                <action android:name="net.havchr.mr2.services.ALARM_ALERT" /> <!-- Morning Routine -->
                <action android:name="net.havchr.mr2.ALARM_ALERT" /> <!-- Morning Routine -->
                <action android:name="net.havchr.mr2.ALARM_ALERT_SERVICE" /> <!-- Morning Routine -->
                <action android:name="net.havchr.mr2.ALARM_NOTIFICATION_SERVICE" /> <!-- Morning Routine -->
                <action android:name="net.havchr.mr2.FLIC_LISTENER_SERVICE" /> <!-- Morning Routine -->
                <action android:name="REFRESH_THEM_VIEWS" /> <!-- Morning Routine -->
                <action android:name="com.vp.alarmClockPlusDock" /> <!-- Alarm Clock Plus -->

But it is not working any more in Android 8 Oreo. Is there a way to handle some task when the alarm goes off? For example, when the user wakes up in the morning I want to display a "Good morning message".

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Ton
  • 9,235
  • 15
  • 59
  • 103

2 Answers2

1

To solve this I needed to create a bit complicated workaround. But it works. Let me explain what I did.

I first create a NotificationListenerService to read every single notification that is displayed in the device. Of course, you need to ask for the permission with:

startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));

Then, in the onNotificationPosted() function I do this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    if (sbn.getPackageName().equalsIgnoreCase("com.google.android.deskclock")) {
        //The Android standard clock app is displaying a notification. We still need to check that the notification is because the alarm is going off right now, and not because the user has set an alarm at a specific time and the system displays de clock icon: 
        oNotification = sbn.getNotification();
        String s1 = "" + oNotification.extras.getCharSequence(Notification.EXTRA_TITLE);
        if(s1.equalsIgnoreCase("Alarm") || s1.equalsIgnoreCase("Alarma") || s1.equalsIgnoreCase("Alarme") || s1.equalsIgnoreCase("Sveglia") || s1.equalsIgnoreCase("Будильник") || s1.equalsIgnoreCase("Alarmă") || s1.equalsIgnoreCase("المنبه") || s1.equalsIgnoreCase("Wecker") || s1.equalsIgnoreCase("Ébresztő") || s1.equalsIgnoreCase("Wekker") || s1.equalsIgnoreCase("Будильник") || s1.equalsIgnoreCase("闹钟") || s1.equalsIgnoreCase("鬧鐘") ) {  //En varios idiomas
            //Alarm is going off right now!!!!
        }
    }
}
  1. I check if the posted notification belongs to the standard Android alarm Clock app (com.google.android.deskclock).

  2. Then I need to distinguish from just a regular notification displaying that the alarm is set for a specific time from a real alarm going off notification.

  3. After watching several examples, I found that when the alarm goes off, it has the word "Alarm" in the Notification.EXTRA_TITLE field. You need to pay attention, because in other cases this field could be null or have the text "Pending alarm". But, when it goes off it is just "Alarm".

  4. I also changed the language of the device to the major languages (English, Spanish, Chinese, French, German, Russian...) and see that it can change to Alarma, Alarme, Wecker, Ébresztő, المنبه, Будильник...

It is a bit "analog" but since Android keeps building walls for us, we try to jump over them. I cannot understand why Android limits us so much. It would be beautiful to have a permission to receive the alarm going off events. Even if it is a "dangerous" permission.

Ton
  • 9,235
  • 15
  • 59
  • 103
0

Is it possible to create a JobService to be fired when alarmclock goes off?

No, sorry.

But it is not working any more in Android 8 Oreo.

You are handling alarm broadcasts from ~20 alarm clock apps, which is a small percentage of the alarm clock apps in use. There is no requirement for a third-party alarm clock app to tell other apps when an alarm goes off.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Correct, but those broadcast worked for most Android devices, using the standard Clock app. How can I do it with Oreo? – Ton Apr 02 '18 at 23:44
  • @Ton: "but those broadcast worked for most Android devices" -- there are ~10,000 Android device models. I am guessing that you tested a very small percentage of them. "using the standard Clock app" -- across those ~10,000 Android device models, I would expect there to be dozens, if not hundreds, of "standard Clock apps". That is the sort of thing that many device manufacturers customize. "How can I do it with Oreo?" -- most likely, you don't. You can have a foreground service register for those same broadcasts dynamically, but your users may or may not appreciate the foreground service. – CommonsWare Apr 02 '18 at 23:46
  • It is not that high number. The standar Alarm Clock app in Android sent the "com.android.deskclock.ALARM_ALERT" broadcast and it worked in most of my 50,000 users. I tried also registering that broadcast inside a foreground service but my app is not getting fired when the alarm goes off. Maybe, now with Oreo, the message is different. I don't know. – Ton Apr 02 '18 at 23:51