通过更改NFC读取方式减少延迟
在使用NFC技术进行读取时,可能出现延迟的情况。解决方法是采用非同步读取方式,可以通过PendingIntent来实现。
具体代码示例如下:
private void enableForegroundDispatchSystem(NfcAdapter nfcAdapter, Activity activity) {
Intent intent = new Intent(activity, activity.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
IntentFilter[] intentFilters = new IntentFilter[]{};
nfcAdapter.enableForegroundDispatch(activity, pendingIntent, intentFilters, null);
}
@Override
protected void onPause() {
super.onPause();
disableForegroundDispatchSystem();
}
@Override
protected void onResume() {
super.onResume();
enableForegroundDispatchSystem(nfcAdapter, this);
}
private void disableForegroundDispatchSystem() {
nfcAdapter.disableForegroundDispatch(this);
}