2

I am using the manifest file for creating broadcast receiver, for ACTION_HEADSET_PLUG. But I can't get the broadcast when headset is connect/disconnect, Which permission should I use inside the manifest file in order to be able to receive ACTION_HEADSET_PLUG broadcast intent?

onkar
  • 4,427
  • 10
  • 52
  • 89
BonosF
  • 111
  • 1
  • 2
  • 4

2 Answers2

4

With API 8, I got my broadcast receiver called without creating a service or requesting for extra permissions.

You can define an inner class within your main activity similar to the one I've defined below:

public class HeadSetBroadCastReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent) {

            // TODO Auto-generated method stub
            String action = intent.getAction();
            Log.i("Broadcast Receiver", action);
            if( (action.compareTo(Intent.ACTION_HEADSET_PLUG))  == 0)   //if the action match a headset one
            {
                int headSetState = intent.getIntExtra("state", 0);      //get the headset state property
                int hasMicrophone = intent.getIntExtra("microphone", 0);//get the headset microphone property
                if( (headSetState == 0) && (hasMicrophone == 0))        //headset was unplugged & has no microphone
                {
                               //do whatever
                }
            }           

        }

    }

Then, register your broadcast receiver either dynamically or statically. I registered mine dynamically in my Activity's onCreate() method:

this.registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));

Make sure that you unregister your BroadcastReceiver with the Context's unregisterReceiver. In my case, I did this in the onDestroy() method. That should do it.

WindsurferOak
  • 4,861
  • 1
  • 31
  • 36
3

It's not a permission thing, it's actually a problem with how you registered the receiver. The headset plug action broadcast can only be received by actively registered receivers like this:

registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));

This means that you need to have a service kept alive that holds a reference to the receiver and also unregisters it when the service is killed. Lastly, the service that registers the receiver needs to also be started on boot; which you do with yet another receiver that intercepts the android.intent.action.BOOT_COMPLETED intent. For this part, you will need to use the android.permission.RECEIVE_BOOT_COMPLETED permission.

For a full example of a service that does this stuff, you can check out an app I wrote that does just that.

jakebasile
  • 8,084
  • 3
  • 28
  • 34
  • 1
    How do you know which actions from android.intent.action.* can be used without having an active service running? Like in this case if I did I would not need a service to detect head phone removal. I looked at the android documentation and cannot tell which services can broadcasts can simply be used by registering in the manifest. – ControlAltDelete Jun 04 '12 at 02:47