我是一个有用的助手,可以翻译文本。
我有一个使用Xamarin开发的应用程序,可以读取NFC标签。当应用程序打开时,它可以正常工作,但如果应用程序在后台或关闭时,无法读取标签上的数据。
我的意图过滤器是:
在我的OnNewIntent中,当从应用程序外部扫描时,我的intent.Data始终返回为空。您认为我的问题可能出在哪里吗?
OnResume:
所以有趣的事情是,当在应用程序外部扫描时,AndroidNFCHelper.isNfcIntent返回false。当在应用程序中扫描相同的NFC标签时,它会返回true。继续进行,标签最终为null并且只是命中了return语句。当在应用程序中扫描时,标签不为null并继续执行。
PrepareForegroundNFCHandling - 在OnCreate()中被调用:
我有一个使用Xamarin开发的应用程序,可以读取NFC标签。当应用程序打开时,它可以正常工作,但如果应用程序在后台或关闭时,无法读取标签上的数据。
我的意图过滤器是:
[IntentFilter(
new[] {"android.nfc.action.NDEF_DISCOVERED", "android.intent.action.VIEW", "android.intent.action.MAIN"},
Categories =
new[]
{
"android.intent.category.DEFAULT", "android.intent.category.BROWSABLE",
"android.intent.category.LAUNCHER"
},
DataScheme = "http", DataHost = "app.myDomain.com")]
在我的OnNewIntent中,当从应用程序外部扫描时,我的intent.Data始终返回为空。您认为我的问题可能出在哪里吗?
OnResume:
nfcAdapter.EnableForegroundDispatch(this, nfcPendingIntent, nfcIntentFiltersArray, null);
OnNewIntent:
protected override void OnNewIntent(Intent intent)
{
if (AndroidNFCHelper.isNfcIntent(intent))
{
DisplayMessage("And it is an NFC intent");
}
var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
if (tag == null)
{
Debug.WriteLine("tag is null");
return;
}
//rest of OnNewIntent code...
}
所以有趣的事情是,当在应用程序外部扫描时,AndroidNFCHelper.isNfcIntent返回false。当在应用程序中扫描相同的NFC标签时,它会返回true。继续进行,标签最终为null并且只是命中了return语句。当在应用程序中扫描时,标签不为null并继续执行。
PrepareForegroundNFCHandling - 在OnCreate()中被调用:
private void PrepareForegroundNFCHandling()
{
var tagDetected = new IntentFilter(NfcAdapter.ActionTagDiscovered);
var ndefDetected = new IntentFilter(NfcAdapter.ActionNdefDiscovered);
nfcIntentFiltersArray = new[] { ndefDetected, tagDetected};
var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop | ActivityFlags.BroughtToFront);
nfcPendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
nfcAdapter = NfcAdapter.GetDefaultAdapter(Application.ApplicationContext);
}