Use Of Intents

This is a sample program to show the use of Intents. Intents are a powerful concept as they allow the creation of loosely coupled applications. Intents can be used to communicate between any installed application components on the device.

Underlying Algorithm:

Basic description of algorithm in step by step form:

1.) Create a project IntentBrowser
2.) Put the following code in layout/main.xml:

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
>

<button></button>

 

 

3.) We will start a new Intent with the method :

 

Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com"));

 

 

4.) As your activity gets called with intent you can get the data from the intent and display it in your application.
5.) In this example click event of the button will open Browser.
6.) Run the application.

Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. IntentBrowser. Enter following information:
Project name: IntentBrowser
Build Target: Android 2.3
Application name: IntentBrowser
Package name: com.sample.IntentBrowser
Create Activity: IntentBrowser

On Clicking Finish IntentBrowser code structure is generated with the necessary Android Packages being imported along with IntentBrowser.java. IntentBrowser class will look like following:

 

package com.sample.IntentBrowser;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class IntentBrowser extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void openBrowser(View view) {
Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com"));
startActivity(i);
}
}

 

 

Output –The final output:

Leave a Comment:

4 comments
deepa says July 4, 2011

Please provide the examples for their types…..

Reply
Christopher says September 6, 2011

Good day
Can I use this intent for a button to open the phone email application. The user click at button with the text email and the phone email application opens automatic.
Thanks

Reply
Christopher says September 6, 2011

Another thing

I have download the project but when I am trying to load it at eclipse I have this error “Invalid project Description”

Thanks

Reply
Add Your Reply