Please enable JavaScript to view the comments powered by Disqus.

Use Implicit Intent to Call Another App

A - Preface

In Android development, we usually use Intent to navigate between Activities in one application. It’s called explicit intent because you have to pass the exactly class name of the destination Activity.

Intent can not only start another Activity inside the app but also call another application for user to perform an Action.

For example, instead of develop a module to send email, you can call Gmail app to do that for you! This kind of implementation can be found in a lot of application. And it saves you tons of time!

In this post, I’ll show you how to use implicit intent to call another app.

B - Create Implicit Intent

Unlike explicit intent, implecit intent does not declare the exactly class name of an Activity. It specifies the action that user want to do like view, edit, send or get something.

Implicit intents often also include data associated with user action, such as the map location you want to view, or the email message you want to send. Depend on user action, the data can be very simple like an URI, or complex and have many extra data like reciever, subject, message,…, or have no data required!

1. Implicit Intent with URI data

If your data is a URI, you can use a simple constructor to create an implicit intent.

For examples:

  • Intent to dial a phone number:
Uri number = Uri.parse("tel:11061991");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
  •  Intent to view a map:
// Map point based on latitude/longitude
Uri location = Uri.parse("geo:37.422219,-122.08364?z=14"); // z param is zoom level
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
  •  Intent to view a web page:
Uri webpage = Uri.parse("http://www.icetea09.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);

 2. Implicit Intent with Extra Data

Other kinds of implicit intents require “extra” data that provide different data types, such as a string.

By default, the system determines the appropriate MIME type required by an intent based on the Uri data that’s included.

If your intent doesn’t include an URI, you shoud specify the type of data associated with the in then by calling setType() method.

For examples:

Intent to send an email:

Intent emailIntent = new Intent(Intent.ACTION_SEND);

emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"trinhlbk1991@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://this/is/attachment/path"));

Intent to create a new calendar event:

Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);

Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 0, 19, 7, 30);
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 0, 19, 10, 30);

calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(Events.TITLE, "Ice Tea 09 new post!");
calendarIntent.putExtra(Events.EVENT_LOCATION, "An interesting post about Android");

C - Check If There is an App to Recieve the Intent

Although Android OS has some default application to recieve basic Intents such as Phone, Calendar, Email,… we should always perform a verification step before invoking an intent.

Because, if there’re no app recieve your intent, your app will be crashed!

The verification step is quite simple:

private boolean isIntentSafe(Intent intent){

	// Verify it resolves
	PackageManager packageManager = getPackageManager();
	List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
	return activities.size() > 0;

}

Usage:

if(isIntentSafe(callIntent))
	startActivity(callIntent);
else
	Toast.makeText(getApplicationContext(), "Your phone have no app can dial!", Toast.LENGTH_SHORT).show();

 D - Demo Result

intent

E - Source code Use Implicit Intent to Call Another App

https://drive.google.com/file/d/0Bw3dwdSezn6fMmppU0p5UmJCbGM/edit?usp=sharing