Checking NFC feature on Android Device

牧云@^-^@ 提交于 2019-12-23 01:12:58

问题


This is in response to question

Step #1: Put this in your manifest:

<uses-feature android:name="android.hardware.nfc" android:required="false" />
Step #2: Call hasSystemFeature(PackageManager.FEATURE_NFC) on PackageManager to see if NFC is available on the current device

i set the API level to 8,Step 1 is ok, but when I write hasSystemFeature(PackageManager.FEATURE_NFC) it says Feature_NFC doesnot resolve to a field,Feature_WIFI FEATURE_BLUETOOTH FEATURE_CAMERA are the options there. I have visited Android developer Site,but they didn't mention the API level. Another question is Can I test NFC application on AVD? Anyone who can help me this Thanks


回答1:


Make sure you are setting project build target to at least api level 9.

Include folliwng in manifest.xml

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" /> //If you have icecream sandwich installed.

It will ensure that your application will be runnable on sdk 8 also

Also make sure whenever you are calling Gingerbread specific features then wrap them into if condition as follows.

      if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {

    } else {
        if (getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_NFC)) {
            //do processing
        }
    }

For testing You can give a try to Open NFC Android emulator




回答2:


FEATURE_NFC is defined from API Level 9, but you can still use the constant value "android.hardware.nfc".



来源:https://stackoverflow.com/questions/11070492/checking-nfc-feature-on-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!