- Near Field Communication with Android Cookbook
- Vitor Subtil
- 255字
- 2021-07-16 11:37:21
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:
- Open Eclipse and create a new Android application project named
NfcBookCh1Example1
and a package namednfcbook.ch1.example1
, as shown in the following screenshot: - Make sure you select API 10: Android 2.3.3 (GingerBread) in the Minimum Required SDK field.
- When prompted to create an activity, select Blank Activity.
- 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.
- 程序員修煉之道:從小工到專家
- Mastering Ninject for Dependency Injection
- 企業(yè)級數(shù)據(jù)與AI項(xiàng)目成功之道
- 網(wǎng)站數(shù)據(jù)庫技術(shù)
- 從0到1:JavaScript 快速上手
- 圖數(shù)據(jù)實(shí)戰(zhàn):用圖思維和圖技術(shù)解決復(fù)雜問題
- Hadoop大數(shù)據(jù)開發(fā)案例教程與項(xiàng)目實(shí)戰(zhàn)(在線實(shí)驗(yàn)+在線自測)
- 新手學(xué)會計(jì)(2013-2014實(shí)戰(zhàn)升級版)
- 數(shù)據(jù)庫應(yīng)用系統(tǒng)技術(shù)
- 代碼的未來
- 一類智能優(yōu)化算法的改進(jìn)及應(yīng)用研究
- AI Crash Course
- 從零進(jìn)階!數(shù)據(jù)分析的統(tǒng)計(jì)基礎(chǔ)(第2版)
- 實(shí)用預(yù)測分析
- Kafka權(quán)威指南(第2版)