Embarcadero XE5中的Android NFC
试图让NFC在Embarcadero XE5中使用Android。 从以下几点开始: 这似乎起作用了。现在,我想为NFC目的注册回调 Java方法:Embarcadero XE5中的Android NFC,android,delphi,nfc,delphi-xe5,Android,Delphi,Nfc,Delphi Xe5,试图让NFC在Embarcadero XE5中使用Android。 从以下几点开始: 这似乎起作用了。现在,我想为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.
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);
}
}
资料来源:
德尔菲法(如我所想):
资料来源:
我想应该调用enableForegroundDispatch
方法。将其定义为:
procedure enableForegroundDispatch; cddcl;
从Android API调用它
但由于我以前从未做过这件事,我不知道如何继续编辑:似乎我错过了一个标记,OP也没有要求Java代码。将此内容留作将来参考
您的猜测是正确的,虽然可以在AndroidManifest.xml中定义您想要监听的意图,但前台调度确实将您的应用程序置于前端,使您能够捕获所有启动的NFC意图
文章中描述的方式为您提供了线索
我想你们对安卓系统的活动、意图调度等都很熟悉
结构 使用以下结构,您将有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和NDEFoformable
我们为什么订阅ACTION\u NDEF\u Discovery
Android尝试处理意图的顺序如下:
启用烟气脱硫 将以下代码行放入
onResume
方法中:
if (mNfcAdapter != null) {
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilters, mTechLists);
}
为什么这会出现在简历中?正如文档所述:enableForegroundDispatch()必须从主线程调用,并且仅当活动位于前台时(调用onResume()可以保证这一点)
当然,这应该能让你的应用程序在实际运行时收到目的。
如果希望在不运行时接收意图,则必须转到AndroidManifest
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()}};
}
if (mNfcAdapter != null) {
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, mIntentFilters, mTechLists);
}