Launching external applications in Android

Download source code of this article.

In Android we use intent to start an activity, start a service, send broadcasts, etc. Intent is used for late runtime binding of applications. In Android intent represents the action to be performed. There are two primary information in an intent, they are action and data. Action attribute represents the action to be performed such as edit, view, dial, etc. The data attribute is the data to be operated on. Some of the common actions are ACTION_VIEW, ACTION_DIAL, ACTION_EDIT, ACTION_PICK, ACTION_SEND, ACTION_SEARCH, ACTION_CALL, etc. The following code snippets show some of the examples:

Start the email client

final String[] addressList = new String[] { toAddress };

final Intent email_intent = new Intent(android.content.Intent.ACTION_SEND);
email_intent.setType("message/rfc822");
email_intent.putExtra(android.content.Intent.EXTRA_EMAIL, addressList);
email_intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample mail");
email_intent.putExtra(android.content.Intent.EXTRA_TEXT, "Sample mail for testing purpose");

startActivity(Intent.createChooser(email_intent, "Send mail..."));

Dial a number

// needs permission android.permission.CALL_PHONE
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel://123456789"));

startActivity(intent);

Start a web search

Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);

intent.putExtra(SearchManager.QUERY, "Krishnaraj Varma");

startActivity(intent);

For a more detailed information about intent and intent action follow this link

There is another method to start an application using PackageManager class. This class has a method getLaunchIntentForPackage which will return the LAUNCHER intent of the specified package or null if not found. We can use this returned intent to start the application. The following code snippet starts the example application of this article:

PackageManager packageManager = getPackageManager();
String packageName = "com.varma.samples.applauncher";

try
{
	Intent intent = packageManager.getLaunchIntentForPackage(packageName);

	if(null != intent)
	{
		startActivity(intent);
	}

}
catch (NameNotFoundException e)
{
	e.printStackTrace();
}
catch (ActivityNotFoundException e)
{
	e.printStackTrace();
}

To enumerate all the installed application packages, we can use getInstalledApplications method of the PackageManager class. This method returns a list of ApplicationInfo class. The ApplicationInfo class contains information regarding and application such as package name, label, icon, etc. This information comes from the AndroidManifest.xml file of the particular application.

The example application lists all the installed applications in a list view and when you click on an item, the application will be launched if a LAUNCHER intent is found.

1 Comment

Add yours →

  1. 10x very much is was really helpful

Leave a Reply