App Model

download App Model

of 45

Transcript of App Model

  • 7/29/2019 App Model

    1/45

  • 7/29/2019 App Model

    2/45

  • 7/29/2019 App Model

    3/45

  • 7/29/2019 App Model

    4/45

  • 7/29/2019 App Model

    5/45

  • 7/29/2019 App Model

    6/45

    6

    Components - Service

    A service does not have a visual user interface, but rather runs inthe background for an indefinite period time.

    Example: music player, network download, etc

    Each service extends the Service base class.

    It is possible to bind to a running service and start the service ifit's not already running.

    While connected, it is possible communicate with the servicethrough an interface defined in an AIDL (Android Interface

    Definition Language).

    Media Player

    Activity

    Service

    Background runningfor playback

    Binder

    Notification

    Communication

    Pause/re

    wind

    /stop/restart

  • 7/29/2019 App Model

    7/45

  • 7/29/2019 App Model

    8/458

    Components - Broadcast Receivers

    A broadcast receiver is a component that receives and reacts tobroadcast announcements (Intents).

    Many broadcasts originate in system code.

    E.g. announcements that the time zone has changed, that

    the battery is low, etc.

    SMS

    Broadcast Receiver Activity

    Get incoming calls

    Get incoming SMS

  • 7/29/2019 App Model

    9/459

    Components - Broadcast Receivers (Cont)

    A broadcast receiver is a component that receives and reacts tobroadcast announcements. (Cont)

    Applications can also initiate broadcasts.

    E.g. to let other applications know that some data has been

    downloaded to the device and is available for them to use.

    All receivers extend the BroadcastReceiverbase class.

  • 7/29/2019 App Model

    10/45

  • 7/29/2019 App Model

    11/4511

    Components - Content Providers (Cont)

    Using a content provider is the only way to share data betweenAndroid applications.

    It extends the ContentProvider bas class and implements astandard set of methods to allow access to a data store.

    Querying Delete, update, and insert data

    Applications do not call these methods directly.

    They use a ContentResolver object and call its methodsinstead.

    A ContentResolver can talk to any content provider.

    Content is represented by URI and MIME type.

  • 7/29/2019 App Model

    12/4512

    Intents

    Intents are simple message objects each of which consists of

    Action to be performed(MAIN/VIEW/EDIT/PICK/DELETE/DIAL/etc)

    Data to operate on (URI)

    startActivity(new Intent(Intent.VIEW_ACTION, Uri.parse("http://www.fhnw.ch"));startActivity(new Intent(Intent.VIEW_ACTION, Uri.parse("geo:47.480843,8.211293"));startActivity(new Intent(Intent.EDIT_ACTION,Uri.parse("content://contacts/people/1"));

  • 7/29/2019 App Model

    13/4513

    Intents (Cont)

    Intent Filters

    A component's intent filters in the manifest file informAndroid of the kinds of intents the component is able tohandle.

    An example

  • 7/29/2019 App Model

    14/4514

    Intents (Cont)

    Intent Filters (Cont)

    An example (Cont)

    A component can have any number of intent filters, eachone declaring a different set of capabilities.

    The first filter in the example indicates that theactivity is the entry point for the application.

    The second filter declares an action that the activitycan perform on a particular type of data.

  • 7/29/2019 App Model

    15/4515

    Android Component Model

    An Android application is packaged in a .apk file.

    A .apk file is a collection of components.

    Components share a Linux process: by default, one processper .apk file.

    .apk files are isolated and communicate with each other viaIntents or AIDL.

    Every component has a managed lifecycle.

    Application

    (.apk)Process

    Activity Activity

    Activity Activity

    Content Provider

    Service Service

  • 7/29/2019 App Model

    16/45

  • 7/29/2019 App Model

    17/4517

    Activities and Tasks (Cont)

    Tasks

    A task is a collection of related Activities.

    It is capable of spanning multiple processes.

    Application (.apk)

    Process

    Activity Activity

    Activity Activity

    Content Provider

    Service Service

    Application (.apk)

    Process

    Activity Activity

    Activity Activity

    Content Provider

    Service Service

  • 7/29/2019 App Model

    18/45

  • 7/29/2019 App Model

    19/4519

    Activities and Tasks (Cont)

    Affinities

    An affinity means a preference for each activity to belong toa certain task.

    An individual affinity can be set for each activity:

    By default, a new activity is launched into the task of theactivity that called startActivity().

  • 7/29/2019 App Model

    20/45

  • 7/29/2019 App Model

    21/45

    21

    Activities and Tasks (Cont)

    Launch Modes

    There are four launch modes:

    standard (default) / singleTop / singleTask / singleInstance

    A launch mode can be set for each activity:

  • 7/29/2019 App Model

    22/45

    22

    Activities and Tasks (Cont)

    Launch Modes (Cont)

    The modes differ from each other on four points:

    Which task will hold the activity that responds to theintent

    New Activity

    Activity A

    Root Activity

    Original Task

    Activity A

    Root Activity

    Original Task

    New Activity

    New Task

    standard/singleTopwithout

    FLAG_ACTIVITY_NEW_TASKsingleTask/singleInstance

  • 7/29/2019 App Model

    23/45

  • 7/29/2019 App Model

    24/45

  • 7/29/2019 App Model

    25/45

    25

    Activities and Tasks (Cont)

    Launch Modes (Cont)

    The modes differ from each other on four points: (Cont)

    Whether a new instance of the class will be launched tohandle a new intent

    Activity B

    Activity A

    Activity C

    Original Task

    Activity DAn intent arrives foran activity of type D

    Activity B

    Activity A

    Activity C

    Activity D

    Activity D

    If D is"standard"

    Activity B

    Activity A

    Activity C

    Activity D

    If D is"singleTop"

    The existinginstance D is

    expected to handlethe new intent

    (since it's at the topof the stack)

  • 7/29/2019 App Model

    26/45

  • 7/29/2019 App Model

    27/45

    27

    Activities and Tasks (Cont)

    Launch Modes (Cont)

    The modes differ from each other on four points: (Cont)

    Whether a new instance of the class will be launched tohandle a new intent(Cont)

    Activity B

    Original Task

    An intent arrives foran activity of type B

    If B is"singleInstance"

    A "singleInstance"activity is always atthe top of the stack,

    so it is always inposition to handle

    the intent.

    Activity B

  • 7/29/2019 App Model

    28/45

  • 7/29/2019 App Model

    29/45

    29

    Activities and Tasks (Cont)

    Clearing the Stack

    Default Control

    If the user leaves a task for a long time, the systemclears the task of all activities except the root activity.

    Some activity attributes that can be used to modify thedefault control:

    If alwaysRetainTaskState is set to the root activity of atask

    The task retains all activities in its stack even after a long period.

    If clearTaskOnLaunch is set to the root activity of a task The stack is cleared down to the root activity whenever the user

    leaves the task and returns to it.

    The user always returns to the task in its initial state, even after amomentary absence.

  • 7/29/2019 App Model

    30/45

    30

    Activities and Tasks (Cont)

    Clearing the Stack (Cont)

    Some activity attributes that can be used to modify thedefault control: (Cont)

    If finishOnTaskLaunch is set to an activity of a task

    - The activity remains part of the task only for the current session.- If the user leaves and then returns to the task, it no longer is present.

    Another way to force activities to be removed from thestack (FLAG_ACTIVITY_CLEAR_TOP flag):

    If an intent includes the FLAG_ACTIVITY_CLEAR_TOP flag and the

    target task already has an instance of the type of activity that should

    handle the intent in its stack, all activities above that instance are cleared

    away.

  • 7/29/2019 App Model

    31/45

    31

    Activities and Tasks (Cont)

    Starting Tasks

    How to set up an activity as the entry point of a task

  • 7/29/2019 App Model

    32/45

    32

    Processes and Threads

    Processes

    When the first of an application's components needs to berun, Android starts a Linux process for it with a single threadof execution (Main Thread).

    Android may decide to kill a process to reclaim resources.

    Application

    (.apk)Process Main Thread1 1

  • 7/29/2019 App Model

    33/45

    33

    Processes and Threads (Cont)

    Process (Cont)

    We can specify a process where an individual componentshould run by setting a process name to process attribute of

    , , , or .

    Each component can run in its own process.Some components share a process while others do not.Components of different applications also can run in the

    same process. We can set a default value that applies to all components by

    setting a default process to "process attribute of.

  • 7/29/2019 App Model

    34/45

  • 7/29/2019 App Model

    35/45

    35

    Processes and Threads (Cont)

    Threads (Cont)

    Anything that may not be completed quickly should beassigned to a different thread.

    Threads are created in code using standard Java Threadobjects.

    Some convenience classes Android provides for managingthreads:

    Looper for running a message loop within a thread

    Handler for processing messages

    HandlerThread for providing a handy way for starting anew thread that has a looper

  • 7/29/2019 App Model

    36/45

    36

    Component Lifecycles

    Activity Lifecycle Three states

    If an activity is paused or stopped, the system can drop itfrom memory either by:

    1. asking it to finish (calling its finish()method)

    2. simply killing its process.

    State Description

    Running An activity is in the foreground of the screen (at the top of

    the activity stack for the current task).Paused An activity has lost focus but is still visible to the user.

    Stopped An activity is completely obscured by another activity. It still retains all state and member information.

  • 7/29/2019 App Model

    37/45

    37

    Component Lifecycles

    Activity Lifecycle (Cont) An activity's overall lifecycle

    onCreate()

    Called when the activity isfirst created or when theactivity was killed

    onStart()

    Called just before theactivity becomes visible to

    user

    onRestart()

    Called after the activity hasbeen stopped, just prior toit being started again

  • 7/29/2019 App Model

    38/45

    38

    Component Lifecycles (Cont)

    Activity Lifecycle (Cont) An activity's overall lifecycle (Cont)

    onResume()

    Called just before the activity starts interacting with the user

    At this point, the activity is at the top of the activity stack, withuser input going to it.

    onPause()

    Called when the system is about to start resuming another activity

    This method is typically used to commit unsaved changes to

    persistent data, stop animations and other things that may beconsuming CPU, and so on.

  • 7/29/2019 App Model

    39/45

    39

    Component Lifecycles (Cont)

    Activity Lifecycle (Cont) An activity's overall lifecycle (Cont)

    onStop()

    Called when the activity is no longer visible to the user

    This may happen because it is being destroyed, or because anotheractivity has been resumed and is covering it.

    onDestroy()

    Called before the activity is destroyed

  • 7/29/2019 App Model

    40/45

    40

    Component Lifecycles (Cont)

    Activity Lifecycle (Cont) Three nested loops for the entire lifecycle

    Visible Lifetime

    During this time, the user can see the activity on-screen, though itmay be in the foreground and interacting with the user.

    onStart() and onStop() can be called multiple times, as the activityalternates between being visible and hidden to the user.

    Foreground Lifetime

    During this time, the activity is in front of all other activities onscreen and is interacting with the user.

  • 7/29/2019 App Model

    41/45

    41

    Component Lifecycles (Cont)

    Service Lifecycle Two ways that a service can be used

    The service can be started and allowed to run untilsomeone stops it or it stops itself.

    started by calling Context.startService() and stopped by callingContext.stopService()

    The service can be operated programmatically using aninterface that it defines and exports.

    Clients establish a connection to the Service object and use thatconnection to call into the service.

    established by calling Context.bindService() and closed by callingContext.unbindService()

  • 7/29/2019 App Model

    42/45

    42

    Component Lifecycles (Cont)

    Service Lifecycle (Cont)

  • 7/29/2019 App Model

    43/45

  • 7/29/2019 App Model

    44/45

    44

    Component Lifecycles (Cont)

    Processes and Lifecycles Android tries to maintain a process for as long as possible,

    but eventually it will need to remove old processes whenmemory runs low.

    To determine candidates to be killed, Android places eachprocess into an "importance hierarchy" based on thecomponents running in it and the state of those components.

  • 7/29/2019 App Model

    45/45

    Component Lifecycles (Cont)

    Processes and Lifecycles (Cont) Five levels in the Importance Hierarchy