logo

Intent-based programming is one of the most distinctive features of the Android platform. The entire application space effectively consists of components (called Activities in Android) and messages among components (called Intents). The Android application model is therefore a simple service-oriented architecture which is indeed an interesting approach.

An Intent object, is a passive data structure holding an abstract description of an operation to be performed. Intent is a very powerful concept.

    Advantages of Intents:

1. It loosely couples your application.
2. It can activate three core components of the android applications activities, services, and broadcast receivers.
3. Using intents you can start any other activity that is installed in the system.
e.g. you can start address book application and you can get contacts from address.
4. Using intents you can expose your applications activity for other applications.
e.g. You can register your own “Send SMS by myApp” application for sending SMS.

Intents are of two types:

Explicit Intents: Explicit intents used when you know the name of activity that you want to launch.
e.g.

    Intent intent = new Intent(FirstActivity.this,SecondActivity.class);  
    startActivity(intent);  

Implicit Intents: Instead of giving name of the intent you tell the system what you want to perform and system will find suitable activity for your task.
e.g.

intent = new Intent(Intent.ACTION_VIEW,  
Uri.parse("http://www.yahoo.com"));  
startActivity(intent); 

Example of Explicit Intents and data transfer between activities

The following demonstrates how you can transfer data between two activities. We will use explicit intents in this example and create two activities. The first activity will call the second one via an explicit intent. This second activity will receive data from the first one via the class “Bundle” which can be retrieved via intent.getExtras().

The second activity can be finished either via the back button on the phone or via the button. The method finish() is performed in this case. In this method you can transfer some data back to the calling activity. This is possible because we use the method startActivityForResult(). If you start an activity via this method the method onActivityRresult is called on the calling activity once the called activity is finshed.

a) Create a new Android application “com.mobisys.android.explicit_intent” with the Activity “ActivityOne“. Change the layout “main.xml” to the following

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="60dip"
            android:text="First Activity. Press button to call second activity"
            android:textSize="20sp" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </LinearLayout>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Calling an intent" >
    </Button>
</LinearLayout>

b) Create the layout “second.xml“:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LL02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/TV02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="First value from Activity 1" >
        </TextView>

        <EditText
            android:id="@+id/ET02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id/ET02" >
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LL03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/TV03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2nd Value from Activity1" >
        </TextView>

        <EditText
            android:id="@+id/ET03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+id/ET03" >
        </EditText>
    </LinearLayout>

    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Finished this activity" >
    </Button>
</LinearLayout>

c) Create a new activity “ActivityTwo” via the AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mobisys.android.explicite_intent"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivityOne"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:label="ActivityTwo" android:name=".ActivityTwo"></activity>
    </application>
    <uses-sdk android:minSdkVersion="7" />

</manifest> 

d) ActivitiOne.java contains:

package com.mobisys.android.explicite_intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class ActivityOne extends Activity {
    /** Called when the activity is first created. */
	private static final int REQUEST_CODE = 10;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        ((Button)findViewById(R.id.Button01)).setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
			 
				Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
				i.putExtra("Value1", "This value one for ActivityTwo ");
				i.putExtra("Value2", "This value two ActivityTwo");
				// Set the request code to any code you like, you can identify the
				// callback via this code
				startActivityForResult(i, REQUEST_CODE);
		  
			}       	
        });
        
    }
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
			if (data.hasExtra("returnKey1")) {
				Toast.makeText(this, data.getExtras().getString("returnKey1"),
						Toast.LENGTH_SHORT).show();
			}
		}
	}
}

e)ActivityTwo.java contains:

package com.mobisys.android.explicite_intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ActivityTwo extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.second);
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		String value1 = extras.getString("Value1");
		String value2 = extras.getString("Value2");
		if (value1 != null && value2 != null) {
			EditText text1 = (EditText) findViewById(R.id.ET02);
			EditText text2 = (EditText) findViewById(R.id.ET03);
			text1.setText(value1);
			text2.setText(value2);
		}
		((Button)findViewById(R.id.Button02)).setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
			 
				finish();
		  
			}       	
        });
	}
	@Override
	public void finish() {
		Intent data = new Intent();
		data.putExtra("returnKey1", "Swinging on a star. ");
		setResult(RESULT_OK, data);
		super.finish();
	}
}

f) Run the application, You will have the following screens:



Examples of Implicit Intents

The following creates an example project for calling several implicit intent. The Android system is asked to display a URI and chooses the corresponding application for the right URI.

1)
Create a new Android application “com.mobisys.android.implicit_intent” with the Activity “CallIntents“. Change the layout “main.xml” to the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:gravity="center">

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Call browser" >
    </Button>

    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Call Someone"
        android:width="100px" >
    </Button>

    <Button
        android:id="@+id/Button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dial"
        android:width="100px" >
    </Button>

    <Button
        android:id="@+id/Button04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Map"
        android:width="100px" >
    </Button>

    <Button
        android:id="@+id/Button05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search on Map"
        android:width="100px" >
    </Button>

    <Button
        android:id="@+id/Button06"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take picture"
        android:width="100px" >
    </Button>

    <Button
        android:id="@+id/Button07"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show contacts"
        android:width="100px" >
    </Button>

    <Button
        android:id="@+id/Button08"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edit first contact"
        android:width="100px" >
    </Button>
</LinearLayout>

2) To be able to use certain intents you need to register & use the certain permissions for your application. Maintain the following “AndroidManifest.xml“.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mobisys.android.impicit_intent"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CallIntents"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.CALL_PRIVILEGED"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
</manifest> 

3) Change your CallIntents.java to the following. We will start the new intent with the method startActivityForResult() which allow us to specify a desired result code. Once the intent is finished the method onActivityResult() is called and you can perform actions based on the result of the activity.

package com.mobisys.android.implicit_intent;

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.Toast;

public class CallIntents extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		((Button)findViewById(R.id.Button01)).setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
			Intent	intent = new Intent(Intent.ACTION_VIEW,
						Uri.parse("http://www.yahoo.com"));
				startActivity(intent);
			}	
		});
		((Button)findViewById(R.id.Button02)).setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
			Intent intent = new Intent(Intent.ACTION_CALL,
						Uri.parse("tel:(+91)9762214137"));
				startActivity(intent);
			}	
		});
		((Button)findViewById(R.id.Button03)).setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_DIAL,
						Uri.parse("tel:(+91)9762214137"));
				startActivity(intent);
			}	
		});
		((Button)findViewById(R.id.Button04)).setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW,
						Uri.parse("geo:50.123,7.1434?z=19"));
				startActivity(intent);
			}	
		});
		((Button)findViewById(R.id.Button05)).setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW,
						Uri.parse("geo:0,0?q=query"));
				startActivity(intent);
			}	
		});
		((Button)findViewById(R.id.Button06)).setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
				startActivityForResult(intent, 0);
			}	
		});
		((Button)findViewById(R.id.Button07)).setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/"));
				startActivity(intent);
			}	
		});
		((Button)findViewById(R.id.Button08)).setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_EDIT, Uri.parse("content://contacts/people/1"));
				startActivity(intent);
			}	
		});
	}	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	  if (resultCode == Activity.RESULT_OK && requestCode == 0) {
	    String result = data.toURI();
	    Toast.makeText(this, result, Toast.LENGTH_LONG);
	  }
	}
}

4) Run The application & you should see the following screen:

AUTHOR: Vikas Hiran
No Comments

Leave a Comment

Your email address will not be published.