Google's Android - An Overview

tibburfrogtownMobile - Wireless

Dec 14, 2013 (3 years and 5 months ago)

59 views

Google'sAndroid:AnOverview
YoniRabkin
yonirabkin@member.fsf.org
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.1
Abstract
Thislectureisanoverviewofdevelopingapplicationsfor
Google'sAndroid.WestartbyintroducingAndroidandits
components,welookattheanatomyofanAndroid
application,weexplainbasiccomponentsoftheAndroid
applicationAPIincludingUIdesignandnallywesaysome
thingsaboutthedevelopmentenvironment.
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.2
ShouldIbegivingthislecture?
IshouldbecauseI...
wroteoneAndroidapplicationfromstarttonish
Ishouldn'tbecauseI...
wroteonlyoneAndroidapplicationfromstarttonish
don'tknowJava
don'tuseEclipse
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.3
LegalStuff
Portionsofthisworkarereproducedfromworkcreatedand
sharedbytheAndroidOpenSourceProjectandused
accordingtotermsdescribedintheCreativeCommons2.5
AttributionLicense.
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.4
WhatisAndroid?
Androidisabunchofsoftware
(a)
releasedbyGoogleand
theOpenHandsetAlliance
(b)
.
(a)
platformblahblahframeworkblahblah
(b)
Theallianceincludesdiversememberssuchas:ChinaMobile,Asus,T-Mobile
(U.S.),SoftbankMobile(Japan),Motorola,Samsung,etc.
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.5
Whatisn'tAndroid?
anyspecicpieceofhardware
writtenentirelyinJava
runsonlyJava
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.6
WhatdoesAndroidinclude?
LinuxKernel(2.6)+drivers
AndroidRuntime:corelibraries+Dalvikvirtualmachine
Libraries:OpenGL,SGL
(a)
,Freetype,SSL,Sqlite,
Webkit,libc
XManagerwhereXissomeAPIcomponent
Pre-builtandpackagedapplications:Contacts,Phone,
Browser,Calendar,etc.
(b)
AndroidSDK
(a)
2Dgraphics
(b)
http://source.android.com/
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.7
TheDalvikVirtualMachine
TheDalvikvirtualmachine...
isaregister-basedJavaVM
ismemoryefcient
isdesignedtorunmultipleVMsefciently(oneper
application)
(a)
hasnoJustInTimecompilation
usesspecialbyte-code
reliesontheLinuxkernelforlow-levelstufflike
threading
(a)
communicationviaAIDL(AndroidInterfaceDenitionLanguage)
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.8
Low-levelLibraries
BSD-derivedlibctunedforsmalldevices
PacketVideo'sOpenCoremultimediacode
(a)
which
supports:MPEG4,H.264,MP3,OGG,AAC,AMR,
JPG,andPNG
(b)
libWebCore:Webkitbasedlibrarytosupportthe
browserandweb-views
3Dsupportforhardwareorsoftwarerenderingvia
OpenGL
Freetypefontrendering
Sqlite:relationaldatabaseforapplicationuse
(a)
http://www.packetvideo.com/products/core/index.html
(b)
PNGisthestandardforapplications
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.9
AppAnatomy:lestructure
SelectedlesfromasimpleAndroidapplicationstructure.
./AndroidManifest.xml
./build.xml
./bin/Work.apk
./bin/classes/deliverator/foo/Work.class
./bin/classes/deliverator/foo/R.class
./res/layout/main.xml
./res/layout/setup.xml
./res/values/theme.xml
./res/values/strings.xml
./res/drawable/background.png
./res/drawable/overlay.png
./src/deliverator/foo/R.java
./src/deliverator/foo/Work.java
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.10
AppAnatomy:lestructuredetail
Androidcaninferwhichresourcetoloadbydirectory
structure.
./res/values-en/strings.xml
./res/values-fr/strings.xml
..forlanguagesor...
./res/drawable-en-rUS-finger/
./res/drawable-port/
./res/drawable-port-160dpi/
./res/drawable-qwerty/
...forlayoutsandgraphics,whichcanleadto...
./res/drawable-en-rUS-port-160dpi-...-qwerty-dpad-480x320/
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.11
AppAnatomy:AndroidManifest.xml
AndroidManifest.xmlisanessentialpartoftheapplication
becauseit(amongstotherthings):
namestheJavapackagefortheapplication
describesactivities,services,contentproviders,etc.
declarespermissionstheapplicationmusthave
declarestheminimumAndroidAPIlevel
declaresIntents
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.12
AppAnatomy:Intents
Intentsconveyrequestsbetweenalldifferentcomponents
ofthesystem.
TheIntentResolutionmechanismrevolvesaround
matchinganIntentagainstallofthe<intent-lter>
descriptionsintheinstalledapplicationsand
BroadcastReceivers.
(a)
denedinAndroidManifest.xmlordynamically
belongtoanActivityorBroadcastReceiver
candeliverdataviaRFC2396URIs
(a)
multipleintentsallgetcalled!
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.13
Intents:CodeExample
AndroidManifest.xml <activityandroid:name=".Work"...>
<intent−filter>
<actionandroid:name="android.intent.action.MAIN"/>
<actionandroid:name="android.intent.action.SENDTO"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
<dataandroid:scheme="smsto"/>...
Work.java
Intentintent=getIntent();Stringaction=intent.getAction();
if(Intent.ACTION\_SENDTO.equals(action)){
doStuff(intent.getData().getEncodedSchemeSpecificPart()...
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.14
Activities
Anactivitypresentsavisualuserinterfaceforasingletask.
alwaysasubclassoftheActivitybaseclass
oneapplication,manyactivities
oneactivitycallsanother(stack)
(a)
givenadefaultwindowtodrawin
visualcontentdenedbyViews
havealife-cycle
(a)
Anactivitycanreturnavalueonexit,butdoesn'thaveto.
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.15
Activities:CodeExample
Supposethatyourapplicationhasamessagecomposition
screen.ThisisanActivity.TheUIwouldbeimplementedin
itsownle,say:src/blah/blooey/Compose.java:
publicclassComposeextendsActivity
{
@Override
publicvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.16
ActivitiesLife-Cycle:CodeExample
Activitiescalloneanother,soourComposeactivitywould
needtodenewhattodoifitgetsinterrupted,callsanother
activityorgetscalled:
@Override protectedvoidonResume(){
super.onResume();
dosomething();}
@Override protectedvoidonPause(){
super.onPause();
dosomethingelse();}
protectedvoidonDestroy(){...}
protectedvoidonStop(){...}
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.17
Threads
ActivityUIandyourcoderuninthesameprocess.So
non-trivialcomputationsrequireaseparatethread.
finalHandlermHandler=newHandler();
finalRunnablemUpdateResults=newRunnable(){
publicvoidrun(){
updateResultsInUi();}};
...
Threadt=newThread(){
publicvoidrun(){
mResults=doSomeThingHeavy(input);
mHandler.post(mUpdateResults);}};
t.start();
...
privatevoidupdateResultsInUi(){
if(mResults==Something){updateUI(mResults);}}
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.18
Services
Aservicerunsinthebackgroundforanindeniteperiodof
time.AserviceislikeanActivitywithoutaUI.
examples:gettingdatafromthenetwork,playinga
video
anactivitycanstartaservice
startsexplicitlywithContext.startService(intent)
startsimplicitlywithContext.bindService(intent)
providesacallbackviaitsonBind()method.
runsuntilContext.stopService()orstopSelf()
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.19
Broadcastreceivers
Abroadcastreceivesandreactstobroadcast
announcements.
oneapplication,manybroadcastreceivers
alwaysasubclassofBroadcastReceiverbaseclass
examplebroadcasts:lowbattery,callincoming,sms
arrivesetc.
canstartanactivityortheNoticationManager
asckleandsubtleasanyasynchronousprocessis
(a)
(a)
example:registeringandunregisteringinonResume()andonPause()
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.20
Broadcastreceivers:CodeExample
insomeserviceofthesystem:
Intentintent=newIntent(SOMEACTION);
intent.putExtra("name","value");
sendBroadcast(intent);
...thenintheapp:
privateBroadcastReceiverreceiver=newBroadcastReceiver(){
publicvoidonReceive(Contextcontext,Intentintent){
Stringvalue=intent.getStringExtra("name");
SomeActivityMethod(value);
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.21
Contentproviders
Acontentprovidermakesanapplication'sdataavailableto
otherapplications.
datacanbefromles,sqliteDBoranythingelse
(a)
alwaysasubclassofContentProviderbaseclass
useaContentResolverobjectandcallitsmethods
(a)
preferencesarelightweightname-valuestorage
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.22
Contentproviders:CodeExample
String[]projection=newString[]{
People.\_ID,
People.\_COUNT,
People.NAME,
People.NUMBER};
Uricontacts=People.CONTENT\_URI;
CursormanagedCursor=managedQuery(contacts,
projection,null,
null,
People.NAME+"
ASC");
managedCursorandmanagedQuerymanagethecursorlife-cycleforyou.
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.23
UI:somelayout.xml
Stacksomelayoutswithwidgetsinboxes:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/sometext"
android:text="@string/howareyougentlemen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
...
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.24
UI:SomeActivity.java
Callthelayoutandconnecttothewidgets:
publicvoidsetSomeText(Stringtext){
TextViewsomewidget
=(TextView)findViewById(R.id.sometext);
somewidget.setText(text);
}
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.25
UI:gengerpokenundmittengrabben
Findabuttoninthelayoutandconnectittoalistener:
finalButtonsomebutton
=(Button)findViewById(R.id.somebutton);
somebutton.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
doSomething();
}});
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.26
TheDevelopmentenvironment
Eclipse
(a)
isthenaturalenvironmentfordevelopingAndroid
applications.However,realeditorscanbealsousedifyou
arearealprogrammer.
TheAndroidSDKintegratesintoEclipseandhelpsthe
otherwiseaddledprogrammerwritecode.
TherearedifferentversionsoftheSDK(1.0,1.1,1.5)
(b)
Threeimportantcommandlinetools:"android",
"emulator"and"adb"
(a)
ItriedtoloadEclipseforthislecture...it'sstillloading.
(b)
atthetimeofwriting
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.27
tools/android
AnavdisanAndroidVirtualDevice.Usetools/androidto
create,list,modifyandremoveavds.
$./androidcreateavd-t3-n"1.5lvl3"\
--sdcard/path/to/disk/image
$./androidlist
Name:1.5lvl3
Path:/home/foo/.android/avd/1.5lvl3.avd
Target:GoogleAPIs(GoogleInc.)
BasedonAndroid1.5(APIlevel3)
Skin:HVGA
Sdcard:/path/to/disk/image
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.28
tools/emulatorandtheConsole
TheemulatorrunsavirtualAndroiddevice.
$./emulator@1.5lvl3
Oncetheemulatorisrunning,theConsole(optionally)
connectstoanemulatorinstanceandcanissues
commandsorretrievestatus.
$telnetlocalhost5554
AndroidConsole:type'help'
OK
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.29
ConsoleExamples
ExampleConsolecommands:
geonmeaGPGGA,123519,4807.038,N,01131.000,E...
rediraddtcp:5000:5554
powerdisplay/ac/status/present/health/capactiy
networkdelaygprs/edge/umts/none
networkspeedgsm/hscsd/gprs/.../full
gsmcall/accept/busy/.../list/voice/status
smssend...
Alsofromthecommandline:
$./emulator-netspeedgprs
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.30
tools/adbakaAndroidDebugBridge
TheAndroidDebugBridgeconnectstoadeviceor
emulator.
$./adb-sHT93LLZ00513shell
$./adb-sHT93LLZ00513install/path/app.apk
...orconnecttothedevicelogs
(a)
adb#logcat
I/DEBUG(551):debuggerd:Apr212009
I/vold(550):AndroidVolumeDaemon
...
...orconnecttothedevice'ssqliteDB
#sqlite3
SQLiteversion3.5.9
sqlite>
(a)
usefulwithLog.X(TAG,info)
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.31
Publishing
Publishingallowsotherstoinstallyoursoftware.Publishing
includes:
signtheapplication(notwiththedebugkey)
(a)
versiontheapp:android:versionCode="2"and
android:versionName="1.1"
(b)
provideandroid:label="@string/app_name"and
android:icon="@drawable/icon"
turnoffdebugging/logging,compile,signandtest
uploadtotheAndroidmarket
(a)
samesignature:permissionsandupgradesmadeeasy
(b)
accessiblevia:getPackageInfo(java.lang.String,int)
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.32
GettingtheSource
Thesource(about2GB)ishostedat
http://source.android.com/,usingRepo(somePython)and
Git.
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.33
ReferencesandAdieu
http://developer.android.com/
http://android-developers.blogspot.com/
http://groups.google.com/group/android-beginners
http://groups.google.com/group/android-developers
HappyHacking
ThisworkislicensedundertheCreativeCommonsAttribution2.5License.Toviewacopyofthislicense,visithttp://creativecommons.org/licenses/by/2.5/.p.34