[英]How to get a information or an nfc id of Android program programmatically?
[英]How to get NFC id in Android?
我需要獲取設備的NFC ID。
AndroidManifest看起來像這樣:
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
為了獲得ID,在MainActivity中(而不是在片段中),我添加了以下代碼:
public void onResume() {
super.onResume();
Log.i("MYTAG", "entered");
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
Log.i("MYTAG", "enter here also");
Tag tag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
Toast.makeText(getApplicationContext(), "TAG: " + tag.getId().toString(), Toast.LENGTH_LONG).show();
}
}
但這沒有輸入if語句。 有人有主意嗎?
我嘗試使用從stackoverflow找到的不同答案,但沒有任何變化。
我會去if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction()))
,但是由於缺少設備而無法測試它^^
問題是您在清單意圖過濾器中為TAG_DISCOVERED指定,但在代碼中嘗試與ACTION_NDEF_DISCOVERED匹配。 我在onResume中使用TAG_DISCOVERED測試了您的代碼,沒關系(Nexus 5)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.