我需要检测插入的有线耳机是否有麦克风。
我可以检查耳机是否使用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);
}
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(58条)