13

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?

Sumit
  • 928
  • 2
  • 15
  • 34

5 Answers5

25

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 ,,,

Mohamed Hussien
  • 1,084
  • 13
  • 14
3

Problem here is that this broadcast has flag Intent.FLAG_RECEIVER_REGISTERED_ONLY set. This means that manifest receivers will not catch that. Here is a full explanation.

Community
  • 1
  • 1
Marek R
  • 32,568
  • 6
  • 55
  • 140
2

Answer to my question was this.. Thanks for replying each and everyone

class NoisyAudioStreamReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
        if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
           // pause();
            Log.d("Mesage","Unplug");
            Toast.makeText(context, "Heello",Toast.LENGTH_LONG).show();
        }
    }
}
 IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
        NoisyAudioStreamReceiver receiver = new NoisyAudioStreamReceiver();
        registerReceiver( receiver, intentFilter );

Sumit
  • 928
  • 2
  • 15
  • 34
0

I recommend you to toggle breakpoint inside onReceive and check in Debug mode what kind of event are you receiving when you plugin your handset device. And then insert this event instead of Intent.ACTION_HEADSET_PLUG Thanks.

Roman
  • 13
  • 3
0

I suppose there is a problem with the registering of broadcast receiver with the respective intent. Refer to this post and see if it works out.

Exception in my logcat

07-30 12:41:01.448: E/ActivityThread(321): Activity com.example.test.MainActivity has leaked IntentReceiver com.example.test.BroadcastsHandler@44ee81d8 that was originally registered here. Are you missing a call to unregisterReceiver()?
07-30 12:41:01.448: E/ActivityThread(321): android.app.IntentReceiverLeaked: Activity com.example.test.MainActivity has leaked IntentReceiver com.example.test.BroadcastsHandler@44ee81d8 that was originally registered here. Are you missing a call to unregisterReceiver()?
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.<init>(ActivityThread.java:939)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:734)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:791)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:778)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:772)
07-30 12:41:01.448: E/ActivityThread(321):  at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
07-30 12:41:01.448: E/ActivityThread(321):  at com.example.test.MainActivity.onCreate(MainActivity.java:19)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 12:41:01.448: E/ActivityThread(321):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 12:41:01.448: E/ActivityThread(321):  at android.os.Looper.loop(Looper.java:123)
07-30 12:41:01.448: E/ActivityThread(321):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 12:41:01.448: E/ActivityThread(321):  at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:41:01.448: E/ActivityThread(321):  at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:41:01.448: E/ActivityThread(321):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 12:41:01.448: E/ActivityThread(321):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 12:41:01.448: E/ActivityThread(321):  at dalvik.system.NativeStart.main(Native Method)

This is how I registered a broadcast receiver in one of my projects:

onCreate:

$////////////////broadcast reciever/////////////
    IntentFilter iFilter = new IntentFilter();
    iFilter.addAction(handler.ACTION);
    iFilter.addCategory(Intent.CATEGORY_DEFAULT);
    handler = new HandleTime();
    registerReceiver(handler, iFilter);

Manifest.xml:

<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>
Community
  • 1
  • 1
faizanjehangir
  • 2,771
  • 6
  • 45
  • 83