O'Reilly logo

Learning Android by Marko Gargenta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding Custom Permissions to Send and Receive Broadcasts

As discussed in Updating the Manifest File for Internet Permission, an application must be granted permissions to access certain restricted features of the system, such as connecting to the Internet, sending SMS messages, making phone calls, reading the user’s contacts, taking photos, and so on. The user has to grant all or none of the permissions to the application at installation time, and it is the job of the application developer to list all the permissions the app needs by adding the <uses-permission> element to the manifest file. So far, we’ve added permissions to Yamba in order to access the Internet, kick off our boot-time service, and learn about network changes.

But now that we have our Updater service sending a broadcast action to our Timeline receiver, we might want to restrict permission to send and receive that broadcast to our own app. Otherwise, another app, knowing what our action looks like, could send it and cause actions in our application that we didn’t intend.

To fill up this security hole, we define our own permission and ask the user to grant it to the Yamba application. Next, we’ll enforce both sending and receiving the permissions.

Declaring Permissions in the Manifest File

The first step is to declare our permissions, explaining what they are, how they are to be used, and setting their protection level, shown in Example 11-11.

Example 11-11. Adding permissions to manifest file

<manifest>

  ...
  <!--  --> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required