Java Code Examples for android.content.Intent#ACTION_HEADSET_PLUG

The following examples show how to use android.content.Intent#ACTION_HEADSET_PLUG . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: FamilyChat   File: HeadSetReceiver.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent)
{
    String action = intent.getAction();
    switch (action)
    {
        //插入和拔出耳机会触发此广播
        case Intent.ACTION_HEADSET_PLUG:
            int state = intent.getIntExtra("state", 0);
            if (state == 1)
            {
                //耳机插入
                if (mListener != null)
                    mListener.onHeadSetStateChanged(true);
            } else if (state == 0)
            {
                //耳机拔出
                if (mListener != null)
                    mListener.onHeadSetStateChanged(false);
            }
            break;
        default:
            break;
    }
}
 
Example 2
private void registerWiredHeadsetStateReceiver() {
  wiredHeadsetStateReceiver = new WiredHeadsetStateReceiver();

  String action;

  if (Build.VERSION.SDK_INT >= 21) {
    action = AudioManager.ACTION_HEADSET_PLUG;
  } else {
    action = Intent.ACTION_HEADSET_PLUG;
  }

  registerReceiver(wiredHeadsetStateReceiver, new IntentFilter(action));
}
 
Example 3
Source Project: PHONK   File: PMedia.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void onHeadsetConnection(ReturnInterface callbackfn) {
    headsetCallbackfn = callbackfn;

    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    headsetPluggedReceiver = new HeadSetReceiver();
    getContext().registerReceiver(headsetPluggedReceiver, filter);
}
 
Example 4
@Override
protected void onResume() {
    super.onResume();

    if (callTYpe == ProtoSignalingOffer.SignalingOffer.Type.VIDEO_CALLING) {
        G.onVideoCallFrame = ActivityCall.this;
        WebRTC.getInstance().startVideoCapture();
    }

    mSensorManager.registerListener(sensorEventListener, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(headsetPluginReciver, filter);
}
 
Example 5
public CustomTwilioVideoView(ThemedReactContext context) {
    super(context);
    this.themedReactContext = context;
    this.eventEmitter = themedReactContext.getJSModule(RCTEventEmitter.class);

    // add lifecycle for onResume and on onPause
    themedReactContext.addLifecycleEventListener(this);

    /*
     * Enable changing the volume using the up/down keys during a conversation
     */
    if (themedReactContext.getCurrentActivity() != null) {
        themedReactContext.getCurrentActivity().setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
    }
    /*
     * Needed for setting/abandoning audio focus during call
     */
    audioManager = (AudioManager) themedReactContext.getSystemService(Context.AUDIO_SERVICE);
    myNoisyAudioStreamReceiver = new BecomingNoisyReceiver();
    intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);

    // Create the local data track
   // localDataTrack = LocalDataTrack.create(this);
   localDataTrack = LocalDataTrack.create(getContext());

   // Start the thread where data messages are received
    dataTrackMessageThread.start();
    dataTrackMessageThreadHandler = new Handler(dataTrackMessageThread.getLooper());

}
 
Example 6
@Override
public void onResume() {
    super.onResume();
    backgroundPlayHelper.hideBackgroundPlaybackNotification();

    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(headSetReceiver, filter);
}
 
Example 7
@Override
public void onResume() {
    super.onResume();
    backgroundPlayHelper.hideBackgroundPlaybackNotification();

    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(headSetReceiver, filter);
}
 
Example 8
Source Project: sealrtc-android   File: AppRTCAudioManager.java    License: MIT License 4 votes vote down vote up
/**
 * Registers receiver for the broadcasted intent when a wired headset is plugged in or
 * unplugged. The received intent will have an extra 'state' value where 0 means unplugged, and
 * 1 means plugged.
 */
private void registerForWiredHeadsetIntentBroadcast() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);

    /** Receiver which handles changes in wired headset availability. */
    wiredHeadsetReceiver =
            new BroadcastReceiver() {
                private static final int STATE_UNPLUGGED = 0;
                private static final int STATE_PLUGGED = 1;
                private static final int HAS_NO_MIC = 0;
                private static final int HAS_MIC = 1;

                @Override
                public void onReceive(Context context, Intent intent) {
                    if (BluetoothUtil.hasBluetoothA2dpConnected()) return;
                    int state = intent.getIntExtra("state", STATE_UNPLUGGED);
                    int microphone = intent.getIntExtra("microphone", HAS_NO_MIC);
                    String name = intent.getStringExtra("name");
                    Log.d(
                            TAG,
                            "BroadcastReceiver.onReceive"
                                    + AppRTCUtils.getThreadInfo()
                                    + ": "
                                    + "a="
                                    + intent.getAction()
                                    + ", s="
                                    + (state == STATE_UNPLUGGED ? "unplugged" : "plugged")
                                    + ", m="
                                    + (microphone == HAS_MIC ? "mic" : "no mic")
                                    + ", n="
                                    + name
                                    + ", sb="
                                    + isInitialStickyBroadcast());

                    boolean hasWiredHeadset = (state == STATE_PLUGGED) ? true : false;
                    switch (state) {
                        case STATE_UNPLUGGED:
                            updateAudioDeviceState(hasWiredHeadset);
                            audioManager.setSpeakerphoneOn(isCurrentSpeakerOn);
                            break;
                        case STATE_PLUGGED:
                            if (selectedAudioDevice != AudioDevice.WIRED_HEADSET) {
                                updateAudioDeviceState(hasWiredHeadset);
                            }
                            break;
                        default:
                            Log.e(TAG, "Invalid state");
                            break;
                    }
                }
            };
    if (apprtcContext != null) {
        apprtcContext.registerReceiver(wiredHeadsetReceiver, filter);
    }
}
 
Example 9
Source Project: IdealMedia   File: PlayerService.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
	super.onCreate();

	// initialize default output device
	if (!BASS.BASS_Init(-1, 44100, 0)) {
		return;
	}

	// look for plugins
	plugins = "";
       String path = getApplicationInfo().nativeLibraryDir;
	String[] list = new File(path).list();
	for (String s: list) {
		int plug = BASS.BASS_PluginLoad(path+"/"+s, 0);
		if (plug != 0) { // plugin loaded...
			plugins += s + "\n"; // add it to the list
		}
	}
	if (plugins.equals(""))
           plugins = "no plugins - visit the BASS webpage to get some\n";
	if(playerInterface != null) {
		playerInterface.onPluginsLoaded(plugins);
	}

       BASS.BASS_SetConfig(BASS.BASS_CONFIG_BUFFER, 1000);
       Log.w("BASS.BASS_CONFIG_BUFFER", "" + BASS.BASS_GetConfig(BASS.BASS_CONFIG_BUFFER));

	// Pending Intend
	Intent intent = new Intent(this, NavigationActivity.class);
	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
	pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

       //tracklist
       loadTracks();
       loadEqualizerValues();

       tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
       tm.listen(telephone, PhoneStateListener.LISTEN_CALL_STATE);

       myBroadcastReceiver = new ServiceBroadcastReceiver();
       IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
       intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
       intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
       registerReceiver(myBroadcastReceiver, intentFilter);

       ComponentName rcvMedia = new ComponentName(getPackageName(), MediaControlReceiver.class.getName());
       mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
       mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
       mAudioManager.registerMediaButtonEventReceiver(rcvMedia);

       // Use the remote control APIs (if available) to set the playback state
       if (Build.VERSION.SDK_INT >= 14 && remoteControlClient == null) {
           registerRemoteControl(rcvMedia);
       }
}
 
Example 10
Source Project: Rey-MusicPlayer   File: MusicService.java    License: Apache License 2.0 4 votes vote down vote up
public void registerHeadsetPlugReceiver() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    mHeadsetPlugReceiver = new HeadsetPlugBroadcastReceiver();
    mService.registerReceiver(mHeadsetPlugReceiver, filter);
}
 
Example 11
Source Project: Status   File: HeadphoneIconData.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public IntentFilter getIntentFilter() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        return new IntentFilter(AudioManager.ACTION_HEADSET_PLUG);
    else return new IntentFilter(Intent.ACTION_HEADSET_PLUG);
}
 
Example 12
Source Project: Yahala-Messenger   File: AppRTCAudioManager.java    License: MIT License 4 votes vote down vote up
/**
 * Registers receiver for the broadcasted intent when a wired headset is
 * plugged in or unplugged. The received intent will have an extra
 * 'state' value where 0 means unplugged, and 1 means plugged.
 */
private void registerForWiredHeadsetIntentBroadcast() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);

    /** Receiver which handles changes in wired headset availability. */
    wiredHeadsetReceiver = new BroadcastReceiver() {
        private static final int STATE_UNPLUGGED = 0;
        private static final int STATE_PLUGGED = 1;
        private static final int HAS_NO_MIC = 0;
        private static final int HAS_MIC = 1;

        @Override
        public void onReceive(Context context, Intent intent) {
            int state = intent.getIntExtra("state", STATE_UNPLUGGED);
            int microphone = intent.getIntExtra("microphone", HAS_NO_MIC);
            String name = intent.getStringExtra("name");
            Log.d(TAG, "BroadcastReceiver.onReceive" + AppRTCUtils.getThreadInfo()
                    + ": "
                    + "a=" + intent.getAction()
                    + ", s=" + (state == STATE_UNPLUGGED ? "unplugged" : "plugged")
                    + ", m=" + (microphone == HAS_MIC ? "mic" : "no mic")
                    + ", n=" + name
                    + ", sb=" + isInitialStickyBroadcast());

            boolean hasWiredHeadset = (state == STATE_PLUGGED) ? true : false;
            switch (state) {
                case STATE_UNPLUGGED:
                    updateAudioDeviceState(hasWiredHeadset);
                    break;
                case STATE_PLUGGED:
                    if (selectedAudioDevice != AudioDevice.WIRED_HEADSET) {
                        updateAudioDeviceState(hasWiredHeadset);
                    }
                    break;
                default:
                    Log.e(TAG, "Invalid state");
                    break;
            }
        }
    };

    apprtcContext.registerReceiver(wiredHeadsetReceiver, filter);
}
 
Example 13
Source Project: 365browser   File: AudioManagerAndroid.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Registers receiver for the broadcasted intent when a wired headset is
 * plugged in or unplugged. The received intent will have an extra
 * 'state' value where 0 means unplugged, and 1 means plugged.
 */
private void registerForWiredHeadsetIntentBroadcast() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);

    /** Receiver which handles changes in wired headset availability. */
    mWiredHeadsetReceiver = new BroadcastReceiver() {
        private static final int STATE_UNPLUGGED = 0;
        private static final int STATE_PLUGGED = 1;
        private static final int HAS_NO_MIC = 0;
        private static final int HAS_MIC = 1;

        @Override
        public void onReceive(Context context, Intent intent) {
            int state = intent.getIntExtra("state", STATE_UNPLUGGED);
            if (DEBUG) {
                int microphone = intent.getIntExtra("microphone", HAS_NO_MIC);
                String name = intent.getStringExtra("name");
                logd("BroadcastReceiver.onReceive: a=" + intent.getAction()
                        + ", s=" + state
                        + ", m=" + microphone
                        + ", n=" + name
                        + ", sb=" + isInitialStickyBroadcast());
            }
            switch (state) {
                case STATE_UNPLUGGED:
                    synchronized (mLock) {
                        // Wired headset and earpiece and USB audio are mutually exclusive.
                        mAudioDevices[DEVICE_WIRED_HEADSET] = false;
                        if (hasUsbAudio()) {
                            mAudioDevices[DEVICE_USB_AUDIO] = true;
                            mAudioDevices[DEVICE_EARPIECE] = false;
                        } else if (hasEarpiece()) {
                            mAudioDevices[DEVICE_EARPIECE] = true;
                            mAudioDevices[DEVICE_USB_AUDIO] = false;
                        }
                    }
                    break;
                case STATE_PLUGGED:
                    synchronized (mLock) {
                        // Wired headset and earpiece and USB audio are mutually exclusive.
                        mAudioDevices[DEVICE_WIRED_HEADSET] = true;
                        mAudioDevices[DEVICE_EARPIECE] = false;
                        mAudioDevices[DEVICE_USB_AUDIO] = false;
                    }
                    break;
                default:
                    loge("Invalid state");
                    break;
            }

            // Update the existing device selection, but only if a specific
            // device has already been selected explicitly.
            if (deviceHasBeenRequested()) {
                updateDeviceActivation();
            } else if (DEBUG) {
                reportUpdate();
            }
        }
    };

    // Note: the intent we register for here is sticky, so it'll tell us
    // immediately what the last action was (plugged or unplugged).
    // It will enable us to set the speakerphone correctly.
    ContextUtils.getApplicationContext().registerReceiver(mWiredHeadsetReceiver, filter);
}
 
Example 14
@Override
public void onResume() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(headsetReceiver, filter);
    super.onResume();
}
 
Example 15
/**
 * Registers receiver for the broadcasted intent when a wired headset is
 * plugged in or unplugged. The received intent will have an extra
 * 'state' value where 0 means unplugged, and 1 means plugged.
 */
private void registerForWiredHeadsetIntentBroadcast()
{
   IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);

   /** Receiver which handles changes in wired headset availability. */
   wiredHeadsetReceiver = new BroadcastReceiver() {
      private static final int STATE_UNPLUGGED = 0;
      private static final int STATE_PLUGGED = 1;
      private static final int HAS_NO_MIC = 0;
      private static final int HAS_MIC = 1;

      @Override
      public void onReceive(Context context, Intent intent)
      {
         int state = intent.getIntExtra("state", STATE_UNPLUGGED);
         int microphone = intent.getIntExtra("microphone", HAS_NO_MIC);
         String name = intent.getStringExtra("name");
         RCLogger.d(TAG, "BroadcastReceiver.onReceive" + AppRTCUtils.getThreadInfo()
               + ": "
               + "a=" + intent.getAction()
               + ", s=" + (state == STATE_UNPLUGGED ? "unplugged" : "plugged")
               + ", m=" + (microphone == HAS_MIC ? "mic" : "no mic")
               + ", n=" + name
               + ", sb=" + isInitialStickyBroadcast());

         switch (state) {
            case STATE_UNPLUGGED:
               updateAudioDeviceState(false);
               break;
            case STATE_PLUGGED:
               updateAudioDeviceState(true);
               break;
            default:
               RCLogger.e(TAG, "Invalid state");
               break;
         }
      }
   };

   apprtcContext.registerReceiver(wiredHeadsetReceiver, filter);
}
 
Example 16
Source Project: freemp   File: ServicePlayer.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // initialize default output device
    if (!BASS.BASS_Init(-1, 44100, 0)) {
        return;
    }

    // look for plugins
    plugins = "";
    String path = getApplicationInfo().nativeLibraryDir;
    String[] list = new File(path).list();
    for (String s : list) {
        int plug = BASS.BASS_PluginLoad(path + "/" + s, 0);
        if (plug != 0) { // plugin loaded...
            plugins += s + "\n"; // add it to the list
        }
    }
    if (plugins.equals("")) plugins = "no plugins - visit the BASS webpage to get some\n";
    if (activity != null) {
        activity.onPluginsLoaded(plugins);
    }
    BASS.BASS_SetConfig(BASS.BASS_CONFIG_BUFFER, 1000);
    Log.w("BASS.BASS_CONFIG_BUFFER", "" + BASS.BASS_GetConfig(BASS.BASS_CONFIG_BUFFER));
    //screen
    screenHeight = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("screenHeight", 1000);
    screenWidth = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt("screenWidth", 800);

    // Pending Intend
    Intent intent = new Intent(this, ActPlayer.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

    //tracklist
    updateTrackList();

    tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(telephone, PhoneStateListener.LISTEN_CALL_STATE);
    myBroadcastReceiver = new MyBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
    intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
    registerReceiver(myBroadcastReceiver, intentFilter);


    mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
    ComponentName rcvMedia = new ComponentName(getPackageName(), RcvMediaControl.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(rcvMedia);

    // Use the remote control APIs (if available) to set the playback state
    if (android.os.Build.VERSION.SDK_INT >= 14 && remoteControlClient == null) {
        registerRemoteControl(rcvMedia);
    }
}
 
Example 17
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate");

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    populateCameraOrientations();

    setContentView(R.layout.tabhost);

    IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().compareTo(Intent.ACTION_HEADSET_PLUG)
                    == 0) {
                int state = intent.getIntExtra("state", 0);
                Log.v(TAG, "Intent.ACTION_HEADSET_PLUG state: " + state +
                     " microphone: " + intent.getIntExtra("microphone", 0));
                if (voERunning) {
                    routeAudio(state == 0 && cbEnableSpeaker.isChecked());
                }
            }
        }
    };
    registerReceiver(receiver, receiverFilter);

    mTabHost = getTabHost();

    // Main tab
    mTabSpecVideo = mTabHost.newTabSpec("tab_video");
    mTabSpecVideo.setIndicator("Main");
    mTabSpecVideo.setContent(R.id.tab_video);
    mTabHost.addTab(mTabSpecVideo);

    // Shared config tab
    mTabHost = getTabHost();
    mTabSpecConfig = mTabHost.newTabSpec("tab_config");
    mTabSpecConfig.setIndicator("Settings");
    mTabSpecConfig.setContent(R.id.tab_config);
    mTabHost.addTab(mTabSpecConfig);

    TabSpec mTabv;
    mTabv = mTabHost.newTabSpec("tab_vconfig");
    mTabv.setIndicator("Video");
    mTabv.setContent(R.id.tab_vconfig);
    mTabHost.addTab(mTabv);
    TabSpec mTaba;
    mTaba = mTabHost.newTabSpec("tab_aconfig");
    mTaba.setIndicator("Audio");
    mTaba.setContent(R.id.tab_aconfig);
    mTabHost.addTab(mTaba);

    int childCount = mTabHost.getTabWidget().getChildCount();
    for (int i = 0; i < childCount; i++) {
        mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;
    }
    orientationListener =
            new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
                public void onOrientationChanged (int orientation) {
                    if (orientation != ORIENTATION_UNKNOWN) {
                        currentDeviceOrientation = orientation;
                        compensateCameraRotation();
                    }
                }
            };
    orientationListener.enable ();

    // Create a folder named webrtc in /scard for debugging
    webrtcDebugDir = Environment.getExternalStorageDirectory().toString() +
            webrtcName;
    File webrtcDir = new File(webrtcDebugDir);
    if (!webrtcDir.exists() && webrtcDir.mkdir() == false) {
        Log.v(TAG, "Failed to create " + webrtcDebugDir);
    } else if (!webrtcDir.isDirectory()) {
        Log.v(TAG, webrtcDebugDir + " exists but not a folder");
        webrtcDebugDir = null;
    }

    startMain();

    if (AUTO_CALL_RESTART_DELAY_MS > 0)
        startOrStop();
}
 
Example 18
/**
 * Register this receiver.
 *
 * @param context The context
 * @param handler A handler for headset plug events
 */
public void register(final Context context, final HeadsetPlugHandler handler) {
	mHandler = handler;
	IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
	context.registerReceiver(this, intentFilter);
}