这是我检测耳机是连接还是断开连接的方法。但我需要找到一个信用
“读卡器”连接到Android设备。因为它也会使用与插入耳机相同的端口。所以如何区分连接设备是“读卡器”或“耳机”。我已经搜索过它以便找到它。但是没有任何解决方案可以找到“读卡器”插入/拔出。
private static final String TAG = "plugstatus";
private PlugListener myReceiver;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new PlugListener();
}
@Override public void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(myReceiver, filter);
super.onResume();
}
private class PlugListener extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d(TAG, "Headset unplugged");
break;
case 1:
Log.d(TAG, "Headset plugged");
break;
default:
Log.d(TAG, "Not able to detect plug state");
}
}
}
}
@Override public void onPause() {
unregisterReceiver(myReceiver);
super.onPause();
}
答案 0 :(得分:0)
基本上我们无法检测到它是耳机还是读卡器。