我需要获取设备的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中找到的不同答案,但没有任何变化。
问题是您在清单意图过滤器中为TAG_DISCOVERED指定,但在代码中尝试与ACTION_NDEF_DISCOVERED匹配。我在onResume中使用TAG_DISCOVERED测试了您的代码,没关系(Nexus 5)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句