Editing Activity Permissions

Most Activity Action Intents fall into the category of requiring that the proper permission be set before Android will allow the action. As with many systems, Android just needs to make sure that only Activities with the correct credentials be allowed to perform actions with Activities that are outside of their base. Here are the available permissions:

• ACCESS_ASSISTED_GPS

• ACCESS_SURFACE_FLINGER

• ADD_SYSTEM_SERVICE

• DELETE_PACKAGES

• INSTALL PACKAGES

INTERNAL_SYSTEM_WINDOW

RAISED_THREAD_PRIORITY

READ_CONTACTS

READ_FRAME_BUFFER

RECEIVE_BOOT_COMPLETED

RECEIVE_SMS

RECEIVE_WAP_PUSH

RUN_INSTRUMENTATION

SET_ACTIVITY_WATCHER

SET_PREFERRED_ APPLICATIONS

SIGNAL_PERSISTENT_ PROCESSES

SYSTEM_ALERT_WINDOW WRITE_CONTACTS WRITE SETTINGS

Compare this list of permissions with the list of Intents in Table 7-1. You should find that most of the Intents match up with a corresponding permission. The CALL_ACTION Intent is no exception. You need to assign your Activity the CALL_PHONE permission to be able to execute your Intent.

To assign your Activity the correct permission, you first need to know what permission you need to assign. The current example is using the Dialer Activity. Access to the Dialer Activity is governed by the CALL_PHONE permission. By assigning this permission to your Activity, Android will let your Intent launch the Dialer Activity.

How do you add permissions to the Activity? You need to edit the Activity's Manifest. If you are using Eclipse, double-click AndroidManifest.xml. This opens the Android Manifest Overview window, shown in the following illustration.

AndroidPhoneDialer.j ;K main.xrnl CI AndroidPhoneDialer M KÏ

ci Android Manifest Overview

T General Information

Defines générai information about the AndroidManifest.xml Package andrcid_programmers_guide.AndrGidPhoneDialer

Shared User Id Signature

Browse,.

The content of the Android Manifest is made up of three sections.'You can also edit the XML directly,

CI Application: Activities, intent filters, providers, services and receivers. Cl Permission: Permissions defined and permissions used. G Instrumentation: Instrumentation defined, Ci XMl. Sourcei Directly edit the AndroidManifest.xml file.

Cl Documentation : Documentation from the Android SDKfor AndroidManifest.xml,

Overview Application Permissions Instrumentation AndroidManifest,xml

To edit the Activity's permissions, click the Permission link. This should take you to the Android Manifest Permissions window, shown in the following illustration.

This window lists the permissions that are currently assigned to your Activity. Given that you are working in a new project, you do not have any assigned permissions. Therefore, click the Add button to begin the process. In the dialog box that opens, select Uses Permission and click OK.

Back in the Android Manifest Permissions window, in the Name drop-down list, select android.permission.CALL_PHONE, as shown next. This adds the CALL_PHONE permission to your Activity.

Now that you have added the CALL_PHONE permission, take a look at AndroidManifest.xml. It should look similar to the following:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android=http://schemas.android.com/apk/res/android package="android_programmers_guide.AndroidPhoneDialer"> <application android:icon="@drawable/icon">

<activity android:name=".AndroidPhoneDialer" android:label="@string/app_name"> <intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>

<uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission></manifest>

The line of most interest is at the end of the file:

<uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission>

This line of code was added by the Android plugin for Eclipse. If you wanted to, you could have edited AndroidManifest.xml directly to assign the permission. However, if there are times when you are not sure what permission you need to add, or the syntax with which to add it, you can use the Manifest's wizard.

Now that the permission is in place, recompile and run your Activity. Your Emulator should now be making a phone call, as shown in the following illustration.

The Activity you created has used an Intent to launch the device's Call Activity and call the number 555-1212. This demonstrates how you can use Intents to your benefit. However, this application does little for you practically. That is to say, how practical would it be to launch an Activity with a hard-coded phone number, just to make a call?

In the following section, you are going to make your application more practical, by adding both a button, to initiate the launching of the Call_Action Intent, and a textbox, to allow the user to input a phone number of their choosing.

This section shows you how to modify your AndroidPhoneDialer to make it a bit more practical by adding a few features. By the end of this section, you should be pretty comfortable using not only Intents, but EditTexts and Buttons.

If you did not follow along with the project from the last section, go back and create that Activity. The tutorial in this section assumes that you already have the completed

This section shows you how to modify your project to include a Button. Instead of launching the Intent when the Activity is started, it will be launched by the Button. With the exception of text, buttons are the most prevalent type of object on an application. Buttons form the main interaction between users and applications. Learning how to create and utilize buttons in Android is essential to creating a practical, user-friendly Activity.

You are going to create the Button in main.xml. Think back to Chapter 5, in which you created the TextView for your Hello World! Activity. The TextView had a distinct structure to it, something like this:

what you want the View to look like. You still need to assign functionality to it in AndroidPhoneDialer.java.

<View android:id=<id> android:layout_width=< width> android:layout_height=<height>

0 0

Post a comment