Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android how do you register to receive headset plug broadcasts?

I am working in Android 2.1, and I want to detect when the headset is plugged in/taken out. I'm pretty new to android.

I think the way to do it is using a Broadcast receiver. I sublcassed this, and I also put the following in my AndroidManifest.xml. But do you have to register the receiver somehwere else, like in the activity? I'm aware there are lots of threads on this, but I don't really understand what they're talking about. Also, what's the difference between registering in AndroidManifest.xml versus registering dynamically in your activity?

<receiver android:enabled="true" android:name="AudioJackReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.HEADSET_PLUG" >
            </action>
        </intent-filter>
    </receiver>

And this was the implementation of the class (plus imports)

public class AudioJackReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.w("DEBUG", "headset state received");
}

}

I was just trying to see if it works, but nothing shows up when I unplug/plug in the headset while running the application.

EDIT: the documentation doesn't say this, but is it possible that this one won't work if registered in the manifest? I was able to get it to respond when I registered the receiver in one of my applications (or do you have to do that anyway?)

like image 385
shim Avatar asked Dec 15 '22 17:12

shim


1 Answers

Just complementing Greg`s answer, here is the code that you need divided in two parts

  1. Register the Service in the first Activity (here its called MainActivity.java).

  2. Switch over the result of the ACTION_HEADSET_PLUG action in the BroadCastReceiver.

Here it goes:

public class MainActivity extends Activity  {
private static final String TAG = "MainActivity";
private MusicIntentReceiver myReceiver;

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myReceiver = new MusicIntentReceiver();
}

@Override public void onResume() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(myReceiver, filter);
    super.onResume();
}

private class MusicIntentReceiver extends BroadcastReceiver {
    @Override public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
            case 0:
                Log.d(TAG, "Headset is unplugged");
                break;
            case 1:
                Log.d(TAG, "Headset is plugged");
                break;
            default:
                Log.d(TAG, "I have no idea what the headset state is");
            }
        }
    }
}
like image 86
Renato Lochetti Avatar answered Apr 19 '23 20:04

Renato Lochetti