Anda di halaman 1dari 9

11/1/2009

1
Android Android
19
Android Android
IntentFilters IntentFilters
VictorMatos
Cleveland State University ClevelandStateUniversity
Notesarebasedon:
AndroidDevelopers
http://developer.android.com/index.html
19.Android IntentFilters
IntentFilters IntentFilters
AnAnalogy:RequestingActionsUsingHTTPandAndroid
1. TheHPPT
1
protocolusesanumberof<Action,resource>pairsto
accomplish its work accomplishitswork.
2. SomeoftheHTTP actionsarethewellknown(andlesserknown)
operations:POST,GET,PUT,DELETE,CONNECT,HEAD,OPTIONS.
3. AndroidusesamechanismquitesimilartoHTTPfortheinvocationof
worktobedone.
2222
4. INTENT istheAndroidsnamefortheabstractionrequestingactions.
5. UnlikeHTTPagivenAndroidsINTENTcouldberesolvedinmorethan
onepotentialway(forinstance,wemayhaveseveralSMSappswanting
toprocessanincomingtextmessage).
____
1. Source:HypertextTransferProtocol HTTP/1.1(1999).http://www.w3.org/Protocols/rfc2616/rfc2616sec9.html
11/1/2009
2
19.Android IntentFilters
IntentFilters IntentFilters
INTENTS
Anintentisanabstractdescriptionofanoperationtobeperformed.
Itsmostsignificantuseisinthelaunchingofactivities.
The primary pieces of information in an intent are action & data Theprimarypiecesofinformationinanintentare:action &data.
ACTION DATA Misc
Thegeneralactiontobe
performed,suchas:
ACTION_EDIT,
Thedatatooperateon,suchasaperson
recordinthecontactsdatabase,
expressedasa URI.
Iamgoodforeditingadocument
3333
Source:http://developer.android.com/reference/android/content/Intent.html
_ ,
ACTION_VIEW,
ACTION_MAIN,
ACTION_LAUNCHER
etc.
g f g
Iamgoodforviewingadocument
Iamthefirstexec.Activ.ofApplication
PutmeonthephonesMenu_Pad
19.Android IntentFilters
IntentFilters IntentFilters
PartsofaTypicalIntent
ACTION DATA MISC
Standard URI Category Standard URI Category
CATEGORY_DEFAULT
CATEGORY_BROWSABLE
CATEGORY_TAB
CATEGORY_ALTERNATIVE
CATEGORY_SELECTED_ALTERNATIVE
CATEGORY_LAUNCHER
CATEGORY_INFO
CATEGORY_HOME
CATEGORY_PREFERENCE
CATEGORY_TEST
ACTION_MAIN
ACTION_VIEW
ACTION_ATTACH_DATA
ACTION_EDIT
ACTION_PICK
ACTION_CHOOSER
ACTION_GET_CONTENT
ACTION_DIAL
ACTION_CALL
ACTION_SEND
ACTION_SENDTO
ACTION_ANSWER
ACTION_INSERT
ACTION_DELETE
ACTION_RUN
ACTION_TIME_TICK
ACTION_TIME_CHANGED
ACTION_TIMEZONE_CHANGED
ACTION_BOOT_COMPLETED
ACTION_PACKAGE_ADDED
ACTION_PACKAGE_CHANGED
ACTION_PACKAGE_REMOVED
ACTION_PACKAGE_RESTARTED
ACTION_PACKAGE_DATA_CLEARED
ACTION_UID_REMOVED
ACTION_BATTERY_CHANGED
ACTION_POWER_CONNECTED
ACTION_POWER_DISCONNECTED
ACTION_SHUTDOWN
CONTENTS suchas:
content://contacts/
content://contacts/1
SCHEME suchas:
tel:123
http://aaa.bbb.ccc
mailto://aa@bbb.ccc
MIME
Explicittype(aMIMEtype)of
theintentdata.
C
4444
ACTION_SYNC
ACTION_PICK_ACTIVITY
ACTION_SEARCH
ACTION_WEB_SEARCH
ACTION_FACTORY_TEST
// @
ftp://aaa.bbb.ccc
. ..
pop://
smtp://
ssl://
Component
Explicitnameofacomponent
classtousefortheintent.
Extras
putExtra(String,Bundle)
Flags
11/1/2009
3
19.Android IntentFilters
IntentFilters IntentFilters
Aside:MIME
Thissetofdocuments,collectivelycalledthe
M lti I t tM ilE t i MIME d fi th MultipurposeInternetMailExtensions,orMIME,redefinesthe
formatofmessagestoallowfor
(1) textualmessagebodiesincharactersetsotherthan
USASCII,
(1) anextensiblesetofdifferentformatsfornontextual
messagebodies,
5555
(2) multipartmessagebodies,and
(3) textualheaderinformationincharactersetsotherthan
USASCII.
____
Source:MultipurposeInternetMailExtensions.(MIME)PartTwo:MediaTypes.
Availableat:http://tools.ietf.org/html/rfc2046
NOTE:
CurrentusageofMIMEdescribes
contenttypeingeneral.
19.Android IntentFilters
IntentFilters IntentFilters
IntentResolution
WhenIntentsareissued,Androidlooksforthemostappropriatedwayof
respondingtotherequest.
Thedecisionofwhattoexecuteisbasedonhowdescriptivethecallis:
ExplicitIntents specifyaparticularcomponent
(viasetComponent(ComponentName) orsetClass(Context,Class) ),
whichprovidestheexactclasstoberun.Thisisatypicalwayforan
applicationtolaunchvariousinternal activitiesithasastheuserinteracts
with the application
66666
withtheapplication.
ImplicitIntents donotspecifiedaparticularcomponent.Howeverthey
includeenoughinformationforthesystemtodeterminewhichofthe
availablecomponentsareintheisbestcategorytorunforthatintent.
11/1/2009
4
19.Android IntentFilters
IntentFilters IntentFilters
IntentResolution
Theintentresolutionmechanismbasicallyrevolvesaround
t hi I t t i t ll f th i t t filt matchinganIntentagainstallofthe<intentfilter>
descriptionsintheinstalledapplicationpackages.
777777
19.Android IntentFilters
IntentFilters IntentFilters
IntentResolution
88888
11/1/2009
5
19.Android IntentFilters
IntentFilters IntentFilters
IntentResolution
Asshowninthepreviousillustration.Activity3hasissueageneric requestfor
helpprocessinganincomingtextmessage.
AssumetheuserhasinstalledaFancySMSapplicationto(perhaps)replace
thestandardHUMBLESMSapporiginallyincludedinAndroid.
UponthearrivaloftheimplicitIntent,Androidwill(somehow)telltheuser:
Youhavegotanewtextmessage.IhaveaFANCYandaHUMBLESMS
application whichoneyouwantmetoexecute?Makeitadefault?
99999
Choosingcandidates: Foranactivitytobeeligibleforexecutionitmust:
1.Supportthespecifiedaction
2.SupporttheindicatedMIMEtype(ifsupplied)
3.Supportallofthecategories namedintheintent.
_____________
RULEOFTHUMB:YourIntentsshouldbeasspecificaspossible
19.Android IntentFilters
IntentFilters IntentFilters
Example:IntentFilters
TheManifesttellstheapplication(FancySms)isabletointerceptincomingSMS
datausingitsSMSReceiver (potentialalternativetothedefaultSMSapp.)
<?xmlversion="1.0"encoding="utf8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="cis493.intentfilters"android:versionCode="1"android:versionName="1.0.0">
<usespermissionandroid:name="android.permission.RECEIVE_SMS"/>
<applicationandroid:icon="@drawable/icon">
<activityandroid:name=".FancySms">
<intentfilter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
/
10 10 10 10 10
</intentfilter>
</activity>
<receiverandroid:name="SMSReceiver"android:enabled="true">
<intentfilter>
<actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>
</intentfilter>
</receiver>
</application>
</manifest>
11/1/2009
6
19.Android IntentFilters
IntentFilters IntentFilters
Commentsontheexample:
Theapplicationconsistsoftwocomponents:
1 a common Activity called FancySms (acting as the main routine) and 1. acommonActivitycalledFancySms (actingasthemainroutine)and
2. abackgroundService(BroadcastReceiver)calledSMSService.
TheclausebelowindicatestheapplicationisallowedtoreceiveSMS
<usespermissionandroid:name="android.permission.RECEIVE_SMS"/>
ThecomponentSMSService hasthefilter
<intentfilter>
11 11 11 11 11
<intent filter>
<actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/>
</intentfilter>
thattriggersitsexecutionwheneveranewSMSisreceived
OtherapplicationswiththesamefiltercanbealsocalledbyAndroidwhen
newSMSarrives(untilaDEFAULTischosen)
19.Android IntentFilters
IntentFilters IntentFilters
Example:InterceptingIncomingSMS
<?xml ver si on="1.0" encoding="utf-8"?>
<Li near Layout
andr oi d: i d="@+id/mainLayout"
andr oi d: l ayout wi dt h="fill parent andr oi d: l ayout hei ght ="fill parent" andr oi d: l ayout _wi dt h= fill_parent andr oi d: l ayout _hei ght = fill_parent
andr oi d: or i ent at i on="vertical"
xml ns: andr oi d="http://schemas.android.com/apk/res/android"
>
<Text Vi ew
andr oi d: l ayout _wi dt h="fill_parent" andr oi d: l ayout _hei ght ="wrap_content"
andr oi d: t ext Si ze="20px" andr oi d: t ext St yl e="bold ndr oi d: backgr ound="#ff0000ff"
andr oi d: t ext ="Intercepting SMS messages"
/ >
<Scr ol l Vi ew
andr oi d: i d="@+id/myScroller1"
andr oi d: l ayout _wi dt h="fill_parent"
andr oi d: l ayout _hei ght ="fill_parent"
12 12 12 12 12
>
<Text Vi ew
andr oi d: i d="@+id/theMessage"
andr oi d: l ayout _wi dt h="fill_parent" andr oi d: l ayout _hei ght ="fill_parent"
andr oi d: backgr ound="#ffffffff" andr oi d: paddi ng="4px"
andr oi d: t ext Si ze="14px" andr oi d: t ext Col or ="#ff000000"
/ >
</ Scr ol l Vi ew>
</ Li near Layout >
11/1/2009
7
19.Android IntentFilters
IntentFilters IntentFilters
Example:InterceptingIncomingSMS
Note:
Testthefollowingapplication
fromtheEclipsesDDMS
perspective.SelectEmulator
Control>TelephonyActions.
Setphoneno.to5554,typea
message,clickonSend.
Alternativelyyoumaystart
anotheremulatorandsend
13 13 13 13 13
SMSto5554
19.Android IntentFilters
IntentFilters IntentFilters
Example:InterceptingIncomingSMS
/ / FancySms: mai n scr een - di spl ays i nt er cept ed SMS
package cis493.intentfilters;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class FancySms extends Activity {
static TextView txtMsg;
@Over r i de
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
14 14 14 14 14
set Cont ent Vi ew( R. l ayout . main);
txtMsg = (TextView)findViewById(R.id.theMessage);
}
}/ / cl ass FancySms
11/1/2009
8
19.Android IntentFilters
IntentFilters IntentFilters
Example:InterceptingIncomingSMS
/ / SMSRecei ver : l i st ens t o br oadcast ed SMS_RECEI VED si gnal s
package cis493.intentfilters;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver {
15 15 15 15 15
@Over r i de
public void onReceive(Context context, Intent intent) {
/ / Andr oi d saves i n a bundl e t he cur r ent t ext - message
/ / under name " pdus" and t ype: Obj ect [ ] . Lat er we cast t o
/ / SmsMessage[ ] . J ar gon pdu st ands f or " pr ot ocol dat a uni t "
Bundl e bundl e = i nt ent . get Ext r as( ) ;
19.Android IntentFilters
IntentFilters IntentFilters
Example:InterceptingIncomingSMS
Obj ect messages[ ] = ( Obj ect [ ] ) bundl e. get ( " pdus" ) ;
SmsMessage smsMessage[ ] = new SmsMessage[messages.length];
/ / Not e: l ong sms ar e br oken and t r ansmi t t ed i nt o var i ous pi eces
St r i ng msg = " " ;
int smsPieces = messages.length;
for (int n = 0; n < smsPieces; n++) {
smsMessage[ n] = SmsMessage. createFromPdu((byte[]) messages[n]);
/ / gr ab al l pi eces of t he i nt er cept ed sms
msg += " \ n" + ( n + 1) + " - of - " + smsPi eces + " \ n"
+ " Sender : \ t " + smsMessage[ n] . get Or i gi nat i ngAddr ess( ) + " \ n"
+ " Body: \ n " + smsMessage[ n] . get MessageBody( ) ;
}
16 16 16 16 16
}
/ / show f i r st par t of i nt er cept ed ( cur r ent ) message
Toast t oast = Toast . makeText(context, "FANCY >>> Received SMS: "
+ smsMessage[ 0] . get MessageBody( ) , Toast . LENGTH_LONG);
t oast . show( ) ;
ci s493. i nt ent f i l t er s. FancySms. txtMsg.setText(msg);
}
}/ / cl ass SMSRecei ver
11/1/2009
9
19.Android IntentFilters
IntentFilters IntentFilters
Questions Questions
17 17 17
19.Android IntentFilters
IntentFilters IntentFilters
JARGON:
PDU
i h t f "P t l D t U it" Thi t t f i f ti isshortfor"ProtocolDataUnit".Thisrepresentsanamountofinformation
deliveredthroughanetworklayer.
VND
virtualnetworkdata (todaytypicallyrepresentsabusinesscardwithname,
phone,email,etc).OriginallyregisteredasMIME vnd.abcintendedfor
transmissionofabc folkmelodiesinemails
see:http://www.iana.org/assignments/mediatypes/text/vnd.abc
18 18 18

Anda mungkin juga menyukai