Intents New

download Intents New

of 23

Transcript of Intents New

  • 7/31/2019 Intents New

    1/23

    Introduction

    To

    Intents

    (Our Intent is to make you

    fundamentally clear onIntents )

  • 7/31/2019 Intents New

    2/23

    Intents:2

    Intents are the most important and uniqueconcept in android.

    Androids innovative navigation and triggering

    mechanism is achieved by intents.i.e. intents are used for message passingbetween the different android components aswell as for starting other android components

    ( Activities, Services and Broadcast receivers)

  • 7/31/2019 Intents New

    3/23

    Understanding intents and

    intentFilters3

    An Intent is a declaration of need.

    An Intent-Filter is a declaration of capabilityand interest in offering assistance to those in

    need.

    An Intent is made up a number of pieces ofinformation describing the desired action or

    service. An intent-Filter may be generic or specific with

    respect to which intents it offers to service.

  • 7/31/2019 Intents New

    4/23

    Understanding intents and

    intentFilters4

    New City

    Feelshungry

    Xerox Shop(10 Digital

    xeroxmachines)

    Indoor

    stadiumRestaurantTea stall

    Intent

    Manifest file

    Intent filterResolution

  • 7/31/2019 Intents New

    5/23

    Intents:

    School of Computer Engineering & InnoVAdors Lab

    one of the most common use of intents is tostart an activity.

    Methods required to start an activity

    startActivity(Intent intent);

    startActivityForResult(Intent intent,int RequestCode);

  • 7/31/2019 Intents New

    6/23

    Intents:

    School of Computer Engineering & InnoVAdors Lab

    Types

    Implicit

    Explicit

    In explicit intent there is a short circuit to thetarget activity.

    In implicit intent the actual activity to start is

    decided at runtime by looking into the intentfilter.

  • 7/31/2019 Intents New

    7/23

    Intents: (Explicit)

    School of Computer Engineering & InnoVAdors Lab

    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());

    }

    }

  • 7/31/2019 Intents New

    8/23

    Intents: (Explicit)

    School of Computer Engineering & InnoVAdors Lab

    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);}

    }

    Intent Y

  • 7/31/2019 Intents New

    9/23

    Intents: (Explicit)

    School of Computer Engineering & InnoVAdors Lab

    Manifest file

  • 7/31/2019 Intents New

    10/23

    Intents: (Explicit)

    School of Computer Engineering & InnoVAdors Lab

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

    Activity X Activity Y

  • 7/31/2019 Intents New

    11/23

    Intents: (Explicit)

    School of Computer Engineering & InnoVAdors Lab

    Values can be sent between activities in bothdirections 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) ;

  • 7/31/2019 Intents New

    12/23

    Intents: (Explicit)

    School of Computer Engineering & InnoVAdors Lab

    Intent in=new Intent(x.this,y.class);In.putExtra(name1,10);In.putExtra(name2,20);

    startActivityForResult(in );

    Activity X Activity Y

    name1,10name2,20

  • 7/31/2019 Intents New

    13/23

    Intent

    School of Computer Engineering & InnoVAdors Lab

    To retrieve the values from the intent first theintent is caught using the getIntent() method.

    The values are then retrieved using the

    getIntExtra() , getFloatExtra().etc methods.

  • 7/31/2019 Intents New

    14/23

    Intents: (Explicit)

    School of Computer Engineering & InnoVAdors Lab

    Intent in=new Intent(x.this,y.class);In.putExtra(name1,10);In.putExtra(name2,20);

    startActivityForResult(in );

    Activity X

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

    Activity Y

    Name1,10Name2,20

  • 7/31/2019 Intents New

    15/23

    Intent

    School of Computer Engineering & InnoVAdors Lab

    To return values from the target activity back to thecaller 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 likeRESULT_OK,RESULT_CANCELED etc.)

    As the control will always go to the calling activitythe intent can be created in the simplest form like:

    Intent res=new Intent();

  • 7/31/2019 Intents New

    16/23

    Intents: (Explicit)

    School of Computer Engineering & InnoVAdors Lab

    Intent in=new Intent(x.this,y.class);In.putExtra(name1,10);In.putExtra(name2,20);

    startActivityForResult(in );

    Activity X

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

    Intent res=new Intent();Res.putExtra(ret1,30);

    setResult(RESULT_OK,res);

    Activity Y

    name1,10name2,20

    ret1,30

  • 7/31/2019 Intents New

    17/23

    Intent

    School of Computer Engineering & InnoVAdors Lab

    To retrieve the return values from the finishedactivity 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)

  • 7/31/2019 Intents New

    18/23

    Intents: (Explicit)

    School of Computer Engineering & InnoVAdors Lab

    onCreate( Bundle b){Intent in=new Intent(x.this,y.class);

    In.putExtra(name1,10);

    In.putExtra(name2,20);startActivityForResult(in,req );

    }

    onActivityResult(int requestcode,intresultcode,Intent in)

    {r.onActivityResult(requestcode,resultcode

    ,in);int p=in.getIntExtra(ret1,998);

    }

    Activity X

    onCreate( ){

    Intent i=getIntent();int

    num1=i.getIntextra(name1,999);Intent res=new Intent();Res.putExtra(ret1,30);

    setResult(RESULT_OK,res);}

    Activity Y

    name1,10name2,20

    ret1,30

  • 7/31/2019 Intents New

    19/23

    Attributes of Intent

    Primary Attributes

    Action

    Data

    Secondary Attributes

    Category

    Type

    Component

    Extras

  • 7/31/2019 Intents New

    20/23

  • 7/31/2019 Intents New

    21/23

    Attributes of Intent

    Category Gives additional information about theaction to execute.

    Example : CATEGORY_HOME

    CATEGORY_LAUNCHER CATEGORY_PREFERENCE

    CATEGORY_LAUNCHER means it shouldappear in the Launcher as a top-levelapplication.

    I Fil

  • 7/31/2019 Intents New

    22/23

    Intent Filters

    The core components of an application (its activities,

    services, and broadcast receivers) are activatedby intents. An intent is a bundle of information describinga desired action including the data to be acted upon,the category of component that should perform theaction. Android locates an appropriate component to

    respond to the intent, launches a new instance of thecomponent if one is needed, and passes it the Intentobject.

    Components advertise their capabilities the kinds ofintents they can respond to through intent filters. TheAndroid system must learn which intents a componentcan handle before it launches the component. Intent

    filters are specified in the manifest as

  • 7/31/2019 Intents New

    23/23

    Intent Filters