24 September, 2018


Related Blogs

Before you can begin to interact with the phone dialer, you need to understand the type of code that you will use to do the job. Android uses Intents to do specific jobs within applications. Once you master the use of Intents, a whole new world of application development will be open to you. This section defines what an Intent is and how it is used. An Intent is Android’s method for relaying certain information from one Activity to another. An Intent, in simpler terms, expresses to Android your intent to do something. You can think of an Intent as a message passed between Activities. For example, assume that you have an Activity that needs to open a web browser and display a page on your Android device. Your Activity would send an “intent to open x page in the web browser,” known as a WEB_SEARCH_ACTION Intent, to the Android Intent Resolver. The Intent Resolver parses through a list of Activities and chooses the one that would best match your Intent; in this case, the Web Browser Activity. The Intent Resolver then passes your page to the web browser and starts the Web Browser Activity. Intents are broken up into two main categories:  

  • Activity Action Intents Intents used to call Activities outside of your application. Only one Activity

can handle the Intent. For example, for a web browser, you need to open the Web Browser Activity to display a page.  

  • Broadcast Intents Intents that are sent out for multiple Activities to handle. An example of a Broadcast

Intent would be a message sent out by Android about the current battery level. Any Activity can process this Intent and react accordingly—for example, cancel an Activity if the battery level is below a certain point. the current Activity Action Intents that are available to you. As you’ll notice, in most cases, the name of the Intent does a good job of describing what that Intent does. 16 17 The Intent is only one-third of the picture. An Intent is really just that, an intent to do something; an Intent cannot actually do anything by itself. You need Intent Filters and Intent Receivers to listen for, and interpret, the Intents. An Intent Receiver is like the mailbox of an Activity. The Intent Receiver is used to allow an Activity to receive the specified Intent. Using the previous web browser example, the Web Browser Activity is set up to receive web browser Intents. A system like this allows unrelated Activities to ignore Intents that they would not be able to process. It also allows Activities that need the assistance of another Activity to utilize that Activity without needing to know how to call it. With Intents and Intent Receivers, one Activity can send out an Intent and another can receive it. However, there needs to be something that governs the type of information that can be sent between the two Activities. This is where Intent Filters come in. Intent Filters are used by Activities to describe the types of Intents they want to receive. More importantly, they outline the type of data that should be passed with the Intent. Therefore, in our example scenario, we want the web browser to open a web page. The Intent Filter would state that the data passed with the WEB_SEARCH_ACTION Intent should be in the form of a URL. In the next section, you will begin to use Intents to open and utilize the phone’s dialer. Using the Dialer Now that you know what an Intent is, it is time to see one in action. This section shows you how to use the DIAL_ACTION Intent to open the phone dialer. You will pass a telephone number with your Intent. If your application works correctly, you should see displayed in the dialer the number you pass with your Intent. The first step is to create a new project for this Activity.   Name the project AndroidPhoneDialer. The following illustration shows the New Android Project wizard for this project. With your new application open in Eclipse, the first order of business is to remove the TextView from main.xml that contains that Hello World statement. The main.xml file should look like this after you remove the TextView:   <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout>   You need to add two new packages to your project to utilize the DIAL_ACTION Intent, as follows. The first package allows you to set up Intents and the second allows you to parse URIs. import android.content.Intent; import;   NOTE There are several different Intent Filters on the DIAL_ACTION Intent that you can use. You are using the Filter that lets you pass a phone number as a URI. The next step is to create your Intent. The syntax for creating an Intent is as follows: Intent <intent_name> = new Intent(<Android_Intent>,<data>) For your application, replace the first parameter, <intent_name>, with DialIntent. To get the value for the second parameter, <Android_Intent>, refer to the list of Activity Actions in Table 7-1. You’ll find that, to call the dialer, you need to use the DIAL_ACTION Intent. To call the Intent properly, use the format Intent.DIAL_ACTION. The last parameter, <data>, is the phone number. The DIAL_ACTION Intent takes in data as a URI. Thus, you need to use Uri.parse to parse out your phone number. Using Uri.parse will ensure that the DIAL_ACTION Intent understands the number you are trying to dial. You pass Uri.parse a string that represents the phone number you want to dial, "tel:5551212" in this example. The final call to create an Intent for your project should look like this: Intent DialIntent = new Intent(Intent.DIAL_ACTION,Uri.parse("tel:5551212"));   TIP You use the notation tel:<phone_number> to dial a specific phone number. You can also use voicemail: instead of tel: to dial the phone’s voicemail shortcut. With the Intent created, you now have to tell Android that you want the dialer to be launched in a new Activity. To do this, you use the setLaunchFlags( ) method of the Intent. You must pass setLaunchFlags( ) the appropriate parameter for launching. The following is a list of the possible launch flags that you can set:   NOTE In some cases, more than one of the following flags may be set to accomplish the desired outcome.  

  • NO_HISTORY_LAUNCH Launches the Activity without recording it in the system’s launch history
  • SINGLE_TOP_LAUNCH Tells the system not to launch the Activity if it is already running
  • NEW_TASK_LAUNCH Launches the Activity
  • MULTIPLE_TASK_LAUNCH Launches the Activity even if it is already running
  • FORWARD_RESULT_LAUNCH Allows the new Activity to receive results that would normally be

forwarded to the existing Activity In this case, you want to use Intent.NEW_TASK_LAUNCH, which simply lets you open a new instance of the dialer Activity:   DialIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH ); The last step to creating your dialer Intent is to actually launch the Activity. (More accurately, you are telling Android that you have an intent to launch the dialer as a New Task. It is ultimately up to Android to launch the Dialer Activity.) To tell Android that you want to start the dialer, you need to use startActivity( ): startActivity(DialIntent);   Notice that you pass to startActivity( ) your Intent. The Intent is then passed to Android, and the action is resolved. The full code for should look like this: package sk.AndroidPhoneDialer; import; import android.content.Intent; import android.os.Bundle; import; public class AndroidPhoneDialer extends Activity { /** Called when the Activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);   /** Create our Intent to call the Dialer */ /** Pass the Dialer the number 5551212 */ Intent DialIntent = new Intent(Intent.DIAL_ACTION,Uri.parse("tel:5551212")); /** Use NEW_TASK_LAUNCH to launch the Dialer Activity */ DialIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH ); /** Finally start the Activity */ startActivity(DialIntent); } } Placing a Call from Your Activity In this section you will learn what Intent to add to your Activity when calling the dialer. You will also learn where to add your chosen Intent in the Activity’s code. Further, you will learn how to parse the intended phone number as a URI. You need to make a few changes to your code to move from the Dialer Activity to the Call Activity. In this section, you are going to edit your AndroidPhoneDialer Activity to place a call after opening the dialer. Adding the Intent to Your Activity You still need the Intent and Uri packages—shown here—so leave those in place at the header of your file. import android.content.Intent; import; These packages will enable you to not only instantiate the Intent that you need, but also pass the needed telephone number data to the Intent (with the Uri package).   TIP If you are flipping through the chapters out of order, and did not work on the project in the previous section, simply create a new project, name it AndroidPhoneDialer, and add the previous two packages to

  1. That will catch you up to speed. True to its name, the Intent that you need in your Activity is

CALL_ACTION. In much the same way that DIAL_ACTION opened the Android dialer, CALL_ACTION will launch the phone’s calling process and initiate a call to the supplied number. To create the Intent, use the same procedure as you did for the dialer, only this time call CALL_ACTION: Intent CallIntent = new Intent(Intent.CALL_ACTION,Uri.parse("tel:5551212"));   Notice that you use Uri.parse to pass a correctly parsed telephone number to the Activity. The next step is to tell Android that you want to set this Activity to launch, and then launch it. This is accomplished using the following two lines of code. CallIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH ); startActivity(CallIntent);   In the first line, you sent the launch flag to NEW_TASK_LAUNCH. This launches a new instance of the Call Activity. Finally, you tell Android to start the Activity using your Intent. When finished, your file should look like this. package android_programmers_guide.AndroidPhoneDialer; import; import android.content.Intent; import android.os.Bundle; import; public class AndroidPhoneDialer extends Activity { /** Called when the Activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); /** Create our Intent to call the device's Call Activity */ /** Pass the Call the number 5551212 */ Intent CallIntent = new Intent(Intent.CALL_ACTION,Uri.parse("tel:5551212")); /** Use NEW_TASK_LAUNCH to launch the Call Activity */ CallIntent.setLaunchFlags(Intent.NEW_TASK_LAUNCH ); /** Finally start the Activity */ startActivity(CallIntent); } } 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: 15   Building the Activities Up to now, you have created only single-Activity applications. This is to say, you have created rather simplistic applications that encompass only one “screen” of data. Take a minute, and think of the last few applications you have used. Chances are, they used more than one “window.” Most applications use multiple windows to gather, display, and save data. Your Android applications should be no different. Although you have not yet learned how to create multiple-Activity applications that run on Android, you got a hint about how to leverage multiple Activities in the last chapter. You used a new concept called Intents to call—and run—a core Android Activity. While the concept still holds true in this chapter, the execution is slightly different when you want to call Activities that you have created, as opposed to calling core Android Activities. The first thing you need to do is build the Activities. Then you can create the Intents that will call them. When building the Activities, you need to follow a three-step process.

  • Intent code for the .xml file
  • Intent code for the .java file
  • Calling Activities using an Intent

Once you create your first additional Activity, the rest should come very easily.   NOTE These steps are not bound to each other. You can perform them in any order. Intent Code for the .xml File Remember that all Android Activities comprise three main parts: the .java file that contains the code, the .xml file that holds the layout, and the package’s Manifest. To this point in the book, you have only used main.xml to control the layout of a single Activity. However, to take advantage of having multiple Activities, you must have multiple .xml layout files. To create a new .xml file, open your Eclipse project and navigate to the Package Explorer. Open the res directory, right-click the layout folder, and choose New | File. The layout file is created, but it is empty. To get the Activity off on the right foot, add the following code to test.xml. This code will provide a base for your layout. If you need to, you can simply copy this code from the existing main.xml file.   <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout>   Intent Code for the .java File Using the Package Explorer again, navigate to the src directory, open it, and right-click the android_programmers_guide.AndroidViews package, as shown in the following illustration. Once again, you are going to add a new file to the folder. After you right-click the AndroidViews package, select New | File from the context menu. This file will hold all the code for the second Activity in this project. Name the file You should now have a nice, new (but empty) .java file. You just need to add a few lines of code to the file to make it usable: package testPackage.test; import; import android.os.Bundle; public class test extends Activity { /** Called when the Activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.test); /** This is our Test Activity All code goes below */ } } Modifying the AndroidManifest.xml Open your AndroidManifest.xml file in Eclipse. AndroidManifest.xml contains the global settings for your project. More importantly, AndroidManifest.xml also contains the Intent Filters for your project. The information that facilitates this process is kept in AndroidManifest.xml.   NOTE There is only one AndroidManifest.xml file per project. If your AndroidManifest.xml file is currently open, it should appear as follows: <activity android:name=".AndroidViews" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> What you are looking at here is the Intent Filter for the AndroidViews Activity, the main Activity that was created with the project. To this file you can add any other Intent Filters that you want your project to handle. In this case, you want to add an Intent Filter that will handle the new Test Activity that you created. The following is the code for the Intent Filter that you need to add to the   AndroidManifest.xml file: <activity android:name=".Test" android:label="Test Activity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>   Adding this code to AndroidManifest.xml enables Android to pass Intents for the Test Activity to the correct place. The full AndroidManifest.xml file should look like this:   <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android= package="android_programmers_guide.AndroidViews"> <application android:icon="@drawable/icon"> <activity android:name=".AndroidViews" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".AutoComplete" android:label="AutoComplete"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>    

About Author


Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .