我正在尝试获得一段代码,以便能够扫描标签并在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>发布于 2014-03-18 22:13:53
如果要在您的活动已在前台可见时接收NFC标签发现事件,则应向NFC前台调度系统注册。参见Advanced NFC: Using the NFC Foreground Dispatch System。您通常会使用NfcAdapter的enableForegroundDispatch()方法进行注册。之后,您可以在您的活动的onNewIntent()方法中获取意图(或者作为一个挂起的意图结果,这取决于您是如何注册的)。
发布于 2016-09-24 05:31:34
无效onNewIntent (Intent intent)
对于在其包中将launchMode设置为"singleTop“的活动,或者如果客户端在调用startActivity(Intent)时使用FLAG_ACTIVITY_SINGLE_TOP标志,则会调用此函数。在任何一种情况下,当活动处于活动堆栈的顶部而不是正在启动的活动的新实例时重新启动活动时,将使用重新启动它的意图在现有实例上调用onNewIntent()。
https://stackoverflow.com/questions/22467510
复制相似问题