Taste of Android :: Part-5


Bhaskar S 05/19/2013


Android Application Development - IV

In Part-4 we used Intent to trigger an event in the Android system to navigate between activities within our application.

Interestingly, Intents can also be used to launch built-in applications that come with the Android device. This is a very powerful and useful concept.

Before we demonstrate the ability to launch built-in applications, let us understand the basics of an Intent.

An Intent is a passive object that encapsulates some information which the Android system can use to determine which component in the Android system should handle the Intent. Once the appropriate component is identified, that component will then act accordingly based on the information in the Intent.

The following are the information parts in an Intent:

Let us create a new Android application named DroidAppLauncher to illustrate the concept of launching built-in Android applications using an Intents. This application will display a screen with four buttons - one to launch the browser app, second to launch the calculator app, third to launch the calendar app, and the last one to exit the application. We will not go step-by-step to show the various screens since we already did that in Part-2 for the DroidTipCalculator application.

Modify the contents of the dimens.xml file to look like the one shown in the listing 5.1 below:

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

<dimen name="title_size">26sp</dimen>

</resources>

Next, modify the contents of the strings.xml file to look like the one shown in the listing 5.2 below:

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

<!-- id references -->

<string name="button1">button1</string>
<string name="button2">button2</string>
<string name="button3">button3</string>
<string name="button4">button4</string>

<!-- text references -->

<string name="app_name">DroidBuiltInApps</string>
<string name="intent_title">Built-In Apps Launcher</string>
<string name="action_settings">Settings</string>
<string name="browser">Launch Browser</string>
<string name="calculator">Launch Calculator</string>
<string name="calendar">Launch Calendar</string>
<string name="exit">Exit</string>

</resources>

The contents of the activity_launcher.xml layout file will look like the one shown in the listing 5.3 below:

Listing-5.3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/intent_title"
android:textColor="#cc3300"
android:textSize="@dimen/title_size"
android:textStyle="bold"
android:typeface="sans" />

<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/browser" />

<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/calculator" />

<Button
android:id="@+id/button3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/calendar" />

<Button
android:id="@+id/button4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/exit" />

</LinearLayout>

Previewing the activity_launcher.xml layout file in the Graphical Mode in Eclipse will look like the one shown in the figure 5.1 below:

Activity One Image
Figure-5.1

The contents of the java source file LauncherActivity.java will look like the one shown in the listing 5.4 below:

Listing-5.4
package com.polarsparc.android.droidapplauncher;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
import android.content.ComponentName;
import android.widget.Button;

public class LauncherActivity extends Activity {
final Activity act = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);

// Launch Browser button
Button browserBtn = (Button) findViewById(R.id.button1);

/*
* When we click the Launch Browser button, we want to launch the android
* built-in browser application
*/
browserBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));

startActivity(intent);
}
});

// Launch Calculator button
Button calculatorBtn = (Button) findViewById(R.id.button2);

/*
* When we click the Launch Calculator button, we want to launch the android
* built-in calculator application
*/
calculatorBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String CALCULATOR_PACKAGE = "com.android.calculator2";
String CALCULATOR_CLASS = "com.android.calculator2.Calculator";

Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(CALCULATOR_PACKAGE, CALCULATOR_CLASS));

startActivity(intent);
}
});

// Launch Calendar button
Button calendarBtn = (Button) findViewById(R.id.button3);

/*
* When we click the Launch Calendar button, we want to launch the android
* built-in calendar application
*/
calendarBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String CALENDAR_PACKAGE = "com.android.calendar";
String CALENDAR_CLASS = "com.android.calendar.LaunchActivity";

Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(CALENDAR_PACKAGE, CALENDAR_CLASS));

startActivity(intent);
}
});

// Exit button
Button exitBtn = (Button) findViewById(R.id.button4);

/*
* When we click the Exit button, we want to exit the application
*/
exitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
act.finish();
}
});
}

}

When the DroidAppLauncher application starts up, the user is presented with the main launcher screen as shown in the following figure 5.2 below:

App Launcher Image
Figure-5.2

When the user clicks on the Launch Browser button, we create an instance of the Intent object. Next, we set the action to be the pre-defined generic Android action Intent.ACTION_VIEW by calling the method setAction(). Since the action indicates we desire to view some data, we need to provide some data on what we want to view. For this, we set the URI data as the URL of the Google home page http://www.google.com by calling the method setData(). Finally, we send our Intent to teh Android system by calling the method startActivity(). Android system will the launch the built-in browser application as shown in the following figure 5.3 below:

Browser Launched Image
Figure-5.3

To exit the Browser application and go back to the main launcher screen, click on the BACK button as shown in the following figure 5.4 below:

Browser Launched Image
Figure-5.4

When the user clicks on the Launch Calculator button, we create an instance of the Intent object. Next, we set the action to be the pre-defined generic Android action Intent.ACTION_MAIN by calling the method setAction(). This action indicates we desire to start the main screen of the application. Next, we set the category to be the pre-defined generic Android category Intent.CATEGORY_LAUNCHER by calling the method setCategory(). Next, we set the component name (class and package) of the Calculator application by calling the method setComponent(). Finally, we send our Intent to the Android system by calling the method startActivity(). Android system will the launch the built-in calculator application as shown in the following figure 5.5 below:

Calculator Launched Image
Figure-5.5

To exit the Calculator application and go back to the main launcher screen, click on the BACK button.

When the user clicks on the Launch Calendar button, we perform similar steps as we did to launch the Calculator application. Android system will the launch the built-in calendar application as shown in the following figure 5.6 below:

Calendar Launched Image
Figure-5.6

To exit the Calendar application and go back to the main launcher screen, click on the BACK button.

Finally, when the user clicks on the Exit button, we exit the DroidAppLauncher application.


References

Taste of Android :: Part-1

Taste of Android :: Part-2

Taste of Android :: Part-3

Taste of Android :: Part-4