synapseindia android apps development

Upload: synapseindiaappsdevelopment

Post on 02-Jun-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Synapseindia Android Apps Development

    1/47

    Android Development Tutorial

  • 8/10/2019 Synapseindia Android Apps Development

    2/47

    Contents

    2

    Whats AndroidAndroid architecture

    Android software development

    Hello World on AndroidMore

  • 8/10/2019 Synapseindia Android Apps Development

    3/47

    Whats Android

    3

  • 8/10/2019 Synapseindia Android Apps Development

    4/47

    Android Phones

    4

  • 8/10/2019 Synapseindia Android Apps Development

    5/47

    Mobile Devices

    5

    ts obvious that mobile device ma! ta"e the place of PC in future

    #$ pla!s a vital part

  • 8/10/2019 Synapseindia Android Apps Development

    6/47

    6

  • 8/10/2019 Synapseindia Android Apps Development

    7/47

    #HA and Android

    7

    #HA%#pen Handset Alliance& is a 'roup of () technolo'! and mobile companies* includin' +oo'le* ntel*Dell* HTC and China Mobile

    #HAs aim ,accelerate innovation in mobile phones

    offer consumers a richer* less e-pensive* and better mob

    ile e-perience#HA developed Android.* the first complete* open*and free mobile platform

    #HA was initiall! called up b! +oo'le* and +oo'le isthe captain

  • 8/10/2019 Synapseindia Android Apps Development

    8/47

    Whats Android

    8

    +enerall!* Android is a software stac" for mobile devices that includes an operatin' s!stem* middleware and "e! applica

    tionsAndroid is based on /A0A and all its applications are developed in /A0A

    The /A0A 0M* "nown as Dalvi"* is hi'hl!customi1ed and optimi1ed for mobile devices

    ndroid $D2 offers rich tools for andro

    id application development and man! u

    The core of Android

  • 8/10/2019 Synapseindia Android Apps Development

    9/47

    Android 4eatures 5)

    9

    Application framewor" enablin' reuse and replacement of components

    #ptimi1ed /ava virtual machine6 Dalvi"

    #ptimi1ed +raphics Processin'* supportin' 7D and8D 'raphics%#pen+9 :$ );< &

    nte'rated open source web browser6 Web2it

    $=9ite for structured data stora'e

  • 8/10/2019 Synapseindia Android Apps Development

    10/47

    Android 4eatures 57

    10

    Multimedia capabilit!* supportin' varieties ofaudio* video and still ima'e formats

    +$M Telephon!

    >luetooth* :D+:* 8+ and Wi?4i support

    Camera* +P$* compass* accelerometer

    and other sensors support

    @ich development environment* includin' an emulator* debu''in' tools* memor! probe tools* lo' tools and powerful eclipse plu'ins

    Hardwaredependent

  • 8/10/2019 Synapseindia Android Apps Development

    11/47

    Android architecture

    11

  • 8/10/2019 Synapseindia Android Apps Development

    12/47

    12

  • 8/10/2019 Synapseindia Android Apps Development

    13/47

    9inu- 2ernel

    13

    ote that Android based on a 9inu- "ernel not a 9inu- #$

    $upplies $ecurit!* Memor! mana'ement* Process m

    ana'ement* etwor" stac" and Driver modelActs as an abstraction la!er between the hardware and the rest of the software stac"

  • 8/10/2019 Synapseindia Android Apps Development

    14/47

    9ibraries

    14

    @un in s!stem bac"'round

    Bsin' C C 9an'ua'e

    E t!pes of 9ibraries>ionic 9ibc* s!stem C libraries

    4unction 9ibraries* supportin' multimedia* web browser* $=9ite;;;

    ative $erversHardware

    Abstraction 9ibraries

  • 8/10/2019 Synapseindia Android Apps Development

    15/47

    Core 9ibraries

    15

    $!stem C librar!* the standard C s!stem librar!* tuned for embedded 9inu-?based devices

    Media 9ibraries* support pla!bac" and recordin' of man! popular audio and video formats* as well as ima'e files* includin' MP:+E* H;7FE* MP8* AAC* AM@*

    /P+* and P +

    $urface Mana'er* mana'es access to the displa! subs!stem and seamlessl! composites 7D and 8D 'raphic la!ers from multiple applications

    Web2it* a modern web browser en'ine which powers both the Android browser and an embeddable w

    eb view

  • 8/10/2019 Synapseindia Android Apps Development

    16/47

    Andoid @untime

    16

    The core of ndroid platformDalvi" 0irtual Machine

    @e'ister?based

    :-ecutes files in the Dalvi" :-ecutable %;de-& format

    /ava core 9ibraries

    Provides most of the functionalit! of the /ava pro'rammin' lan'ua'e;

  • 8/10/2019 Synapseindia Android Apps Development

    17/47

    Android @untime %cont;&

    17

    The functions of /ava core libraries rel! on the Dalvi" 0M and the underl!in' 9inu- "ernel

    Multiple Dalvi" 0Ms ma! run at the same time

    :ver! Android application runs in its own process* with its own instance of the Dalvi" virtual machine

    The Gd-G tool in Android $D2 can transform compiled /A0A class into the ;de- format

  • 8/10/2019 Synapseindia Android Apps Development

    18/47

    Dalvi" 0irtual Machine

    18

    Android custom implementation virtual machineProvides application portabilit! and runtime consistenc!

    @uns optimi1ed file format %;de-& and Dalvi" b!tecode

    /ava ;class ; ar files converted to ;de- at build timeDesi'ned for embedded environment

    $upports multiple virtual machine processes per device

    Hi'hl! CPB?optimi1ed b!tecode interpreter:fficientl! Bsin' runtime memor!

    Core 9ibraries

    Core AP s for /ava lan'ua'e provide a powerful* !et simple and familiar develo ment latform

  • 8/10/2019 Synapseindia Android Apps Development

    19/47

    D0M vs; /0M

    19

    D0M+oo'le

    Dalvi" e-ecutable

    #nl! supports a subset of standard /ava 9ibrar! /0M

    $un

    /ava b!tecode$ome worries that /ava world ma! be divided into different communities* each has its own /ava standard

  • 8/10/2019 Synapseindia Android Apps Development

    20/47

    Application 4ramewor"

    20

    $implif! the reuse of components

    Applications can publish their capabilities and an! otherapplication ma! then ma"e use of those capabilities

    Applications is a set of services and s!stems* include

    0iews s!stem* content providers* resources mana'er and so on

  • 8/10/2019 Synapseindia Android Apps Development

    21/47

    Application 4ramewor" %cont;&

    21

    ctivit! Mana'er* mana'es the lifec!cle of applications and provides a common navi'ation bac"stac"

    Aotification Mana'er* enables all applications to displa! custom alerts in the status bar

    @esource Mana'er * providin' access to non?code resources such as locali1ed strin's* 'raphics* and la!out files

    Content Providers* access data from other applications %such as Contacts&* or to share their own data

    0iews* used to build an application* includin' lists* 'rids* te-t bo-es* buttons* and even an embeddableweb browser

  • 8/10/2019 Synapseindia Android Apps Development

    22/47

    Applications

    22

    A set of core applications shipped with Android platf orm

    an email client* $M$ pro'ram* calendar* maps* browser*

    contacts* and othersAll written in /ava

    #ur applications are in the same level as these appli

    cations

  • 8/10/2019 Synapseindia Android Apps Development

    23/47

    Android software development

    23

  • 8/10/2019 Synapseindia Android Apps Development

    24/47

    Development :nvironment

    D: I :clipse:clipse plu'?in ? ADT

    $oftware Development 2it %$D2&

    Android :mulatorDebu''er

  • 8/10/2019 Synapseindia Android Apps Development

    25/47

    $etup Android $D2

    25

    Download Android $D2 and e-tract the 1ip file to anarbitrar! folder

    http6 androidappdocs;appspot;com sd" inde-;html

    :;';6 e-tract to C6J

    The $D2 will be used b! ADT in eclipse

    http://androidappdocs.appspot.com/sdk/index.htmlhttp://androidappdocs.appspot.com/sdk/index.html
  • 8/10/2019 Synapseindia Android Apps Development

    26/47

    $etup ADT plu'in

    26

    nstall :clipse ADT plu'in

    :clipse must be /7:: edition* 8;K recommended

    Bpdate site6https6 dl?ssl;'oo'le;comandroid eclipse

    nstall all the plu'ins in th

    e repositor!@estart needed after installation

    https://dl-ssl.google.com/android/eclipse/https://dl-ssl.google.com/android/eclipse/https://dl-ssl.google.com/android/eclipse/https://dl-ssl.google.com/android/eclipse/
  • 8/10/2019 Synapseindia Android Apps Development

    27/47

    Confi'ure ADT Plu'in

    27

    #pen eclipse Window?LPreferences* select Android

    $etup the $D2 location as the folder where !ou e-tracted the downloaded $D2 1ip file

  • 8/10/2019 Synapseindia Android Apps Development

    28/47

  • 8/10/2019 Synapseindia Android Apps Development

    29/47

    $etup :mulators

    29

    After $D2 AP s installation*clic" 0irtual Devices

    Clic" new* there will be a di

    alo' input a name

    choose a runnin' tar'et anda s"in

    specif! the $D card si1e

  • 8/10/2019 Synapseindia Android Apps Development

    30/47

  • 8/10/2019 Synapseindia Android Apps Development

    31/47

    Hello World on Android

    31

  • 8/10/2019 Synapseindia Android Apps Development

    32/47

    Create a new Android Pro ect

    32

    #pen 4ile?L ew?LAndroid pro ect

    Pro ect name

    >uild Tar'et

    Application name

    Pac"a'e name

    Create Activit!

  • 8/10/2019 Synapseindia Android Apps Development

    33/47

    Hello World Pro ect

    33

    src6 source folder

    'en6 $D2 'enerated file

    android 7;76 reference lib

    assets6 binar! resources

    res6 resource files and resource description files

    AndroidManifest;-ml6 application description file

    default;properties6 pro ectproperties file

  • 8/10/2019 Synapseindia Android Apps Development

    34/47

    $a! Hello World

    34

    modif! HelloWorld; ava

  • 8/10/2019 Synapseindia Android Apps Development

    35/47

    @un Hello World

    35

    $elect HelloWorld Pro ect* @un?L@un as?LAndroid Application

    ADT will start a proper A0D and run HelloWorld app

    on it

  • 8/10/2019 Synapseindia Android Apps Development

    36/47

    >ehind HelloWorld 5)

    36

    @; ava* 'enerated b! Android $D2* represents all the resources of the app; resources are all inres folder

    resources are pre?compiled into binar! format

    /* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically !"!#at!$ %y th! * aa&t tool '#om th! #!sou#c! $ata it 'ou"$. It

    * shoul$ "ot %! mo$i'i!$ %y ha"$. */ package sample.hello; public final class R {

    public static final class attr { ( public static final class drawable { public static final int icon =0x7f020000; ( public static final class layout { public static final int main =0x7f030000; ( public static final class string { public static final int app_name =0x7f040001; public static final int hello =0x7f040000;

    ((

  • 8/10/2019 Synapseindia Android Apps Development

    37/47

    LinearLayout

    >ehind HelloWorld 57

    37

    res la!out * contains la!out declarations of the app*in M9 format* B s are built accordin' to the la!outfile

    main.xml) +ml ,!#sio" "1.0" encoding = "utf-8" ?> ) Li"!a#Layout +ml"s a"$#oi$ http://schemas.android.com/apk/res/android a"$#oi$ o#i!"tatio" "vertical"

    android:la out! idth = "fill!parent" a"$#oi$ layout h!i ht "fill!parent" >

    ) T!+t0i!w a"$#oi$ layout wi$th "fill!parent" a"$#oi$ layout h!i ht " rap!content"android:te#t = "$string/hello" />

    )/ Li"!a#Layout 1

    Text iew!di"p#ay "tatic

    text

    A referenceto $trin%

    re"ource&he##o'

    http://schemas.android.com/apk/res/androidhttp://schemas.android.com/apk/res/android
  • 8/10/2019 Synapseindia Android Apps Development

    38/47

    referencedin

    re"(#ayout()ain*x)#

    >ehind HelloWorld 58

    38

    res values* contains strin' declarations or other values%e;';6colors& of the app

    strin';-ml* contains strin' resources

    ) +ml ,!#sio" "1.0" encoding = "utf-8" ?> ) #!sou#c!s 1 ) st#i" "am! "hello" > %ello &orld' %ello&orld( )/ string > ) st#i" "am! "app!name" > %ello&orld )/ string > )/ #!sou#c!s 1

    referenced inAndroid+anife"t

    *x)#

  • 8/10/2019 Synapseindia Android Apps Development

    39/47

    >ehind HelloWorld 5E

    39

    res drawable* contains all ima'e resources

    folders ma! have suffi-es* app will choose the most suitable one* so do the other resources

    three folders6 drawable?ldpi* drawable?hdpi* drawable?mdpi* each contains an icon;pn' file

    app will choose the proper icon accordin' to the deviceDP

    reference name6Ndrawable icon

    other folders we ma! use in futuremenu* anim %animation&* -ml % preference and searchable&

  • 8/10/2019 Synapseindia Android Apps Development

    40/47

    >ehind HelloWorld 5K

    40

    AndroidManifest;-ml describe the application

    declare apps name* version* icon* permission* etc

    declare the applicationOs components6 activit!* service *receiver or provider) +ml ,!#sio" "1.0" encoding = "utf-8" ?>

    ) ma"i'!st +ml"s a"$#oi$ "http://schemas.android.com/apk/res/android"&ac2a ! "sample.hello" android:version*ode = "1" android:version+ame = "1.0" > ) a&&licatio" a"$#oi$ ico" "$dra a,le/icon" android:la,el = "$string/app!name" >

    ) acti,ity a"$#oi$ "am! ".%ello&orld" android:la,el = "$string/app!name" > ) i"t!"t-'ilt!# 1

    ) actio" a"$#oi$ "am! "android.intent.action. +" /> ) cat! o#y a"$#oi$ "am! "android.intent.categor . +*%234 /> )/ i"t!"t-'ilt!# 1

    )/ acti,ity 1)/ a&&licatio" 1) us!s-s$2 a"$#oi$ mi"3$20!#sio" "8" /> )/ ma"i'!st 1

  • 8/10/2019 Synapseindia Android Apps Development

    41/47

    Core Components?Activit! 5)

    41

    >asicall!* An activit! presents a visual user interface for one focused endeavor the user can underta"e

    An application mi'ht consist of ust one activit! or several* each Activit! is derived from android.app. ctivit and should be declared in AndroidManifest;-mlfile

    :ach activit! is 'iven a default window to draw in* the window ma! be full screen or smaller and on topof other window

    The visual content of the window is provided b! a hierarch! of views ob ects derived from the base 0i

  • 8/10/2019 Synapseindia Android Apps Development

    42/47

    Core Components?Activit! 57

    42

    Activities are activated b! as!nchronous messa'es called intents

    An intent is an ntent ob ect that holds the content of themessa'e

    The action bein' reQuested or the B@ of the data to acton

    The Rintent?filterL label in AndroidManifest;-ml file

    specifies the ntent that can start the Activit!

    declares the main activit!* it will be started automaticall!when the app starts

  • 8/10/2019 Synapseindia Android Apps Development

    43/47

    Activit! lifec!cle

    43

    h

  • 8/10/2019 Synapseindia Android Apps Development

    44/47

    #ther Core Components

    44

    $ervice

    A service doesnOt have a visual user interface* runs in thebac"'round for a period of time

    >roadcast receivers

    a component that does nothin' but receive and react tobroadcast announcements

    Content providers

    A content provider ma"es a specific set of the applicationOs data available to other applications;

    The data can be stored in the file s!stem* in an $=9ite database* or in an! other manner that ma"es sense

    d ll ld )

  • 8/10/2019 Synapseindia Android Apps Development

    45/47

    >e!ond HelloWorld 5)

    45

    >uild up an app that !ou can input !our 'reetin's and displa! !our 'reetin's

    nput6 :ditTe-t

    Displa!6 Te-t0iew

    #f course* we have to add an button

    :dit res la!out main;-ml file to add these components

    each has an android6id propert!* used to reference it in code

    ! d H ll W ld 57

  • 8/10/2019 Synapseindia Android Apps Development

    46/47

    >e!ond HelloWorld 57

    46

    modif! HelloWorld; ava

    firstl! 'et the references declared in main;-ml

    then add event response for >utton

    ! d H ll W ld 58

  • 8/10/2019 Synapseindia Android Apps Development

    47/47

    >e!ond HelloWorld 58

    4inishedS

    @un?L@un as?LAndroid Application