[英]Read a RFID/NFC Tag ID
我正在尝试获取一段代码,以便能够扫描标签并在TextView中显示该标签...
我已经对此深感困惑,任何建议将不胜感激...
当我扫描标签时,会播放被发现的标签的噪音...但是TextView不会更新...因此该应用程序当前可以扫描标签,但并未将所述标签ID放入TextView ...
Java主类
package com.security.nfc;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.widget.TextView;
public class main extends Activity
{
@Override
protected void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main);
}
public void onNewIntent(Intent intent)
{
Tag myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
TextView tagID = (TextView) findViewById(R.id.result);
tagID.setText("TagID: " + myTag.getId());
}
}
Android清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.security.nfc">
<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name="com.security.nfc.main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
</intent-filter>
</activity>
<uses-feature android:name="android.hardware.nfc" android:required="true" />
</application>
<uses-permission android:name="android.permission.NFC" />
</manifest>
如果您希望在活动已经在前台可见的同时接收NFC标签发现事件,则应在NFC前台调度系统中注册。 请参阅高级NFC:使用NFC前景调度系统 。 通常,您将使用NfcAdapter的enableForegroundDispatch()
方法进行注册。 之后,您可以在活动的onNewIntent()
方法中获得意图(或作为挂起的意图结果,具体取决于您的注册方式)。
onNewIntent(Intent intent)无效
对于在其程序包中将launchMode设置为“ singleTop”的活动,或者在客户端调用startActivity(Intent)时使用FLAG_ACTIVITY_SINGLE_TOP标志的活动,将调用此方法。 在这两种情况下,当在活动堆栈的顶部重新启动活动而不是启动活动的新实例时,将使用已用于重新启动的Intent在现有实例上调用onNewIntent()它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.