0

I am working on an app which when the alarm clock is triggered, it will do something in the background. for now its just a toast. When running i don't get any error but when the alarm sounds the broadcast receiver doesn't get called. What am I missing?

AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver {
        public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
        @Override
        public void onReceive(Context context, Intent intent) {
            {
                Intent alarm = new Intent(ALARM_ALERT_ACTION);
               // context.registerReceiver(this, alarm);
                String action = intent.getAction();
                if (action.equals(ALARM_ALERT_ACTION))
                {
                    // for play/pause mediaplayer
                    CharSequence text = "Hello toast!";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                }
            }
        }
    }

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.noel.alarmnotification">
    <uses-permission android:name="com.android.alarm.permission."/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <receiver android:name=".AlarmReceiver" android:process=":remote">
                <intent-filter>
                    <action android:name="com.android.deskclock.ALARM_ALERT"/>
                </intent-filter>
            </receiver>
        </activity>
    </application>

</manifest>

EDIT

MainActivity:

public class MainActivity extends AppCompatActivity {
    public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
    public static final String ALARM_SNOOZE_ACTION = "com.android.deskclock.ALARM_SNOOZE";
    public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS";
    public static final String ALARM_DONE_ACTION = "com.android.deskclock.ALARM_DONE";


    private BroadcastReceiver mReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            String action = intent.getAction();
            if (action.equals(ALARM_ALERT_ACTION) || action.equals(ALARM_DISMISS_ACTION) || action.equals(ALARM_SNOOZE_ACTION) || action.equals(ALARM_DONE_ACTION))
            {
                // for play/pause mediaplayer

            }
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter(ALARM_ALERT_ACTION);
        filter.addAction(ALARM_DISMISS_ACTION);
        filter.addAction(ALARM_SNOOZE_ACTION);
        filter.addAction(ALARM_DONE_ACTION);
        registerReceiver(mReceiver, filter);
    }
}

When i create a register programatically, once the app is closed, it doesn't broadcast the alarms

  • what you mean with ***when the alarm sounds the broadcast receiver doesn't get called*** ? You hear the alarm but the methods inside the receiver are not executed? – Opiatefuchs Apr 28 '16 at 13:31
  • yes that's what happens, I debug it but the program never reaches the breakpoint inside the broadcastreceiver – programernoob Apr 28 '16 at 13:39
  • I am not really sure about that, but I guess you need the permission ``. And if you are acting on marshmallow, you need the permission request at runtime too. – Opiatefuchs Apr 28 '16 at 13:44
  • I just tried, without success. Thanks anyway. If you find out any other error let me know – programernoob Apr 28 '16 at 13:50
  • I can´t find this ALARM_ALERT in the API, I know there are a some action and intent types that only can get worked programmatically. Maybe that´s the problem. Try to create a receiver programmatically and register with `registerReceiver(yourReceiver)` – Opiatefuchs Apr 28 '16 at 13:55
  • will this work when the app is not open? I want it woring in the background of my phone – programernoob May 02 '16 at 12:32
  • yes usually it should work.... – Opiatefuchs May 02 '16 at 12:34
  • When i do the EDIT in my question once the app is closed, the process is killed so it will never broadcast – programernoob May 02 '16 at 12:53
  • what´s the error stacktrace? Please post the logcat.. – Opiatefuchs May 03 '16 at 12:25
  • The error is that the app closes. Activity has leaked IntentReceiver that was originally registered here. Are you missing a call to unregisterReceiver()? But if I unregister it, once the app is closed it won't broadcast any alarm – programernoob May 18 '16 at 09:09
  • @programernoob did you find the answer? it seems that in sdk >= 21 receiver for alarm clock doesn't work. Microsoft's lock screen app uses "monitoring usage" persmission to somehow handle it. – pkleczko Aug 11 '17 at 07:18

1 Answers1

1

The following threads might be helpful for you -

Android: open activity when alarm clock is done or dismmised

and

Listing of manufacturer's clock / alarm package and class name, Please add

Community
  • 1
  • 1
Supratim Haldar
  • 2,376
  • 3
  • 16
  • 26