检查耳机是否连接在不同的 API 级别上

2022-09-03 16:05:00

我有一个面向 API 级别 19+ 的应用程序。我需要检查耳机是否已连接(有线或蓝牙)。

我看到从API级别16开始不推荐使用,并且仅从API级别23开始引入的文档提供使用。AudioManager.isWiredHeadsetOn()AudioManager.getDevices()

问题:检查耳机是否已连接 API 级别 16 - 22 的正确方法是什么?

注意:我知道我仍然可以使用已弃用的方法,但我不想这样做。如果他们弃用了它,他们应该引入一个替代API(虽然我找不到)。


答案 1

您需要收听ACTION_HEADSET_PLUG该事件在耳机事件时触发,并且即使没有任何变化,也显然也会定期触发。

从我现有的代码中,我在我的主要服务中有这个:onCreate

    // listen for headphone events so we can auto switch the audio output when headphones are plugged in
    myHeadphoneMonitor = new HeadphoneMonitor();
    android.content.IntentFilter headphone_filter = new 
    android.content.IntentFilter(Intent.ACTION_HEADSET_PLUG);
    registerReceiver(myHeadphoneMonitor, headphone_filter);

我的显示器看起来像这样:

  package com.myname.foo;

  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.util.Log;
  import android.os.Message;


  public class HeadphoneMonitor extends BroadcastReceiver
  {
    public boolean headphonesActive=false;

    @Override
    public void onReceive(final Context context, final Intent intent)
    {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG))
        {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
                    case 0:
                        Log.d("HeadphoneMonitor", "Headset is unplugged");
                        headphonesActive=false;
                        break;
                    case 1:
                        Log.d("HeadphoneMonitor", "Headset is plugged in");
                        headphonesActive=true;
                        break;
                    default:
                        Log.d("HeadphoneMonitor", "I have no idea what the headset state is");
                        break;
            }

            // push this event onto the queue to be processed by the Handler
            Message msg = MyApp.uiHandler.obtainMessage(MyApp.HEADPHONE_EVENT);
            MyApp.uiHandler.sendMessage(msg);
        }
    }
  }

清单中不需要任何内容来侦听此广播事件。


答案 2

由于AndroidStudio警告说AudioManager.getDevices需要版本代码M,最好的方法是进行这样的版本检查:

    // ... some code
    boolean isWiredHeadsetOn=false;
    AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    Log.d(TAG, "onCreate:::: retrieved AudioManager instance");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Log.d(TAG, "onCreate:::: BuildVersion>=M");
        AudioDeviceInfo[] mAudioDeviceInfos = mAudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
        Log.d(TAG, "onCreate:::: got AudioDeviceInfo[]");
        for (int i = 0; i < mAudioDeviceInfos.length; i++) {
            if (mAudioDeviceInfos[i].getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
                Log.d(TAG, "onCreate:::: \n\nfind wiredHeadset!!!\n\n");
                isWiredHeadsetOn=true;
            } else {
                Log.d(TAG, "onCreate:::: find device type: " + mAudioDeviceInfos[i].getType() + ", id: " + mAudioDeviceInfos[i].getProductName());
            }
        }
    } else {
        Log.d(TAG, "onCreate:::: BuildVersion<M");
        isWiredHeadsetOn=mAudioManager.isWiredHeadsetOn();
    }
    // ... continue code

希望我能帮忙。最好的问候, Cs


推荐