fm/fmapp2/src/com/caf/fmradio/FMRadioService.java
public void registerHeadsetListener() {
        if (mHeadsetReceiver == null) {
            mHeadsetReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
                       Log.d(LOGTAG, "ACTION_HEADSET_PLUG Intent received");
                       // Listen for ACTION_HEADSET_PLUG broadcasts.
                       Log.d(LOGTAG, "mReceiver: ACTION_HEADSET_PLUG");
                       Log.d(LOGTAG, "==> intent: " + intent);
                       Log.d(LOGTAG, "    state: " + intent.getIntExtra("state", 0));
                       Log.d(LOGTAG, "    name: " + intent.getStringExtra("name"));
                       mHeadsetPlugged = (intent.getIntExtra("state", 0) == 1);
                       // if headset is plugged out it is required to disable
                       // in minimal duration to avoid race conditions with
                       // audio policy manager switch audio to speaker.
                       mHandler.removeCallbacks(mHeadsetPluginHandler);
                       mHandler.post(mHeadsetPluginHandler);
                    } else if(mA2dpDeviceState.isA2dpStateChange(action) ) {
                        boolean  bA2dpConnected =
                        mA2dpDeviceState.isConnected(intent);
                        if (!bA2dpConnected) {
                            Log.d(LOGTAG, "A2DP device is dis-connected!");
                            mA2dpDisconnected = true;
                        } else {
                            mA2dpDisconnected = false;
                        }
                        if (isAnalogModeEnabled()) {
                            Log.d(LOGTAG, "FM Audio Path is Analog Mode: FM Over BT not allowed");
                            return ;
                        }
                       //when playback is overA2Dp and A2dp disconnected
                       //when playback is not overA2DP and A2DP Connected
                       // In above two cases we need to Stop and Start FM which
                       // will take care of audio routing
                       if( (isFmOn()) &&
                           (true == ((bA2dpConnected)^(mOverA2DP))) &&
                           (false == mStoppedOnFocusLoss) &&
                           (!isSpeakerEnabled())) {
                           stopFM();
                           startFM();
                       }
                    } else if (action.equals("HDMI_CONNECTED")) {
                        //FM should be off when HDMI is connected.
                        fmOff();
                        try
                        {
                            /* Notify the UI/Activity, only if the service is "bound"
                               by an activity and if Callbacks are registered
                             */
                            if((mServiceInUse) && (mCallbacks != null) )
                            {
                                mCallbacks.onDisabled();
                            }
                        } catch (RemoteException e)
                        {
                            e.printStackTrace();
                        }
                    } else if( action.equals(Intent.ACTION_SHUTDOWN)) {
                        mAppShutdown = true;
                    }

                }
            };
            IntentFilter iFilter = new IntentFilter();
            iFilter.addAction(Intent.ACTION_HEADSET_PLUG);
            iFilter.addAction(mA2dpDeviceState.getActionSinkStateChangedString());
            iFilter.addAction("HDMI_CONNECTED");
            iFilter.addAction(Intent.ACTION_SHUTDOWN);
            iFilter.addCategory(Intent.CATEGORY_DEFAULT);
            registerReceiver(mHeadsetReceiver, iFilter);
        }
    }
/**
     * Registers an intent to listen for ACTION_HEADSET_PLUG
     * notifications. This intent is called to know if the headset
     * was plugged in/out
     */
Registers an intent to listen for ACTION_HEADSET_PLUG
notifications. This intent is called to know if the headset
was plugged in/out
Registers an intent to listen for ACTION_HEADSET_PLUG notifications . This intent is called to know if the headset was plugged in / out
public void registerHeadsetListener() {
        if (mHeadsetReceiver == null) {
            mHeadsetReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
                       Log.d(LOGTAG, "ACTION_HEADSET_PLUG Intent received");
                       Log.d(LOGTAG, "mReceiver: ACTION_HEADSET_PLUG");
                       Log.d(LOGTAG, "==> intent: " + intent);
                       Log.d(LOGTAG, "    state: " + intent.getIntExtra("state", 0));
                       Log.d(LOGTAG, "    name: " + intent.getStringExtra("name"));
                       mHeadsetPlugged = (intent.getIntExtra("state", 0) == 1);
                       mHandler.removeCallbacks(mHeadsetPluginHandler);
                       mHandler.post(mHeadsetPluginHandler);
                    } else if(mA2dpDeviceState.isA2dpStateChange(action) ) {
                        boolean  bA2dpConnected =
                        mA2dpDeviceState.isConnected(intent);
                        if (!bA2dpConnected) {
                            Log.d(LOGTAG, "A2DP device is dis-connected!");
                            mA2dpDisconnected = true;
                        } else {
                            mA2dpDisconnected = false;
                        }
                        if (isAnalogModeEnabled()) {
                            Log.d(LOGTAG, "FM Audio Path is Analog Mode: FM Over BT not allowed");
                            return ;
                        }
                       if( (isFmOn()) &&
                           (true == ((bA2dpConnected)^(mOverA2DP))) &&
                           (false == mStoppedOnFocusLoss) &&
                           (!isSpeakerEnabled())) {
                           stopFM();
                           startFM();
                       }
                    } else if (action.equals("HDMI_CONNECTED")) {
                        fmOff();
                        try
                        {
                            /* Notify the UI/Activity, only if the service is "bound"
                               by an activity and if Callbacks are registered
                             */
                            if((mServiceInUse) && (mCallbacks != null) )
                            {
                                mCallbacks.onDisabled();
                            }
                        } catch (RemoteException e)
                        {
                            e.printStackTrace();
                        }
                    } else if( action.equals(Intent.ACTION_SHUTDOWN)) {
                        mAppShutdown = true;
                    }
                }
            };
            IntentFilter iFilter = new IntentFilter();
            iFilter.addAction(Intent.ACTION_HEADSET_PLUG);
            iFilter.addAction(mA2dpDeviceState.getActionSinkStateChangedString());
            iFilter.addAction("HDMI_CONNECTED");
            iFilter.addAction(Intent.ACTION_SHUTDOWN);
            iFilter.addCategory(Intent.CATEGORY_DEFAULT);
            registerReceiver(mHeadsetReceiver, iFilter);
        }
    }
public void registerHeadsetListener ( ) { if ( mHeadsetReceiver == null ) { mHeadsetReceiver = new BroadcastReceiver ( ) { @ Override public void onReceive ( Context context , Intent intent ) { String action = intent . getAction ( ) ; if ( action . equals ( Intent . ACTION_HEADSET_PLUG ) ) { Log . d ( LOGTAG , "ACTION_HEADSET_PLUG Intent received" ) ; Log . d ( LOGTAG , "mReceiver: ACTION_HEADSET_PLUG" ) ; Log . d ( LOGTAG , "==> intent: " + intent ) ; Log . d ( LOGTAG , "    state: " + intent . getIntExtra ( "state" , 0 ) ) ; Log . d ( LOGTAG , "    name: " + intent . getStringExtra ( "name" ) ) ; mHeadsetPlugged = ( intent . getIntExtra ( "state" , 0 ) == 1 ) ; mHandler . removeCallbacks ( mHeadsetPluginHandler ) ; mHandler . post ( mHeadsetPluginHandler ) ; } else if ( mA2dpDeviceState . isA2dpStateChange ( action ) ) { boolean bA2dpConnected = mA2dpDeviceState . isConnected ( intent ) ; if ( ! bA2dpConnected ) { Log . d ( LOGTAG , "A2DP device is dis-connected!" ) ; mA2dpDisconnected = true ; } else { mA2dpDisconnected = false ; } if ( isAnalogModeEnabled ( ) ) { Log . d ( LOGTAG , "FM Audio Path is Analog Mode: FM Over BT not allowed" ) ; return ; } if ( ( isFmOn ( ) ) && ( true == ( ( bA2dpConnected ) ^ ( mOverA2DP ) ) ) && ( false == mStoppedOnFocusLoss ) && ( ! isSpeakerEnabled ( ) ) ) { stopFM ( ) ; startFM ( ) ; } } else if ( action . equals ( "HDMI_CONNECTED" ) ) { fmOff ( ) ; try { /* Notify the UI/Activity, only if the service is "bound"
                               by an activity and if Callbacks are registered
                             */ if ( ( mServiceInUse ) && ( mCallbacks != null ) ) { mCallbacks . onDisabled ( ) ; } } catch ( RemoteException e ) { e . printStackTrace ( ) ; } } else if ( action . equals ( Intent . ACTION_SHUTDOWN ) ) { mAppShutdown = true ; } } } ; IntentFilter iFilter = new IntentFilter ( ) ; iFilter . addAction ( Intent . ACTION_HEADSET_PLUG ) ; iFilter . addAction ( mA2dpDeviceState . getActionSinkStateChangedString ( ) ) ; iFilter . addAction ( "HDMI_CONNECTED" ) ; iFilter . addAction ( Intent . ACTION_SHUTDOWN ) ; iFilter . addCategory ( Intent . CATEGORY_DEFAULT ) ; registerReceiver ( mHeadsetReceiver , iFilter ) ; } }
Registers an intent to listen for ACTION_HEADSET_PLUG
 notifications.
Registers an intent to listen for ACTION_HEADSET_PLUG notifications .
// Listen for ACTION_HEADSET_PLUG broadcasts. // if headset is plugged out it is required to disable // in minimal duration to avoid race conditions with // audio policy manager switch audio to speaker. //when playback is overA2Dp and A2dp disconnected //when playback is not overA2DP and A2DP Connected // In above two cases we need to Stop and Start FM which // will take care of audio routing //FM should be off when HDMI is connected.