Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当扫描打开应用程序时,如何在Android上使用NFC标签_Android_Xamarin.android_Nfc - Fatal编程技术网

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

当扫描打开应用程序时,如何在Android上使用NFC标签,android,xamarin.android,nfc,Android,Xamarin.android,Nfc,我问了以下问题以寻求建议,我已经在网上看过了(在stackoverflow上也有类似的问题),但没有适合我的解决方案 我正在编写一个Android应用程序(为了充分披露,我正在使用Xamarin进行开发,但这项技术应该是不相关的),我只想在扫描NFC代码时显示一条警报/toast消息。如果我打开应用程序,并将NFC标签贴在手机上,则会按预期显示警报。当应用程序关闭后,我将NFC标签贴在我的手机上,应用程序就会打开,但这就是它的作用 我对Android开发相当陌生(我以前在通过Xamarin创建A

我问了以下问题以寻求建议,我已经在网上看过了(在stackoverflow上也有类似的问题),但没有适合我的解决方案

我正在编写一个Android应用程序(为了充分披露,我正在使用Xamarin进行开发,但这项技术应该是不相关的),我只想在扫描NFC代码时显示一条警报/toast消息。如果我打开应用程序,并将NFC标签贴在手机上,则会按预期显示警报。当应用程序关闭后,我将NFC标签贴在我的手机上,应用程序就会打开,但这就是它的作用

我对Android开发相当陌生(我以前在通过Xamarin创建Android应用程序时非常普通,没有真正使用NFC/Bluetooth等设备功能),因此如果我遗漏了一些非常明显的东西,我道歉

使用System.Text;
使用Android.App;
使用Android.Content;
使用Android.Nfc;
使用Android.OS;
使用Android.Support.V7.App;
名称空间Android.Demo
{
[活动(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=“*/*”]
公共类MainActivity:AppCompativeActivity
{
私人NfcAdapter\u NfcAdapter;
私人吊挂帐篷_吊挂帐篷;
私有字符串[][\u techList=新字符串[][]{
新[]{“android.nfc.tech.ndeformable”},
新[]{“android.nfc.tech.NfcA”},
新[]{“android.nfc.tech.Ndef”},
新[]{“android.nfc.tech.Ndef”};
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
_nfcAdapter=nfcAdapter.GetDefaultAdapter(此);
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(这个,savedInstanceState);
SetContentView(Resource.Layout.activity_main);
_pendingent=pendingent.GetActivity(this,0,Intent,0);
Android.Support.V7.Widget.Toolbar-Toolbar=findviewbyd(Resource.Id.Toolbar);
设置支持操作栏(工具栏);
}
受保护的覆盖void OnPause()
{
base.OnPause();
如果(\u nfcAdapter!=null&&u nfcAdapter.IsEnabled)
_nfcAdapter.DisableForegroundDispatch(本);
}
受保护的覆盖void OnResume()
{
base.OnResume();
//doStuff();
_nfcAdapter.enableforgroundDispatch(此,_pendingent,new[]{new IntentFilter(nfcAdapter.ActionNdefDiscovered),new IntentFilter(nfcAdapter.ActionTechDiscovered),new IntentFilter(nfcAdapter.ActionTagDiscovered),new IntentFilter(nfcAdapter.ActionNdefDiscovered)},技术列表);
}
受保护的覆盖无效Wintent(意图)
{
var tag=intent.GetParcelableExtra(NfcAdapter.ExtraTag)作为标记;
如果(标记!=null)
{
//首先获取所有NDEFM消息
var rawMessages=intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
if(rawMessages!=null)
{
var msg=(NdefMessage)rawMessages[0];
//获取包含实际数据的NdefRecord
var record=msg.GetRecords()[0];
if(记录!=null)
{
if(record.Tnf==NdefRecord.TnfWellKnown)//数据由可从以下站点获得的记录类型定义(RTD)规范定义http://members.nfc-forum.org/specs/spec_list/
{
//获取传输的数据
var data=Encoding.ASCII.GetString(record.GetPayload());
var alert=new App.AlertDialog.Builder(this.Create();
警报。设置消息(数据);
警报。设置标题(“NFC标签定位”);
alert.Show();
}
}
}
}
}
}
}

谢谢

我有一个应用程序,在运行时读取标签,并注册到处理标签,以便在不运行时启动

您可能缺少的是,当您运行带有标记的
Intent
时,其中的一个数据会传递给
OnNewIntent
,但当您不运行该
Intent
时,会传递给
onCreate

在这里,我做了类似的事情:-


创建时受保护的覆盖无效(Bundle savedInstanceState)
{
//其他onCreate的东西
//获取启动应用程序的任何意图
var mIntent=getIntent();
//检查意图是否包含有关NFC数据的详细信息
var tag=minent.GetParcelableExtra(NfcAdapter.ExtraTag)作为标记;
如果(标记!=null)
{
OnNewIntent(Minent);
} 

注意我不做Xamarin,所以没有测试过,但我在Java中也做过类似的工作

通常您应该使用
onNewIntent(Intent-Intent)
来设置您拥有的最后一个intnet。 我将用以下方式编写您的代码:

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();
        if(isNfcIntent(getIntent())){
            processNfcIntent(getIntent());
        }

        // 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)
    {

        setIntent(intent);
    }

}

processIntent(Intent intent){
var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
         // 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();

                    }
                }
            }

     }
}

向我们展示符合您描述的代码请展示代码。否则,我们如何知道缺少了什么?抱歉,我已使用Xamarin代码更新了我的原始帖子。谢谢Andrew。我将在稍后回家时对此进行测试,并报告我的发现。谢谢,这工作绝对完美(在c#中有一个属性Intent,它与getIntent()的作用相同,因此必须对此进行一些调整)。非常感谢Karam,这也很有效,也是一个完全可以接受的答案。
private boolean isNfcIntent(Intent intent){

var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
    return tag != null;
}