我正在开发一个使用 NFC 标签进行识别的应用程序。然而,我发现的所有示例都是关于在读取某张卡片时启动应用程序。我尝试寻找有关如何以不同方式进行操作的示例或文档,但无济于事。
我想要的是:
- 用户启动我的应用
- 用户扫描 NFC 卡
- 应用决定下一步
我现在有一些代码工作,我只是没有得到标签数据:
在onCreate
:
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
tech.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
intentFiltersArray = new IntentFilter[] { tech };
并在onResume
:
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
意图仅在应用程序处于活动状态时到达那里,但Intent
我收到的是PendingIntent
我自己定义的,而不是ACTION_TECH_DISCOVERED
我想要的意图。