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?