Nowadays, many applications that hold onto contact data secure that data by requiring that a user explicitly grant rights for other programs to access the contact information. Those rights could be granted on a case-by-case basis or once at install time.
Android is no different, in that it requires permissions for applications to read or write contact data. Android’s permission system is useful well beyond contact data, and for content providers and services beyond those supplied by the Android framework.
You, as an Android developer, will frequently need to ensure your applications have the appropriate permissions to do what you want to do with other applications’ data. You may also elect to require permissions for other applications to use your data or services, if you make those available to other Android components. This chapter covers how to accomplish both these ends.
Mother, May I?
Requesting the use of other applications’ data or services requires the uses-permission
element to be added to your AndroidManifest.xml
file. Your manifest may have zero or more uses- permission elements, all as direct children of the root manifest
element.
The uses-permission
element takes a single attribute, android:name
, which is the name of the permission your application requires:
<uses-permission
android:name='android.permission.ACCESS LOCATION' />
The stock system permissions all begin with android.permission
and are listed in the Android SDK documentation for Manifest.permission
. Third-party applications may have their own permissions, which hopefully they have documented for you. Here are some of the more important built-in permissions:
• INTERNET
, if your application wishes to access the Internet through any means, from raw Java sockets through the WebView
widget
• READ CALENDAR
, READ CONTACTS
, and the like for reading data out of the built-in content providers
• WRITE CALENDAR
, WRITE CONTACTS
, and the like for modifying data in the built-in content providers
Permissions are confirmed at the time the application is installed — the user will be prompted to confirm it is OK for your application to do what the permission calls for. This prompt is not available in the current emulator, however.
If you do not have the desired permission and you try to do something that needs it, you may get a SecurityException
informing you of the missing permission, but this is not a guarantee — failures may come in other forms, depending on if something else is catching and trying to handle that exception.
Halt! Who Goes There?
The other side of the coin, of course, is to secure your own application. If your application is merely activities and intent receivers, security may be just an
Note that the issue here is less about whether other applications might “mess up” your data, and more about privacy of the user’s information or use of services that might incur expense. That is where the stock permissions for built-in Android applications are focused — can you read or modify contacts, can you send SMS messages, etc. If your application does not store information that might be considered private, security is less of an issue. If, on the other hand, your application stores private data, such as medical information, security is much more important.
The first step to securing your own application using permissions is to declare said permissions, once again in the AndroidManifest.xml
file. In this case, instead of uses-permission
, you add permission
elements. Once again, you can have zero or more permission
elements, all as direct children of the root manifest
element.
Declaring a permission is slightly more complicated than using a permission. There are three pieces of information you need to supply:
1. The symbolic name of the permission. To keep your permissions from colliding with those from other applications, you should use your application’s Java namespace as a prefix.
2. A label for the permission: something short that is understandable by users.
3. A description for the permission: something a wee bit longer that is understandable by your users.
<permission
android:name='vnd.tlagency.sekrits.SEE SEKRITS'
android:label='@string/see sekrits label'
android:description='@string/see sekrits description' />
This does not enforce the permission. Rather, it indicates that it is a possible permission; your application must still flag security violations as they occur.
There are two ways for your application to enforce permissions, dictating where and under what circumstances they are required. You can enforce permissions in your code, but the easier option is to indicate in the manifest where permissions are required.
Enforcing Permissions via the Manifest
Activities, services, and intent receivers can all declare an attribute named android:permission
, whose value is the name of the permission that is required to access those items:
<activity
android:name='.SekritApp'
android:label='Top Sekrit'
android:permission='vnd.tlagency.sekrits.SEE SEKRITS'>
<intent-filter>
<action android:name='android.intent.action.MAIN' />
<category
android:name='android.intent.category.LAUNCHER' />
</intent-filter>
</activity>
Only applications that have requested your indicated permission will be able to access the secured component. In this case, “access” means the following:
• Activities cannot be started without the permission.
• Services cannot be started, stopped, or bound to an activity without the permission.
• Intent receivers ignore messages sent via sendBroadcast()
unless the sender has the permission.