Add Headset button support to your Android application

It’s relatively easy to add headset button support to your application. For example you want to play/pause media playback in your super media player.

But here are a few moments that you should take into consideration:

    1. You should register your broadcast receiver inside your application (not in manifest file). Otherwise Google Music player will catch your broadcast and aboard it.;a=blob;f=src/com/android/music/
    2. Your IntentFilter priority should be higher that other media players priorities in your phone:) That’s kind of tricky thing.

      The code snippet will look like:

      MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
      IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
      registerReceiver(mMediaButtonReceiver, mediaFilter);


      public class MediaButtonIntentReceiver extends BroadcastReceiver {
          public MediaButtonIntentReceiver() {
          public void onReceive(Context context, Intent intent) {
              String intentAction = intent.getAction();
              if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
              KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
              if (event == null) {
              int action = event.getAction();
              if (action == KeyEvent.ACTION_DOWN) {
      	    // do something
      1. Don’t forget to unregister your broadcast receiver
      2. Add “android.permission.BLUETOOTH” permission if you want to support bluetooth headset

      // DL

      How to create calendar event with specific repetition rule

      Just a quick note how to set specific repetition rule when creating new calendar event. I haven’t found any documentation about this and had to inspect platform code.

      Intent intent = new Intent(Intent.ACTION_EDIT);
      intent.putExtra("beginTime", date);
      intent.putExtra("allDay", true);
      intent.putExtra("rrule", "FREQ=YEARLY");
      intent.putExtra("endTime", date);
      intent.putExtra("title", summary);

      date (long) is time in ms
      allDay means “all day event”
      rrule is the repetition rule in specific format (RFC 2445). In my example “FREQ=YEARLY” means yearly repetition.


      © 2020 Android Tales

      Theme by Anders NorenUp ↑