Bor*_*lov 17 delphi android nfc delphi-xe5
尝试在Embarcadero XE5中使用NFC在Android上运行.从以下内容开始:https://forums.embarcadero.com/thread.jspa?threadID = 97574 ,它们似乎正在运行.现在想注册NFC Intent的回调
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);
}
}
Run Code Online (Sandbox Code Playgroud)
资料来源:http://www.jessechen.net/blog/how-to-nfc-on-the-android-platform/
德尔福方法(我想象):
1. Define methods available in Java interface
Run Code Online (Sandbox Code Playgroud)
来源: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?
Run Code Online (Sandbox Code Playgroud)
我的猜测是调用enableForegroundDispatch方法.定义如下:
procedure enableForegroundDispatch; cddcl;
Run Code Online (Sandbox Code Playgroud)
从Android API调用它
但由于我以前从未这样做过,所以我不知道如何继续
编辑:似乎我错过了一个标签,并且 OP 不要求 Java 代码。无论如何留下这个以供将来参考
您的猜测是正确的,尽管可以在 AndroidManifest.xml 中定义您想要侦听的意图,但前台调度确实将您的应用程序放在前面,使您能够捕获所有启动的 NFC 意图。
文档中描述的方式为您提供了线索。
我假设您熟悉Android 活动的生命周期、意图调度等。
使用以下结构,您将有 4 个字段:
private PendingIntent pendingIntent;
private IntentFilter[] mIntentFilters;
private String[][] mTechLists;
private NfcAdapter mNfcAdapter;
Run Code Online (Sandbox Code Playgroud)
在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()}};
}
Run Code Online (Sandbox Code Playgroud)
这实际上还没有启用前台调度,这只是准备工作。应用程序将接收 Ndef 和 NdefFormatable技术 为什么我们要订阅 ACTION_NDEF_DISCOVERED ?
Android 尝试处理意图的顺序如下:
因此,我们确保我们的应用程序是 Android 第一个查看的应用程序。
将以下代码行放入方法中onResume:
if (mNfcAdapter != null) {
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilters, mTechLists);
}
Run Code Online (Sandbox Code Playgroud)
为什么这是在onResume?正如文档所述:enableForegroundDispatch() must be called from the main thread and only when the activity is in the foreground (calling in onResume() guarantees this)
当然,这应该使您的应用程序在实际运行时能够接收意图。如果您想在不运行时接收意图,则必须转到 AndroidManifest。
| 归档时间: |
|
| 查看次数: |
1442 次 |
| 最近记录: |