美文网首页
Android:监听耳机状态

Android:监听耳机状态

作者: 筱南独舞 | 来源:发表于2017-08-04 14:43 被阅读47次

建立BroadcastReceiver的实例。

private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.e("action = " + action);
        if (action.equals(Intent.ACTION_HEADSET_PLUG)) {
            if (intent.hasExtra("state")) {
                int state = intent.getIntExtra("state", 0);
                Timber.e("action state = " + state);
                handsfree(state == 0);
                if (mInCallingDialog != null) {
                    mInCallingDialog.setAllStatus(isMute, isHandfree);
                }
                    // 0为拔出耳机
            }
        }
    }
};

相关文章

网友评论

      本文标题:Android:监听耳机状态

      本文链接:https://www.haomeiwen.com/subject/dgkxhxtx.html