Launching Built-In Apps with Intents

Just like in Processing for Android, we can launch built-in applications using an Intent with straight Android.

Intents are extremely useful in leveraging the built-in capabilities on Android. Using an Intent, we can allow the user to send an SMS message, compose an email, launch a browser window, select a picture, take a picture and more.

Here is a code snippet for sending an SMS message via an Intent:

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "12125551212");
smsIntent.putExtra("sms_body","Body of Message");
startActivity(smsIntent);

For launching a web page in the browser:

Intent intent = new Intent(Intent.ACTION_VIEW); 
Uri uri = Uri.parse("http://www.mobvcasting.com/"); 
intent.setData(uri); 
startActivity(intent);

For taking a picture:

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_RESULT);

In this case, we called it with startActivityForResult so that we can get access to the picture once it is captured.

Here is the full example:

package com.apress.proandroidmedia.ch1.cameraintent;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;

public class CameraIntent extends Activity {

     final static int CAMERA_RESULT = 0;

     ImageView imv;

     @Override
     public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	
		Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(i, CAMERA_RESULT);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		 super.onActivityResult(requestCode, resultCode, intent);
	
		if (resultCode == RESULT_OK) {
			Bundle extras = intent.getExtras();
			Bitmap bmp = (Bitmap) extras.get("data");
			imv = (ImageView) findViewById(R.id.ReturnedImageView);
			imv.setImageBitmap(bmp);
		}
	}
}

A whole slew of applications allow specific actions to be triggered via an intent. http://www.openintents.org offers a good list.