-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Common Implicit Intents
Let's take a look at the most common implicit intents such as making a phone call, launching a web address, sending an email, etc.
Permissions:
<uses-permission android:name="android.permission.CALL_PHONE" />
Intent:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:0377778888"));
if (callIntent.resolveActivity(getPackageManager()) != null) {
startActivity(callIntent);
}
val callIntent = Intent(Intent.ACTION_CALL)
callIntent.data = Uri.parse("tel:0377778888")
if (callIntent.resolveActivity(packageManager) != null) {
startActivity(callIntent)
}
Caution It's possible that a user won't have any apps that handle the implicit intent you send to startActivity(). If that happens, the call will fail and your app will crash. To verify that an activity will receive the intent, call resolveActivity() on your Intent object. If the result is non-null, then there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that issue the intent.
Compose an email in the phone email client:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "some@email.address" });
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(intent, ""));
}
val intent = Intent(Intent.ACTION_SEND)
intent.type = "plain/text"
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("some@email.address"))
intent.putExtra(Intent.EXTRA_SUBJECT, "subject")
intent.putExtra(Intent.EXTRA_TEXT, "mail body")
if (intent.resolveActivity(packageManager) != null) {
startActivity(Intent.createChooser(intent, ""))
}
Gmail does not examine the extra Intent fields, so in order to use this intent, you need to use the Intent.ACTION_SENDTO
and pass a mailto:
URI with the subject and body URL encoded.
String uriText =
"mailto:youremail@gmail.com" +
"?subject=" + Uri.encode("some subject text here") +
"&body=" + Uri.encode("some text here");
Uri uri = Uri.parse(uriText);
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(uri);
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(sendIntent, "Send email"));
}
val uriText = "mailto:youremail@gmail.com" +
"?subject=" + Uri.encode("some subject text here") +
"&body=" + Uri.encode("some text here")
val uri = Uri.parse(uriText)
val sendIntent = Intent(Intent.ACTION_SENDTO)
sendIntent.data = uri
if (sendIntent.resolveActivity(packageManager) != null) {
startActivity(Intent.createChooser(sendIntent, "Send email"))
}
Launch a website in the phone browser:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
if (browserIntent.resolveActivity(getPackageManager()) != null) {
startActivity(browserIntent);
}
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"))
if (browserIntent.resolveActivity(packageManager) != null) {
startActivity(browserIntent)
}
You can also launch a Chrome tab if the app. Take a look at this guide for how to launch this implicit intent.
Open app page on Google Play:
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + context.getPackageName()));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
val intent = Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + this.packageName)
)
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
Uri smsUri = Uri.parse("tel:" + to);
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("address", to);
intent.putExtra("sms_body", message);
intent.setType("vnd.android-dir/mms-sms"); // here setType will set the previous data to null.
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
val smsUri = Uri.parse("tel:$to")
val intent = Intent(Intent.ACTION_VIEW, smsUri)
intent.putExtra("address", to)
intent.putExtra("sms_body", message)
intent.type = "vnd.android-dir/mms-sms" // here setType will set the previous data to null.
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
Show location in maps application:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
String data = String.format("geo:%s,%s", latitude, longitude);
if (zoomLevel != null) {
data = String.format("%s?z=%s", data, zoomLevel);
}
intent.setData(Uri.parse(data));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
val intent = Intent()
intent.action = Intent.ACTION_VIEW
var data = String.format("geo:%s,%s", latitude, longitude)
if (zoomLevel != null) {
data = String.format("%s?z=%s", data, zoomLevel)
}
intent.data = Uri.parse(data)
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
Uri uri = Uri.fromFile(new File(file));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
val uri = Uri.fromFile(File(file))
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
Images or binary data:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
if (sharingIntent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "image/jpg"
val uri = Uri.fromFile(File(filesDir, "foo.jpg"))
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri.toString())
if (sharingIntent.resolveActivity(packageManager) != null) {
startActivity(Intent.createChooser(sharingIntent, "Share image using"))
}
or HTML:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, HtmlCompat.fromHtml(
"<p>This is the text shared.</p>",
HtmlCompat.FROM_HTML_MODE_LEGACY
));
if (sharingIntent.resolveActivity(getPackageManager()) != null) {
startActivity(Intent.createChooser(sharingIntent,"Share using"));
}
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "text/html"
sharingIntent.putExtra(Intent.EXTRA_TEXT, HtmlCompat.fromHtml(
"<p>This is the text shared.</p>",
HtmlCompat.FROM_HTML_MODE_LEGACY
))
if (sharingIntent.resolveActivity(packageManager) != null) {
startActivity(Intent.createChooser(sharingIntent, "Share using"))
}
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.