Using Intents to communicate with device

Somtimes as a developer we simply want to use some predefined or functionalities already provided by the device. Take for example we want the user to be able to make a phone call or send an sms, we dont need to develop our own custome dialer interface. We can simplu use the device's dialer to archieve this. Intents can handle various type of functionality and below are some example of Implicit Intents.

Opening Dialer

Intent intent = new Intent(Intent.ACTION_DIAL);

By passing the Intent.ACTION_DIAL we are teling the device to use any app that supports this feature of dialing a phone number. Could be your default dialer or skype and such. Also we can pass the contact value (here we use 0123456789) and it will be displayed on the screen.

Opening Message

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("address", "12125551212");
smsIntent.putExtra("sms_body","Body of Message");

By passing the Intent.ACTION_VIEW and setting Type = "vnd.android-dir/mms-sms" we are teling the device to use any app that supports mesaging feature of dialing a phone number. Could be your typical sms, mms dialer and such. This includes the body of the sms and the address.

Opening Webview

String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);

This will pass the entered url to the intent which is then oppended in the native browser of the device.

Opening Picture Camera

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);

This method allows you to open the native camera interface to capture a picture. After capture one can simple get the uri of the image by calling the onActivityResult as shown below...

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == Activity.RESULT_OK && requestCode == 0) {
    String result = data.toURI();
    // ...

Opening Text Supporting Apps // Create the text message with a string

Intent sendIntent = new Intent();
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);

// Verify that the intent will resolve to an activity

if (sendIntent.resolveActivity(getPackageManager()) != null) {

This will show a popup with supported Apps that allows texts such as Messenger, Sms, Twitter and so on.

Opening GoogleMaps passing Lat and Lng

double lat = 28.43242324;
double lng = 77.8977673;
String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?q=loc:%f,%f", lat, lng);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));

Passing the lat and lng will open the Map application on the android device with the predefined location.

There are many more intents so feel free to explore here.... Happy Coding!!!

All rights reserved

Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí