【发布时间】:2021-02-26 05:26:00
【问题描述】:
我已尝试实现 Google 文档中的代码,但我仍然不清楚。我在这里错过了什么?
这是 MainActivity.java 中的方法:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
Parcelable[] rawMessages =
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMessages != null) {
NdefMessage[] messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i++) {
messages[i] = (NdefMessage) rawMessages[i];
Log.i("nfcccc",messages[i].toString());
}
// Process the messages array.
Log.i("from tag: ",messages.toString());
Toast.makeText(this,messages.toString(),Toast.LENGTH_LONG).show();
}
}
}
在 onCreated 方法中我还初始化了 nfcAdapter:
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
在清单文件中我有这个:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我花了几天时间尝试实现它,但它仍然无法正常工作。 我正在做的是我使用 NFC Tools android 应用程序来保存纯文本,并且我想在我自己的应用程序中读取这些数据>
【问题讨论】:
标签: android android-studio nfc reader