Search code examples
androidandroid-8.0-oreojobservice

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


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


Solution

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