Android Projects

Android Development Books

Monday

Intent tutorials Part 1 with implicit in android

Objects of type "android.content.Intent" are used to send asynchronous messages within your application or between applications. Intents allow to send or receive data from and to other activities or services. They also allow to broadcast that a certain event has occurred.
Intents are a powerful concept as they allow the creation of loosely coupled applications. Intents can be used to communicate between any installed application component on the device.
An Intent object can contain information for the receiving component. For example if your application calls via an Intent a browser it may send the URL to the browser component. An Intent also contain information for the Android system so that the Android system can determine which component should handle the request.

Implicit vs Explicit Intents

Android supports explicit intents and implicit intents. Explicit intent names the component, e.g. the Java class which should be called.
Implicit intents asked the system to perform a service without telling the system which Java class should do this service. In constructing an implicit Intent you specify the action which should be performed and optionally an URI which should be used for this action. For example you could tell the system that you want to view (action) a webpage (URI). By starting an intent for this data the system would try to find an application which is registered for this event, e.g. a browwer. You can add more data to the Intent by adding "extras" to the Intent. These are key/value pairs.

Getting the Intent data in the called Activity

To get the Intent information in the called Activity use the method getIntent(). If the Activity was called via an implicit Intent you can receive the data and url from this Intent via getAction(), getData() and getExtras().

Implicit Intents - Opening an URL 
The following creates an examle project for calling several implicit intent. The Android system is asked to display a URI and chooses the corresponding application for the right URI. Create a new Android application "de.vogella.android.intent.implicit" with the Activity "CallIntents". Create the following view layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button android:id="@+id/Button01" android:layout_width="wrap_content" 
 android:layout_height="wrap_content" android:text="Call browser" 
 android:onClick="callIntent">
</Button>
<Button android:id="@+id/Button02" android:layout_width="wrap_content" 
 android:layout_height="wrap_content" android:text="Call Someone" 
 android:width="100px" android:onClick="callIntent">
</Button>
<Button android:id="@+id/Button03" android:layout_width="wrap_content" 
 android:layout_height="wrap_content" android:text="Dial" android:width="100px" 
 android:onClick="callIntent">
</Button>

</LinearLayout>
To be able to use certain intents you need to register then 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="de.vogella.android.intent.implicit"
      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="9" />


<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>
</manifest> 
 
Change your activity to the following. We will start the new intent with the method startActivityForResult()
which allow us tospecify a desired result code. Once the intent is finished the methodonActivityResult()
is called and you can perform actions based on the result of the activity.
 
package de.vogella.android.intent.implicit;

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

public class CallIntends extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public void callIntent(View view) {
		Intent intent = null;
		switch (view.getId()) {
		case R.id.Button01:
			intent = new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://www.dharmakshetri.com.np"));
			startActivity(intent);
			break;
		case R.id.Button02:
			intent = new Intent(Intent.ACTION_CALL,
					Uri.parse("tel:(+977)9800017700"));
			startActivity(intent);
			break;
		case R.id.Button03:
			intent = new Intent(Intent.ACTION_DIAL,
					Uri.parse("tel:(+977)12345789"));
			startActivity(intent);
			break;
                 default:
			break;
		}
	}
	
	@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);
	  }
	}
} 
If you start your application you should see an list of buttons and if you press the button,
 different activities should be performed.
Note that you do not specify any specific application. 
 
See part 2 of this article which was show how to implement explicit intent.
 
if you want more this article click.Direct article or  www.vogella.com 
            
 
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

1 comments

  1. please can i some information about intent filters which we usually found in android Manifest file.xml
    Thanks in advance

    ReplyDelete

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0