官术网_书友最值得收藏!

Requesting NFC permissions

As Android developers, we are accustomed to using the manifest file to request permission to use a specific device feature, and NFC is no exception to this.

Getting ready

Make sure you have an NFC-enabled Android device or a virtual test environment—refer to the Testing your app all together recipe.

How to do it…

We will start by creating an Android project where we will request the correct permissions, as shown in the following steps:

  1. Open Eclipse and create a new Android application project named NfcBookCh1Example1 and a package named nfcbook.ch1.example1, as shown in the following screenshot:
    How to do it…
  2. Make sure you select API 10: Android 2.3.3 (GingerBread) in the Minimum Required SDK field.
  3. When prompted to create an activity, select Blank Activity.
    How to do it…
  4. Open the AndroidManifest.xml file located in the project's root and add the following code just before the application node:
    <uses-permission android:name="android.permission.NFC"/>

How it works…

Android requires every app to request permissions from the user to allow the application to perform restricted actions. Examples of restricted actions include access to users' current location, permission to send an SMS without user interaction permission to, read contacts, and many others. This is done in the AndroidManifest.xml file using the <uses-permission/> node. Permissions are granted by the user when the application is installed. Requesting unnecessary permissions may cause users to not trust the application and refrain from installation. If we do not request permissions in the manifest and try to do restricted actions, an exception is thrown and the application will not work correctly.

主站蜘蛛池模板: 小金县| 庆安县| 浪卡子县| 淳化县| 遂平县| 峨山| 闸北区| 巴东县| 吉木萨尔县| 郴州市| 遂溪县| 铜川市| 砚山县| 称多县| 隆德县| 桂林市| 临澧县| 丽江市| 陆川县| 饶河县| 镶黄旗| 秭归县| 逊克县| 鹤庆县| 灌云县| 叶城县| 余江县| 彰化市| 新丰县| 呼和浩特市| 轮台县| 建平县| 焉耆| 东乡| 眉山市| 乌审旗| 江油市| 讷河市| 漳浦县| 尉犁县| 乐亭县|