我需要检测插入的有线耳机是否有麦克风.
我可以使用isWiredHeadSetOn()检查是否插入了耳机,但对于麦克风似乎不是AudioManager类中的这种方法.
我已经找到了一些使用ACTION_HEADSET_PLUG的建议,但我很想知道这些信息,即使在打开我的应用程序之前插入了耳机,这个事件也不会在我的应用程序生命周期内被触发.
关于这个问题的任何想法?先感谢您.
解决方法
更新:
继续在您的活动的onResume()中注册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);
}
}