尝试让NFC在EmbarcaderoXe5中使用Android。
从以下内容开始:https://forums.embarcadero.com/thread.jspa?threadID=97574
这似乎起作用了。现在想注册NFC意图的回调
Java方法:
1. Register current activity as a listener
...
2. Receive Intent
@Override
protected void onNewIntent(Intent intent) {
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
NdefMessage[] msgs = NfcUtils.getNdefMessages(intent);
}
}
资料来源:http://www.jessechen.net/blog/how-to-nfc-on-the-android-platform/
Delphi方法(如我想象的那样):
1. Define methods available in Java interface
资料来源:https://forums.embarcadero.com/thread.jspa?messageID=634212
Question:
How do I register a listener for NFC intent messages and
how do I eventually get messages?
我想应该调用
enableForegroundDispatch
方法。定义如下:procedure enableForegroundDispatch; cddcl;
从Android API调用它
但因为我以前从来没有这样做过,所以我不知道如何继续
最佳答案
编辑:好像我错过了一个标签,而OP不需要Java代码。不管怎样都要留着以备将来参考
您的猜测是正确的,尽管可以在androidmanifest.xml中定义您想要监听的意图,前台调度确实将您的应用放在了最前面,使您能够捕获所有启动的NFC意图。
docs中描述的方式为您提供了一个线索。
我假设您熟悉Android活动的lifecycle和意图调度等。
结构
使用以下结构,您将有4个字段:
private PendingIntent pendingIntent;
private IntentFilter[] mIntentFilters;
private String[][] mTechLists;
private NfcAdapter mNfcAdapter;
在
onCreate
中,您会得到:protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mIntentFilters = new IntentFilter[]{new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)};
mTechLists = new String[][]{new String[]{Ndef.class.getName()},
new String[]{NdefFormatable.class.getName()}};
}
这实际上还没有启用前台调度,只是准备工作。
应用程序将收到ndef和ndefformatabletechnologies
我们为什么要订阅发现的行动?
Android试图处理意图的顺序如下:
发现行动
发现行动技术
发现动作标记
所以我们要确保我们的应用程序是第一个被Android看到的。
启用FGD
在
onResume
方法中放入以下代码行:if (mNfcAdapter != null) {
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilters, mTechLists);
}
为什么会出现在
onResume
中?作为文档状态:enableForegroundDispatch() must be called from the main thread and only when the activity is in the foreground (calling in onResume() guarantees this)
当然,这应该可以让你的应用在实际运行时接收到意图。
如果你想在不跑步的时候收到意图,你就必须去仙女座舱单。