[当扫描到打开应用程序时如何在Android上使用NFC标签

问题描述 投票:0回答:2

我正在询问以下问题以寻求建议,我已经在网上查看(以及有关stackoverflow的类似问题),但没有适合我的解决方案。

我正在编写一个Android应用程序(为了全面披露,我正在Xamarin中进行此操作-但技术[[should不相关),而我想要做的就是在NFC时显示警报/祝酒消息代码已被扫描。如果我打开了应用程序,并且将我的NFC标签贴在手机上,则会按预期显示警报。当应用程序关闭并且我将NFC标签贴在手机上时,该应用程序将打开,但已尽其所能。

我是Android开发的新手(以前我通过Xamarin创建Android应用程序时非常通用,没有真正使用设备功能(如NFC /蓝牙),所以如果我遗漏了一些明显的内容,我深表歉意。) >

using System.Text; using Android.App; using Android.Content; using Android.Nfc; using Android.OS; using Android.Support.V7.App; namespace Android.Demo { [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true, LaunchMode = Content.PM.LaunchMode.SingleInstance)] [IntentFilter(new[] { NfcAdapter.ActionNdefDiscovered }, Categories = new[] { "android.intent.category.DEFAULT" }, DataMimeType = "*/*")] public class MainActivity : AppCompatActivity { private NfcAdapter _nfcAdapter; private PendingIntent _pendingIntent; private string[][] _techList = new string[][]{ new[] { "android.nfc.tech.NdefFormatable" } , new [] { "android.nfc.tech.NfcA" } , new [] { "android.nfc.tech.Ndef" }, new [] { "android.nfc.tech.Ndef" }}; protected override void OnCreate(Bundle savedInstanceState) { _nfcAdapter = NfcAdapter.GetDefaultAdapter(this); base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); SetContentView(Resource.Layout.activity_main); _pendingIntent = PendingIntent.GetActivity(this, 0, Intent, 0); Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); } protected override void OnPause() { base.OnPause(); if (_nfcAdapter != null && _nfcAdapter.IsEnabled) _nfcAdapter.DisableForegroundDispatch(this); } protected override void OnResume() { base.OnResume(); // doStuff(); _nfcAdapter.EnableForegroundDispatch(this, _pendingIntent, new[] { new IntentFilter(NfcAdapter.ActionNdefDiscovered), new IntentFilter(NfcAdapter.ActionTechDiscovered), new IntentFilter(NfcAdapter.ActionTagDiscovered), new IntentFilter(NfcAdapter.ActionNdefDiscovered) }, _techList); } protected override void OnNewIntent(Intent intent) { var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag != null) { // First get all the NdefMessage var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages); if (rawMessages != null) { var msg = (NdefMessage)rawMessages[0]; // Get NdefRecord which contains the actual data var record = msg.GetRecords()[0]; if (record != null) { if (record.Tnf == NdefRecord.TnfWellKnown) // The data is defined by the Record Type Definition (RTD) specification available from http://members.nfc-forum.org/specs/spec_list/ { // Get the transfered data var data = Encoding.ASCII.GetString(record.GetPayload()); var alert = new App.AlertDialog.Builder(this).Create(); alert.SetMessage(data); alert.SetTitle("NFC Tag Located"); alert.Show(); } } } } } } }

谢谢

我正在询问以下问题以寻求建议,我已经在网上查看(以及有关stackoverflow的类似问题),但没有适合我的解决方案。我正在编写一个Android应用(完整披露...

android xamarin.android nfc
2个回答
0
投票
我有一个应用程序,它在运行时会读取标签,并注册为处理标签,以便在未运行时启动。

0
投票
通常,您应仅使用onNewIntent(Intent intent)来设置您拥有的最后一个互联网。我将通过以下方式编写您的代码:
© www.soinside.com 2019 - 2024. All rights reserved.