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

时间:2018-09-01 11:30:41

标签: java android android-audiomanager

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

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

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

注意:我知道我仍然可以使用不推荐使用的方法,但是我不想这样做。如果他们不赞成这样做,他们应该引入了替代API(尽管我找不到)。

2 个答案:

答案 0 :(得分:1)

由于AndroidStudio警告AudioManager.getDevices需要VersionCode 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

答案 1 :(得分: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);
        }
    }
  }

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