微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 检测耳机是否有麦克风

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

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

我已经找到了一些使用ACTION_HEADSET_PLUG的建议,但我很想知道这些信息,即使在打开我的应用程序之前插入了耳机,这个事件也不会在我的应用程序生命周期内被触发.

关于这个问题的任何想法?先感谢您.

解决方法

更新:
继续在您的活动的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);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐