Detect whether headset has microphone

前端 未结 1 1785
长情又很酷
长情又很酷 2020-12-16 04:52

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

I can check if a headset is plugged in using isWiredHeadSetOn(), but for microphone does

相关标签:
1条回答
  • 2020-12-16 05:30

    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.

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题