Asking the User for Permission

You wouldn't want your next-door neighbor storing his holiday decorations in your storage shed without clearing it through you first, would you? I didn't think so! Android is no different — storing data anywhere on the device requires some sort of permission from the user. But that's not the only thing that requires some sort of permission.

Seeing how permissions affect the user experience

When users install applications from the Android Market, the application's manifest file is inspected for required permissions that the application needs to operate. Anytime your application needs access to sensitive components, such as external storage, access to the Internet, phone device information, and so on, the user is notified that the application would like to access these components. The user decides whether she would like to install the application.

If your application requests a lot of unnecessary permissions, the user may get suspicious and choose not to install the application. Imagine if the Screen Brightness Toggle application (built previously in this book) was requesting your current GPS location, needed access to the Internet, and wanted to know information about the device (such as hardware info). The Screen Brightness Toggle application has no need for those permissions. Users tend to be wary of installing an application that is overzealously requesting permissions.

image Through the many different applications that I've published, I've found that the fewer number of permissions your application requests, the more likely the user is to install your application. If your application does not need the permission, yank the permission out of the application.

Setting requested permissions in the AndroidManifest.xml file

When you need to request permissions, you need to add them to the AndroidManifest.xml file in your project. No permission is necessary to work with an SQLite database; therefore, I'm going to add two permissions to the Task Reminder application that will be required when I add the alarm manager code in Chapter 15:

  • android.permission.RECEIVE_BOOT_COMPLETED: This permission allows the application access to know when the tablet reboots.
  • android.permission.WAKE_LOCK: This permission keeps the tablet “awake” while it performs some background processing.

These items are covered in detail, along with the AlarmManager, in Chapter 15.

The RECEIVE_BOOT_COMPLETED and WAKE_LOCK permissions mentioned above are unique and are used infrequently — so here I outline a couple of the most common permissions and describe how to set them. A lot of applications require access to the Internet to operate. Some applications also need to write data to the SD Card. If you need either of these, add the following permissions:

  • Internet: android.permission.INTERNET
  • SD Card: android.permission.WRITE_EXTERNAL_STORAGE

You can add permissions to the AndroidManifest.xml file in one of two ways:

  • Through the AndroidManifest.xml Permissions Editor. Choose AddimageUses Permission and then choose the permission from the drop-down list.
  • Through manually editing the XML file. This method is how I prefer to do it. You need to add a uses-permission element to the manifest element. The XML permission request looks like this:
<uses-permission android:name=“android.permission.WAKE_LOCK” />

If you have not done so already, add the WAKE_LOCK and RECEIVE_BOOT_COMPLETED permissions to the Task Reminder application. To view a full list of available permissions, view the Android permission documentation here: http://d.android.com/reference/android/Manifest.permission.html.

image If you do not declare the permissions your application needs and a user installs your application on his device, your application will not function as expected; sometimes run-time exceptions will be thrown and crash your application. Always be sure to check that your permissions are present. Most likely, they will be because the app will not work on a device or emulator if they are not.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.26.231