Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver for unplug of headphone

Tags:

android

Hi i am working on an application that generate an event when ever the headphone is removed from the mobile phone. I have created a broadcast receiver with receive method as

public void onReceive(Context context, Intent intent) {

        // TODO Auto-generated method stub
        String action = intent.getAction();
        Log.i("Broadcast Receiver", "Hello");
        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
            }
        }           

    }

Calling this method as follows

 IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        HeadSetBroadCastReceiver receiver = new HeadSetBroadCastReceiver();
        registerReceiver( receiver, receiverFilter );

also i have register this in manifest as

   <receiver android:name=".HeadsetBroadCastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.ACTION_HEADSET_PLUG"/>
    </intent-filter>
</receiver>

and permission

But this doesnot works can anyone guide me through this?

like image 566
Sumit Avatar asked Jul 30 '12 06:07

Sumit


People also ask

How do I turn off BroadcastReceiver on Android?

To stop receiving broadcasts, call unregisterReceiver(android. content. BroadcastReceiver) . Be sure to unregister the receiver when you no longer need it or the context is no longer valid.

What does broadcast receiver do?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.


1 Answers

it's tricky point but you can use BroadCast as the Following working well with me in your Activity

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

and in onResume() Method register your Broadcast

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

then Declare your BroadCast in your Activity

private class HeadSetReceiver 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 unplugged");
                break;
            case 1:
                Log.d(TAG, "Headset plugged");
                break;
            }
        }
    }
}

Hope it's Help ,,,

like image 143
Mohamed Hussien Avatar answered Sep 30 '22 00:09

Mohamed Hussien