Android onNewIntent()上的NFC标记为空
我一直在开发一个应用程序,它使用NFC标签来做一些魔术 一切都很好,直到最近,我改变了一些代码,这些代码与之前一直有效的NFC代码无关 当我通过NFC tap启动我的应用程序时,当我在应用程序运行时点击时,所有功能都正常,我将在onNewIntent()中接收未来的NFCTag 当我通过图标启动应用程序并在应用程序运行时尝试点击时,会调用onNewIntent()方法,但当我尝试从intent中获取额外的NFCTag时,它会返回null 我是否正确地认为,即使它为null,但自从调用onNewIntent()以来,我已经正确地设置了ForegroundDispatch 这是密码Android onNewIntent()上的NFC标记为空,android,nfc,Android,Nfc,我一直在开发一个应用程序,它使用NFC标签来做一些魔术 一切都很好,直到最近,我改变了一些代码,这些代码与之前一直有效的NFC代码无关 当我通过NFC tap启动我的应用程序时,当我在应用程序运行时点击时,所有功能都正常,我将在onNewIntent()中接收未来的NFCTag 当我通过图标启动应用程序并在应用程序运行时尝试点击时,会调用onNewIntent()方法,但当我尝试从intent中获取额外的NFCTag时,它会返回null 我是否正确地认为,即使它为null,但自从调用onNewI
protected void onResume() {
if(this.mNfcAdapter==null) {
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter nfcFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
nfcFilter.addDataType("application/application.myorg.myapp");
} catch (MalformedMimeTypeException e) {
Log.e(TAG, "Error Setting FD for NFC", e);
}
String[][] mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
mNfcAdapter.enableForegroundDispatch(this, pendingIntent, new IntentFilter[] {nfcFilter}, mTechLists);
}
protected void onPause() {
super.onPause();
mNfcAdapter.disableForegroundDispatch(this);
Log.d(TAG, "Activity is pausing");
}
protected void onNewIntent(Intent intent) {
Log.d(TAG, "NFC TAP WHILE ACTIVE");
Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
if(tag!=null) {
//NEVER CALLED WHEN LAUNCHED VIA ICON (NOT NFC)
Log.d(TAG, "TAG IS NOT NULL");
}
}
我在IntentFilter中设置的MIME类型与我在清单中设置的MIME类型相同
编辑
我的舱单
<activity
android:name="org.mypackage.myapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<data android:mimeType="application/org.mypackage.myapp" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
问题在于如何在
onNewIntent()方法中检索意图。当前,您正在使用getIntent()
获取试图从中检索EXTRA\u标记的意图。除非您使用setIntent(…)
来更改它(您在代码的相关部分中显然没有做什么),否则这将返回最初启动的意图。NFC发现意图被传递到参数intent intent
中的onNewIntent()
方法。因此,使用它应该可以做到:
protected void onNewIntent(Intent intent) {
Log.d(TAG, "NFC TAP WHILE ACTIVE");
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
Log.d(TAG, "TAG IS NOT NULL");
}
}
此外,您可能需要检查您收到的意图是否实际具有您期望的意图操作(例如,操作\u NDEF\u发现的
)
还有一件事:当您注册NDEF\u DISCOVERED
意图时,您可以安全地将mTechLists
设置为null
。技术列表仅用于tech\u发现的
intent筛选器。问题在于如何在onNewIntent()
方法中检索意图。当前,您正在使用getIntent()
获取试图从中检索EXTRA\u标记的意图。除非您使用setIntent(…)
来更改它(您在代码的相关部分中显然没有做什么),否则这将返回最初启动的意图。NFC发现意图被传递到参数intent intent
中的onNewIntent()
方法。因此,使用它应该可以做到:
protected void onNewIntent(Intent intent) {
Log.d(TAG, "NFC TAP WHILE ACTIVE");
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
Log.d(TAG, "TAG IS NOT NULL");
}
}
此外,您可能需要检查您收到的意图是否实际具有您期望的意图操作(例如,操作\u NDEF\u发现的
)
还有一件事:当您注册NDEF\u DISCOVERED
意图时,您可以安全地将mTechLists
设置为null
。技术列表仅用于tech\u发现的
intent过滤器。您还可以显示清单的相关部分(即该活动的活动部分)吗?此外,您的标签包含什么NDEF消息?嗨,Michael,更新的问题:)您还可以显示清单的相关部分吗(即该活动的活动部分)?此外,您的标签包含什么NDEF消息?嗨,Michael,更新的问题:)明显的错误,我怎么错过了getIntent()?!?!太好了,也谢谢你的额外提示。明显的错误,我错过了getIntent()?!?!太好了,谢谢你的额外提示。