检测耳机是否有麦克风 [英] Detect whether headset has microphone

查看:532
本文介绍了检测耳机是否有麦克风的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测是否插入有线耳机具有麦克风与否。

I need to detect whether the plugged in wired headset has microphone or not.

我可以检查是否插入耳机使用 isWiredHeadSetOn(),但麦克风似乎并没有在AudioManager类这样的方法。

I can check if a headset is plugged in using isWiredHeadSetOn(), but for microphone does not seem to be such a method in AudioManager class.

我发现使用 ACTION_HEADSET_PLUG 的一些建议,但我我有兴趣了解,即使耳机已经插上打开我的应用程序在此之前的信息,该事件将不会被我的应用程序的生命周期内被解雇。

I have found some suggestions using ACTION_HEADSET_PLUG, but I am interested to find out this information even if the headset has been plugged in before opening my application, this event won't be fired during the lifetime of my app.

任何有关这个问题的想法?谢谢你在前进。

Any ideas regarding this issue? Thank you in advance.

推荐答案

更新: 来吧,在注册 ACTION_HEADSET_PLUG 您的活动的 onResume()。 如果用户曾经插入/她的耳机后启动时,平台将提供最新的状态,你的活动将恢复的时候。

UPDATE: Go ahead and register ACTION_HEADSET_PLUG in your activity's onResume(). If user has ever plugged in/out her headset after boot-up, platform will deliver the latest state to your activity when it resumes.

下面的测试code的工作:

Following test code worked:

package com.example.headsetplugtest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

public class HeadSetPlugIntentActivity extends Activity {

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
                Log.d("HeadSetPlugInTest", "state: " + intent.getIntExtra("state", -1));
                Log.d("HeadSetPlugInTest", "microphone: " + intent.getIntExtra("microphone", -1));
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        getApplicationContext().registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();

        getApplicationContext().unregisterReceiver(mReceiver);
    }
}

这篇关于检测耳机是否有麦克风的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆