Skip to content

Using Android Intents Tutorial

The Action You Know

So far we have been opening activities with Intents using the Activity name. Intents, however, are very powerful in that they can determine what activity should be run based on the criteria you provide. If there is a case where more than one activity can use that type of content, Android with ask the user which app they would prefer to use. I’m going to create a new class that gives some examples of common actions.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent" 
	xmlns:android="http://schemas.android.com/apk/res/android">
	<TableRow>
		<TextView
	    	android:text="Latitude"
	    	android:layout_width="150dp"
	    	android:layout_height="wrap_content"
	    	android:layout_column="0" />
		<TextView
	    	android:text="Longitude"
	    	android:layout_width="150dp"
	    	android:layout_height="wrap_content"
	    	android:layout_column="1" />    		
	</TableRow>
	<TableRow>
	    <EditText
	    	android:id="@+id/txtLatitude"
	    	android:width="100px"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="35.696471" />
    		<EditText
    		android:id="@+id/txtLongitude"
	    	android:width="100px"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="139.570472" /> 
	</TableRow>
	<TableRow>
		<Button 
	    	android:id="@+id/btnShowLocation"
	    	android:text="Show Location"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content" />
	</TableRow>
	<TableRow>
	    <TextView	    	
	    	android:width="100px"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="Phone Number" />
    		<EditText
    		android:id="@+id/txtPhoneNumber"
	    	android:width="100px"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:inputType="phone"
	    	android:text="555-1212" /> 
	</TableRow>
	<TableRow>
		<Button 
	    	android:id="@+id/btnDial"
	    	android:text="Dial Number"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content" />
		<Button 
	    	android:id="@+id/btnCall"
	    	android:text="Call Number"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content" />
	</TableRow>
	<TableRow>
		<Button 
	    	android:id="@+id/btnContact"
	    	android:text="Pick Contact"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content" />
	</TableRow>
</TableLayout>
package com.learnandroid.intents;
 
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class UriActivity extends Activity {
 
	private EditText txtLatitude;
	private EditText txtLongitude;
	private EditText txtPhoneNumber;
 
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uri_layout);
 
        //Show Location Example
        txtLatitude = (EditText) findViewById(R.id.txtLatitude);
        txtLongitude = (EditText) findViewById(R.id.txtLongitude);
        Button btnShow = (Button) findViewById(R.id.btnShowLocation);
        btnShow.setOnClickListener(new Button.OnClickListener() {
 
        @Override
		public void onClick(View v) {
			Uri location = Uri.parse("http://maps.google.com/maps?q=" 
					+ txtLatitude.getText().toString() 
					+ "," 
					+ txtLongitude.getText().toString());
			startActivity(new Intent(Intent.ACTION_VIEW, location));
		} });
 
        //Dial Phone Example
        txtPhoneNumber = (EditText) findViewById(R.id.txtPhoneNumber);
        Button btnDial = (Button) findViewById(R.id.btnDial);
        btnDial.setOnClickListener(new Button.OnClickListener() {
 
        @Override
		public void onClick(View v) {				
			Uri phoneNum = Uri.parse("tel:" + txtPhoneNumber.getText().toString());
			startActivity(new Intent(Intent.ACTION_DIAL, phoneNum));
		} });
 
        //Make Call Example
        Button btnCall = (Button) findViewById(R.id.btnCall);
        btnCall.setOnClickListener(new Button.OnClickListener() {
 
        @Override
		public void onClick(View v) {				
			Uri phoneNum = Uri.parse("tel:" + txtPhoneNumber.getText().toString());
			startActivity(new Intent(Intent.ACTION_CALL, phoneNum));
		} });
 
        //Select from Contacts Example
        Button btnContact = (Button) findViewById(R.id.btnContact);
        btnContact.setOnClickListener(new Button.OnClickListener() {
 
        @Override
		public void onClick(View v) {				
			Uri content = Uri.parse("content://contacts/people");
			startActivity(new Intent(Intent.ACTION_PICK, content));
		} });
    }
}

We have a couple of EditText widgets that allow the user to enter in Latitude and Longitude (I went ahead a pre-populated them with the location of the Studio Ghibli museum in Japan). We have a button that when clicked shows the location on the map. We pass a URI to the startActivity method, instead of a class name, and Android uses the most appropriate app. We could have also passed in a URI like this, which would have launched the Google Maps app

Uri location = Uri.parse("geo:" + txtLatitude.getText().toString() + "," + txtLongitude.getText().toString());

The only reason I didn’t do it this way is I don’t have the Maps app on my emulator and so I didn’t want to assume you did either.

The next part of this page is another EditText widget where the user can enter a phone number, along with a button that will dial the number. You can see we used the ACTION_DIAL action this time, and passed in a tel: format URI. This will bring up the dialer with the number pre-populated. The next button demonstrates using ACTION_CALL in order to just place the call. Remember, though, when the user installs you app they will have to grant your app permission to make calls.

Finally, we show the action that allows us to have the user pick a person from their contacts. ACTION_PICK can be used for any collection, not just the contact list.

Next: Categorically Speaking

{ 6 } Comments

  1. shub | May 30, 2012 at 5:07 pm | Permalink

    i am new to android can you please explain what is onclicklistner (not what is does) and why we use View.onclicklistner and what is view there

  2. krishna | August 10, 2012 at 2:19 am | Permalink

    very useful, thaks..

  3. dinesh | February 10, 2014 at 3:24 am | Permalink

    wow…amazing…you give simple and easy look to android layout…..

    thank you very much

  4. fixit | April 11, 2014 at 11:56 am | Permalink

    great post, very useful for beginers

  5. Alex Butler | September 27, 2014 at 3:59 am | Permalink

    Will you accept guest post content?

  6. Sheridan | September 27, 2014 at 7:28 am | Permalink

    Sure, I would be willing to accept a guest post. Basically, my criteria would be that I should be able to open Eclipse, go through your tutorial step by step, and have it work without needed to figure out any extra stuff.

    Basically, I started this website with a friend (he wrote the lots of lists post). I was very gung ho about it… and then I found myself going through a divorce. I’ve kicked around the idea of starting to do tutorials again, and would be glad to have new content on here.

Post a Comment

Your email is never published nor shared. Required fields are marked *