Top Banner

of 44

Android Message Handling

Apr 03, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/28/2019 Android Message Handling

    1/44

    Development

  • 7/28/2019 Android Message Handling

    2/44

    MessageHandling

    Intents

  • 7/28/2019 Android Message Handling

    3/44

    Manifestfile

    AndroidManifest.xml Presentsessen7alinforma7onaboutthe

    applica7ontotheandroidsystem

    Systemmusthaveitbeforeitcanruntheapplica7on

  • 7/28/2019 Android Message Handling

    4/44

    ManifestfileApplica7onManager

    AppB

    AppB

    Manifest

    AppA

    AppA

    ManifestRead

    Run

  • 7/28/2019 Android Message Handling

    5/44

    Manifestfile

    ItnamestheJavapackagefortheapplica7on. Thepackagenameservesasauniqueiden7fierforthe

    applica7on.

    Itdescribesthecomponentsoftheapplica7ontheac7vi7es,services,broadcastreceivers,andcontentprovidersthattheapplica7oniscomposedof.

    Itnamestheclassesthatimplementeachofthecomponentsandpublishestheircapabili7es.

    Thesedeclara7onslettheAndroidsystemknowwhatthecomponentsareandunderwhatcondi7onstheycanbelaunched.

  • 7/28/2019 Android Message Handling

    6/44

    Manifestfile

    Itdetermineswhichprocesseswillhostapplica7oncomponents. Itdeclareswhichpermissionstheapplica7onmusthaveinorderto

    accessprotectedpartsoftheAPIandinteractwithotherapplica7ons.

    Italsodeclaresthepermissionsthatothersarerequiredtohaveinordertointeractwiththeapplica7on'scomponents.

    ItliststheInstrumenta7onclassesthatprovideprofilingandotherinforma7onastheapplica7onisrunning.Thesedeclara7onsarepresentinthemanifestonlywhiletheapplica7onisbeingdevelopedandtested;they'reremovedbeforetheapplica7onispublished.

    ItdeclarestheminimumleveloftheAndroidAPIthattheapplica7onrequires.

    Itliststhelibrariesthattheapplica7onmustbelinkedagainst.

  • 7/28/2019 Android Message Handling

    7/44

    Example:Manifestfile

  • 7/28/2019 Android Message Handling

    8/44

    Manifestfile

    Basically,itisasetupfilethatandroidusespriorofrunningtheapplica7on

    Applica7oninforma7on Ac7vity,services,receiver,provider

    Itwillstatethecomponentsandthepermissionthatyourapplica7onrequirestorun Permissionfromtheuser Permissionfromyourapplica7on

    Lis7ngoflibrariesandinstrumenta7onclasses

  • 7/28/2019 Android Message Handling

    9/44

    Securityconcept

    Androidapplica7onrunsontheconceptofsandbox Thesandboxisdesignedtopreventapplica7onsfrom

    disrup7ngeachother,exceptbyexplicitlydeclaringthepermissionstheyneedforaddiGonalcapabiliGesnotprovidedbythebasicsandbox

    Thepermissionsrequiredbyanapplica7onaredeclaredsta7callyinthatapplica7on,sotheycanbeknownup-frontatinstall7meandwillnotchangeaWerthat

  • 7/28/2019 Android Message Handling

    10/44

    Communica7on?

    CorecomponentsofanAndroidapplica7onAc7vi7esServicesBroadcastreceivers

    ithsomuchrestric7on,howiscommunica7ondone?

  • 7/28/2019 Android Message Handling

    11/44

    Intent

    ThroughmessagescalledIntents Facilityforlaterun-7mebindingbetween

    componentsinthesameordifferentapplica7ons

    SimplelowoverheadIPC(Inter-processcommunica7on)

    passivedatastructureholdinganabstractdescrip7onofanac7ontobeperformed

  • 7/28/2019 Android Message Handling

    12/44

    Manifestfile

    Ac7vityB

    Intent

    Ac7vityA

    OS

    Ac7onData

    Intent

  • 7/28/2019 Android Message Handling

    13/44

    Intentcontent

    Primarypiecesofinforma7onareAc7on

    ACTION_VIE ACTION_EDIT ACTION_MAIN

    Data Thedatatobeworkedon

    Ac7vity1 Ac7vity2

    Intent:ac7on+data

    Op7onalResults

  • 7/28/2019 Android Message Handling

    14/44

    Intentusage Ac7vity

    Usedtostartac7vity Explicitly

    Specifyingwhichclasstoload Implicitly

    Reques7nganac7ontobeperformedonapieceofdata Services

    ini7ateaserviceordelivernewinstruc7onstoanongoingservice. Broadcastreceivers

    AndroidusesbroadcastIntentstoannouncesystemevents Applica7onwillreactaccordingtoanyreceivedevents

    Androidsystemfindstheappropriateac7vity,service,orsetofbroadcastreceiverstorespondtotheintent,instan7a7ngthemifnecessary

  • 7/28/2019 Android Message Handling

    15/44

    Intentuses

    Ac7vityMakesuseof

    startAc7vity(Intentobject) startAc7vityForResult(Intentobject)

    Asmen7oned,twowaystostartanac7vity Implicit Explicit

  • 7/28/2019 Android Message Handling

    16/44

    Intentuses

    Implicitmethodofstar7ngac7vity Constructor

    Intent(Stringac7on,Uri); Intent(Stringac7on);

    E.g. Intentintent=

    newIntent(Intent.ACTION_DIAL,Uri.parse(tel:555-2368));

    startAc7vity(intent);

    NewAc7vitywillbecreatedandbecomevisibleandac7ve,movingtothetopoftheac7vitystack

  • 7/28/2019 Android Message Handling

    17/44

    Example:Dialer

  • 7/28/2019 Android Message Handling

    18/44

    Intentuses

    Explicitmethodofstar7ngac7vity Constructor

    Intent(ContextpackageContext,Classcls) E.g.

    Intentintent=newIntent(MyAc7vity.this,MyOtherAc7vity.class);startAc7vity(intent);

    NewAc7vitywillbecreatedandbecomevisibleandac7ve,movingtothetopoftheac7vitystack

  • 7/28/2019 Android Message Handling

    19/44

    Intentuses

    startAc7vity()Independentcallfromitsparent(caller)illnotprovideanyfeedbackwhenitcloses

    Howistheparenttodetecttheac7vityhasended?

    Ac7vity1 Ac7vity2Start

  • 7/28/2019 Android Message Handling

    20/44

    Intentuses

    startAc7vityForResult() PossibletostartansubAc7vitywhichisconnectedtoits

    parents

    subAc7vitytriggeraneventhandlerwithintheirparentac7vitywhentheyclose

    Anyac7vityregisteredinthemanifestcanbestartedasansub-ac7vity

    Ac7vity1 Ac7vity2Start

    Ended,Myresultsare

  • 7/28/2019 Android Message Handling

    21/44

    LaunchingSubac7vity

    publicclassMyAc7vityextendsAc7vity{...

    sta7cfinalintSHO_SUB_ACTIVITY=99999991;

    protectedbooleanonKeyDown(intkeyCode,KeyEventevent)

    {Intentintent=newintent(this,MyOtherAc7vity.class);

    startAc7vityForResult(intent,SHO_SUB_ACTIVITY);

    }

    returnfalse;

    }

    .}

  • 7/28/2019 Android Message Handling

    22/44

    Returningresults

    publicclassMyOtherAc7vityextendsAc7vity{

    ...

    protectedvoidonMyOwnMethod()

    {

    intresult=0;

    //settheresultstobereturnedtoparent setResult(RESULT_OK,result);

    //Endsthesub-ac7vity

    finish();

    }

    .}

  • 7/28/2019 Android Message Handling

    23/44

    ReceivingResult

    publicclassMyAc7vityextendsAc7vity{

    ...

    protectedvoidonAc7vityResult(intrequestCode,intresultCode,Intent

    data)

    {

    if(requestCode==SHO_SUB_ACTIVITY){ if(resultCode==RESULT_OK){

    startAc7vity(newIntent(Intent.ACTION_VIE,data));

    }

    }

    }}

  • 7/28/2019 Android Message Handling

    24/44

    IntentFilter

    Serviceimplicitintents UsedtoregisterAc7vi7es,Servicesand

    BroadcastReceiversasbeingcapableof

    performinganac7ononapar7culardata Tellandroidthattheycanserviceac7on

    requestsfromothers

  • 7/28/2019 Android Message Handling

    25/44

    Manifest

    IntentFilter

    Ac7onFilter

    Ac7onFilter

    CategoryFilter

    DataFilter

    DataFilter

    Intent

  • 7/28/2019 Android Message Handling

    26/44

    ManifestA

    IntentFilter

    Ac7onFilter

    Ac7onFilter

    CategoryFilter

    DataFilter

    DataFilter

    ManifestA

    Ac7onFilter

    Ac7onFilter

    CategoryFilter

    DataFilter

    DataFilter

    IntentPASS

    FAIL

  • 7/28/2019 Android Message Handling

    27/44

    IntentFilter

    AsAndroidmustknowthecapabilityofacomponentbeforeitcanlaunch,intentfilters

    aregenerallynotsetupinjavacode.But

    ratherinthemanifestxmlfile Useintent-filtertagtoregisteran

    applica7oncomponentasanintenthandler

  • 7/28/2019 Android Message Handling

    28/44

    IntentFilter

    Intentsofacomponentsarerequiredtoprovidethe

    Ac7onDataCategory

    FailinganyoneofthemwillresultinAndroidnotdeliveringtheintenttothecomponent

  • 7/28/2019 Android Message Handling

    29/44

    Intentfilter

    Ac7on Specifythenameoftheac7onbeingserviced Shouldbeinuniquestrings

    E.g.

    ...

  • 7/28/2019 Android Message Handling

    30/44

    Intentfilter

    Possibletolistmorethanone Afiltermustcontainatleastone

    elementoritwillblockallintents

    ToPass,onlyonemustmatch Iffail

    Nointentgoespass Ifintentobjectdoesntspecifyanac7on,automa7callypasses

    thetest

  • 7/28/2019 Android Message Handling

    31/44

    Intentfilter

    Category Specifyunderwhichcircumstancestheac7onshouldbe

    serviced

    Canincludemul7pleintentfiltertag Specifyyourownorusethestandardvalues

    E.g.

    ...

  • 7/28/2019 Android Message Handling

    32/44

    Intentfilter

    Everycategoryintheintentobjectmustmatchacategoryinthefilter

    Filtercanlistaddi7onal,butcannotomitanyintheintentobject

    Intentobjectnocategoriesshouldalwayspassthistest

  • 7/28/2019 Android Message Handling

    33/44

    Intentfilter

    Data Specifymatchesfordatayourcomponentcanacton Canincludemul7pleschemataifyouarecapableofhandlingmorethanone

    E.g.

    ...

    Mainlyspecifies datatype(MimeMediaType) URI(UniformResourceIden7fier)

  • 7/28/2019 Android Message Handling

    34/44

    Intentfilter

    URI Scheme://host:port/path E.g.

    content://com.example.project:200/folder/subfolder/etc Schemeiscontent Hostiscom.example.project Portis200 Pathis/folder/subfolder/etc

    Host+port=Authority Aributesareop7onalbutnotindependentfromeachother

    Authorityneedsschemetobemeaningful Pathneedsbothauthorityandscheme

    Moreathp://en.wikipedia.org/wiki/URI_scheme

  • 7/28/2019 Android Message Handling

    35/44

    Intentfilter

    Topass.. AnintentobjectwithoutURIordatatypeiffilterdoesnot

    specifyanytoo ObjectwithURIandnodatatyperequiresthesameforthefilter ObjectwithdatatypeandnoURIrequiresthesameforthefilter

    ithbothURIanddatatype Objectwithdatatypematchesoneofthedatatypelistedinthe

    filter

    URImustmatchfiltersURI ObjectURIhasacontent:orfile:URIandfilterdoesnotspecifyan

    URI

  • 7/28/2019 Android Message Handling

    36/44

    SendBroadcast

    hoistransming?SystemmessageswillbetransmiedbytheOSApplica7onspecificwillbesentvia

    Context.sendBroadcast();

  • 7/28/2019 Android Message Handling

    37/44

    SendBroadcast

    Intentac7onstringisusedtoiden7fytheeventbeingbroadcast

    Ac7onstringusedistobeunique. E.g.

    Publicsta7cfinalStringNE_LIFEFORM_DETECTED=com.paad.ac7on.NE_LIFEFORM

    Intentintent=newIntent(NE_LIFEFORM_DETECTED);

    Intent.putExtra(lifeformName,lifeformType);

    sendBroadcast(intent);

  • 7/28/2019 Android Message Handling

    38/44

    ReceivingBroadcast

    Updatecontent Launchservices/ac7vi7es 5secondruletoensurethatnomajor

    processingisdone

  • 7/28/2019 Android Message Handling

    39/44

    ReceivingBroadcast

    PublicclassmyBroadCastReceiverextendsBroadcastReceiver{

    publicvoidonReceive(Contextcontext,intentintent)

    {

    Stringtype=

    intent.getStringExtra(lifeformName);

    if(type.equals(alien)

    {

    context.startAc7vity(startIntent);

    }

    }

    }

  • 7/28/2019 Android Message Handling

    40/44

    RegisterBroadcastreceiver

  • 7/28/2019 Android Message Handling

    41/44

    IntentExtras Usedtostorevaluesforthereceivingcomponent Primi7vetypesonly Assignnametoextras Retrievingvalueswithassignedname

  • 7/28/2019 Android Message Handling

    42/44

    IntentExtras MakeuseofputExtramethodtoplaceExtravaluesintotheintent putExtraisoverloadedtotakeinalldifferent

    primi7vetypes

    Int,boolean,long,char,etc.. Beware:retrievalmethodisnotoverloaded

    getXXXExtra();

  • 7/28/2019 Android Message Handling

    43/44

    Example:IntentExtras SendingpartypublicvoidonClick(Viewv){

    //TODOAuto-generatedmethodstub

    myIntent=newIntent(myContext,

    myReceivingIntent.class);

    myIntent.putExtra("MyExtra",65501683);

    startAc7vity(myIntent);

    }

  • 7/28/2019 Android Message Handling

    44/44

    Example:IntentExtras Receivingparty

    publicvoidonCreate(BundlesavedInstanceState){

    IntentreceivingIntent;

    intmyInt;

    TextViewtv=(TextView)findViewById(R.id.myRecvTV);

    receivingIntent=getIntent();

    myInt=receivingIntent.getIntExtra("MyExtra",0);

    tv.setText(""+myInt);

    }