0
這是我如何檢測耳機是連接還是斷開連接。但我需要找到信用卡時插入時在Android中檢測讀卡器(信用卡或借記卡)
「讀卡器」已連接到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();
}