0

这就是我检测耳机是连接还是断开连接的方式。但我需要找到信用的时候

“读卡器”连接到安卓设备。因为这也将使用与插入耳机相同的端口。所以如何区分连接的设备是“读卡器”还是“耳机”。我已经搜索过它来专门找到它。但是找不到任何解决方案来发现“读卡器”已插入/拔出。

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();
}
4

1 回答 1

0

基本上我们无法具体检测它是耳机还是读卡器。

于 2016-02-23T11:53:30.537 回答