Você está na página 1de 23

Introduction To Intents (Our Intent is to make you fundamentally clear on Intents )

Intents:
2

Intents are the most important and unique concept in android. Androids innovative navigation and triggering mechanism is achieved by intents. i.e. intents are used for message passing between the different android components as well as for starting other android components ( Activities, Services and Broadcast receivers)

Understanding intents and intentFilters


3

An Intent is a declaration of need. An Intent-Filter is a declaration of capability and interest in offering assistance to those in need. An Intent is made up a number of pieces of information describing the desired action or service. An intent-Filter may be generic or specific with respect to which intents it offers to service.

Understanding intents and intentFilters


4

Intent filter Resolution

Manifest file Xerox Shop (10 Digital xerox machines) Indoor stadium Restaurant Tea stall Intent

New City

Feels hungry

Intents:

one of the most common use of intents is to start an activity. Methods required to start an activity
startActivity(Intent intent); startActivityForResult(Intent intent,int RequestCode);

School of Computer Engineering & InnoVAdors Lab

Intents:

Types
Implicit
Explicit

In explicit intent there is a short circuit to the target activity. In implicit intent the actual activity to start is decided at runtime by looking into the intent filter.

School of Computer Engineering & InnoVAdors Lab

Intents: (Explicit)
import staements public class x extends Activity { /** Called when the activity is first created. */ class clicking implements Button.OnClickListener { public void onClick(View v) { Intent in=new Intent(x.this,y.class); startActivity(in); } } Button bt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

bt=(Button)findViewById(R.id.Button01);
bt.setOnClickListener(new clicking()); } }

School of Computer Engineering & InnoVAdors Lab

Intents: (Explicit)
Intent Y

package x.first; import android.app.Activity; import android.os.Bundle; public class y extends Activity{ public void onCreate(Bundle a) { super.onCreate(a); setContentView(R.layout.y); } }
School of Computer Engineering & InnoVAdors Lab

Intents: (Explicit)
Manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="x.first" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".x" 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:name=".y" android:label="@string/app_name"> </activity> </application> </manifest>

School of Computer Engineering & InnoVAdors Lab

Intents: (Explicit)
Activity X
Activity Y

Intent in=new Intent(x.this,y.class); startActivity(in );

School of Computer Engineering & InnoVAdors Lab

Intents: (Explicit)
Values can be sent between activities in both directions if the required method is : startActivityForResult(Intent intent,int requestcode);

To send values to the target activity, it is required to put values in the intent using the method : putExtra(name,value) ;

School of Computer Engineering & InnoVAdors Lab

Intents: (Explicit)
Activity X
name1,10 name2,20 Intent in=new Intent(x.this,y.class); In.putExtra(name1,10); In.putExtra(name2,20); startActivityForResult(in ); Activity Y

School of Computer Engineering & InnoVAdors Lab

Intent

To retrieve the values from the intent first the intent is caught using the getIntent() method. The values are then retrieved using the getIntExtra() , getFloatExtra().etc methods.

School of Computer Engineering & InnoVAdors Lab

Intents: (Explicit)
Activity X
Name1,10 Name2,20 Intent in=new Intent(x.this,y.class); In.putExtra(name1,10); In.putExtra(name2,20); startActivityForResult(in ); Activity Y

Intent i=getIntent(); int num1=i.getIntextra(name1,999);

School of Computer Engineering & InnoVAdors Lab

Intent

To return values from the target activity back to the caller activity the setResult(int resultcode,intent) method is used The return values can be put in the intent using the usual methods.(putExtra()) The result code can be ant integer value(Android system defines a set of integer values like RESULT_OK,RESULT_CANCELED etc.) As the control will always go to the calling activity the intent can be created in the simplest form like: Intent res=new Intent();
School of Computer Engineering & InnoVAdors Lab

Intents: (Explicit)
Activity X
name1,10 name2,20 Intent in=new Intent(x.this,y.class); In.putExtra(name1,10); In.putExtra(name2,20); startActivityForResult(in ); Intent i=getIntent(); int num1=i.getIntextra(name1,999); Intent res=new Intent(); Res.putExtra(ret1,30); ret1,30 setResult(RESULT_OK,res); Activity Y

School of Computer Engineering & InnoVAdors Lab

Intent

To retrieve the return values from the finished activity the

onActivityResult(int requestcode,int resultcode,Intent intent)

method has to be overridden in the calling activity. The returned values can be retrieved from the intent by using the usual methods. (getIntExtra(),getFloatExtra().etc)
School of Computer Engineering & InnoVAdors Lab

Intents: (Explicit)
Activity X onCreate( Bundle b){ Intent in=new Intent(x.this,y.class); In.putExtra(name1,10); In.putExtra(name2,20); startActivityForResult(in,req ); } Activity Y name1,10 name2,20 onCreate( ) { Intent i=getIntent(); int num1=i.getIntextra(name1,999); Intent res=new Intent(); Res.putExtra(ret1,30); setResult(RESULT_OK,res); }

onActivityResult(int requestcode,int ret1,30 resultcode,Intent in) { er.onActivityResult(requestcode,resultcode ,in); int p=in.getIntExtra(ret1,998); }

School of Computer Engineering & InnoVAdors Lab

Attributes of Intent
Primary Attributes

Action Data

Secondary Attributes

Category Type Component Extras

Attributes of Intent

Action is a string that specifies the work to be performed by an Activity Such as ACTION_MAIN, ACTION_DIAL, ACTION_VIEW, ACTION_EDIT etc.

public static final String ACTION_MAIN Activity Action: Start as a main entry point, does not expect to receive data. Constant Value: "android.intent.action.MAIN i.e. public static final String ACTION_MAIN = "android.intent.action.MAIN";

Data means the data to operate on, such as a person record in the contacts database.

Attributes of Intent

Category Gives additional information about the action to execute.


Example : CATEGORY_HOME CATEGORY_LAUNCHER CATEGORY_PREFERENCE

CATEGORY_LAUNCHER

means it should appear in the Launcher as a top-level application.

Intent Filters

The core components of an application (its activities, services, and broadcast receivers) are activated by intents. An intent is a bundle of information describing a desired action including the data to be acted upon, the category of component that should perform the action. Android locates an appropriate component to respond to the intent, launches a new instance of the component if one is needed, and passes it the Intent object. Components advertise their capabilities the kinds of intents they can respond to through intent filters. The Android system must learn which intents a component can handle before it launches the component. Intent filters are specified in the manifest as <intent-

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

Você também pode gostar