[英]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.