Launching Different Applications from Android Activity Using Intent

As you might have gone through android till now,  the architecture is based on Events and Event Handlers i.e Intents and Their Corresponding Activities. Calling multiple activities in side an application is done using Intents . Now we shall see how to call different applications using Intents (Its very simple and straight forward)

To open other people’s application, you need to make sure that in their manifest file, the author specify the class to have the android.intent.action.MAIN intent-filter added to them.
final Intent intent = new Intent(Intent.ACTION_MAIN, null);

We then add category that this new intent will be launching something
intent.addCategory(Intent.CATEGORY_LAUNCHER);

Then we get identify the application we need to open by using ComponentName, here you specify the package name of the application as first argument and the class we want to open as the second one. You must understand that com.android.settings has a lot of classes that have Main intent-filter under it making the second argument to be the specific class that we need. (this is more than one line)
final ComponentName cn = new ComponentName(“com.android.settings”, “com.android.settings.fuelgauge.PowerUsageSummary”);

After we identify the component we want, we set it to our intent
intent.setComponent(cn);

We then tell the intent that open opening this one make it as a new task
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Then finally start our intent
startActivity( intent);

 

Original article  is at http://www.tutorialforandroid.com/2009/10/launching-other-application-using-code.html

Following is the sample Application that is created to Show this feature


package com.linkwithweb.app;

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

public class LaunchApp extends Activity {
	private Button showPowerButton;
	private Button launchLessonsButton;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		showPowerButton = (Button) findViewById(R.id.showPowerButton);

		showPowerButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				final Intent intent = new Intent(Intent.ACTION_MAIN, null);

				intent.addCategory(Intent.CATEGORY_LAUNCHER);

				final ComponentName cn = new ComponentName(
						"com.android.settings",
						"com.android.settings.fuelgauge.PowerUsageSummary");

				intent.setComponent(cn);

				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				startActivity(intent);

			}
		});

		launchLessonsButton = (Button) findViewById(R.id.launchLessonsButton);

		launchLessonsButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				final Intent intent = new Intent(Intent.ACTION_MAIN, null);

				intent.addCategory(Intent.CATEGORY_LAUNCHER);

				final ComponentName cn = new ComponentName("com.linkwithweb",
						"com.linkwithweb.AndroidLessonsMain");

				intent.setComponent(cn);

				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				startActivity(intent);
			}
		});

	}
}

Code has been uploaded following svn link
https://linkwithweb.googlecode.com/svn/trunk/Android

Advertisements

5 thoughts on “Launching Different Applications from Android Activity Using Intent

  1. hi
    Thanks for your tutorial.
    I have tried with your sample. But am getting
    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.settings/com.android.settings.fuelgauge.PowerUsageSummary}; have you declared this activity in your AndroidManifest.xml?
    this exception.
    can u please tell what should i do to remove this exception.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s