1

This is my receiver file

package com.example.tony.headsetplug;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MusicIntentReceiver extends BroadcastReceiver {

    @Override public void onReceive(Context context, Intent intent) {
        Log.i("Tag","got receiver");
    }
}

I am trying to implement a static broadcast for plugging in the headset, my manifest file where the static broadcast is defined.

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver
        android:name=".MusicIntentReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.headset_plug" />
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>





</application>

It's not working, not even showing a broadcast received.

Tony Tom
  • 73
  • 12

3 Answers3

1

The value of ACTION_HEADSET_PLUG is "android.intent.action.HEADSET_PLUG" (note upper case letters in it). Check the documentation as well.

p.s.: in addition, you'll have to register the receiver programmatically (check for instance this post), e.g.:

IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
HeadsetStateReceiver receiver = new HeadsetStateReceiver();
registerReceiver( receiver, receiverFilter );
Community
  • 1
  • 1
Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • Thank you, when it's registered in the main activity or any activity it works while the activity is running. I still want the broadcast to be received, even when the activity is closed. Is that possible? Or for this, should I run a service in background ? – Tony Tom Jul 02 '15 at 21:53
  • 1
    Yes, use a service in this case (start it _STICKY_, see http://developer.android.com/reference/android/app/Service.html#START_STICKY). What I forgot to mention: use `unregisterReceiver(...)` in `onDestroy()` because registering a BroadcastReceiver twice causes an exception. **p.s.**: using a LocalBroadcastManager can have some advantages (see http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html); example here: http://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/ – Trinimon Jul 03 '15 at 08:30
  • 1
    Thank you, this has been very helpful. – Tony Tom Jul 04 '15 at 06:49
  • I'd be happy if you could up-vote my question - or accept it, if you think it was the most useful answer ;) – Trinimon Jul 04 '15 at 19:05
1

From the official API documentation on AudioManager.ACTION_HEADSET_PLUG:

Broadcast Action: Wired Headset plugged in or unplugged. You cannot receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().

The intent will have the following extra values:

  • state - 0 for unplugged, 1 for plugged.
  • name - Headset type, human readable string
  • microphone - 1 if headset has a microphone, 0 otherwise

This should explain, why it's not working properly when defined as static. I think since music applications usally rely on this broadcast, it makes no sense to receieve such a broadcast when the user is not actually listening to music or doing anything audio related.

Edit:

What if you want to launch your Music app when headphones are plugged, but your activity is not running?

Consider using a background service, which doens't do much except registering the receiver which handles the ACTION_HEADSET_PLUG. In order to start your servic in case the device is (re-)booted, you can add a static receiver listening to ACTION_BOOT_COMPLETED, which launches the service.

Btw: If you are wondering whether you can start your service as soon as your app gets installed, you can't. At least according to this post.

Hope this helps.

Community
  • 1
  • 1
Chris S.
  • 13
  • 4
  • But what if, I want to play music automatically when I plug in my headphones, the music app is not running(or the activity) – Tony Tom Jul 02 '15 at 21:55
-1

You have to register the receiver by code and it is a must:

stackoverflow.com/questions/6249023/detecting-whether-a-headset-is-plugged-into-an-android-device-or-not

If u wanna your listener to be on always, you should listen for system boot intent and register in that listener.

eduyayo
  • 2,020
  • 2
  • 15
  • 35