怎么调试耳机麦克风:检测耳机是否有麦克风(android test bluetooth microphone)

关于怎么调试耳机麦克风的问题,在android test bluetooth microphone中经常遇到, 我需要检测插入的有线耳机是否有麦克风。

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

我可以检查耳机是否使用isWiredHeadSetOn()插入,但对于麦克风似乎不是 AudioManager 类中的这种方法。

我发现了一些使用ACTION_HEADSET_PLUG的建议,但我有兴趣找出这些信息,即使耳机在打开我的应用程序之前已经插入,这个事件在我的应用程序的生命周期内不会被触发。

关于这个问题有什么想法吗?提前谢谢你。

13

更新:继续并在活动的onResume()中注册ACTION_HEADSET_PLUG。如果用户在启动后插入 / 拔出耳机,平台将在活动恢复时向其提供最新状态。

以下测试代码工作:

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);
    }
}

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(487)
节奏大师ios:AKFrequencyTracker与节奏
上一篇
什么是软件外包:什么是“成熟”软件 (classic matures)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(58条)