Android Application's model - ReTiS Lab

tibburfrogtownMobile - sans fil

14 déc. 2013 (il y a 3 années et 5 mois)

56 vue(s)

Android Framework
How to use and extend it
Alberto Panizzo
2
Lecture 1

A glance at the past

What is Android and what devices it address

Android Stack and key features

Other ways to code in Android
Alberto Panizzo
3
Projects, groups, exam
Take a look at the Projects page of the website.
Alberto Panizzo
4
Lecture 2 Application environment

To start at the beginning: a glance at the boot process

Application model

Foreground entities: Activities

Background entities: Services, Broadcast receivers,
ContentProviders

Component lifecycles

How to trigger code execution: Intents

Lab Activity
Alberto Panizzo
5
Alberto Panizzo
6
It all starts with
init

Execute the initscript (
/init.rc
) launching:

System daemons like: adbd, rild, vold, keystore,
mediaserver ...

init
is the process launched by the kernel to manage
the userspace startup [1]
Alberto Panizzo
7
It all starts with
init

Execute the initscript (
/init.rc
) launching:

service_manager
: handle service registration and
lookup for
Binder

init
is the process launched by the kernel to manage
the userspace startup [1]
Alberto Panizzo
8
It all starts with
init

Execute the initscript (
/init.rc
) launching:

Zygote
: first instance of Dalvik VM which:

Load Java core libraries

Parse/verify installed apks

Fork to start the System Server

init
is the process launched by the kernel to manage
the userspace startup [1]
Alberto Panizzo
9
It all starts with
init

Execute the initscript (
/init.rc
) launching:

Zygote
: first instance of Dalvik VM which:

Load Java core libraries

Parse/verify installed apks

Fork to start the System Server

init
is the process launched by the kernel to manage
the userspace startup [1]
Alberto Panizzo
10
Alberto Panizzo
11
Launching Settings
Alberto Panizzo
12
Launching Settings
Alberto Panizzo
13
Lecture 2 Application environment

To start at the beginning: a glance at the boot process

Application model

Foreground entities: Activities

Background entities: Services, Broadcast receivers,
ContentProviders

Component lifecycles

How to trigger code execution: Intents

Lab Activity
Alberto Panizzo
14
Application model
An application is built using the following Components:

Java Components:

Activities, Services, BroadcastReceivers,
ContentProviders

Resources {XML, raw data}

Native Libraries

AndroidManifest.xml
Java components can react to events external to the
application, making it available even to
complete actions
requested by other applications
.
Alberto Panizzo
15
AndroidManifest.xml
Located on the root directory of any Android project.
Used to declare application specific information like:

Project and package name (
com.company.project
)

All the application components, enabling Android to index
them.

Security related declarations

...
[2]
Alberto Panizzo
16
AndroidManifest.xml
<?
xml

version
=
"1.0"

encoding
=
"utf-8"
?>
<
manifest
>
<
uses-permission

/>
<
permission

/>
<
uses-sdk

/>
<
application
>
<
activity
>

</
activity
>
<
service
>

</
service
>
<
receiver
>

</
receiver
>
<
provider
>

</
provider
>
<
uses-library

/>
</
application
>
</
manifest
>
Alberto Panizzo
17
Activities
An activity is a single, focused thing that the user can do:

Read a mail, show a photo ..
Almost all activities interact with users so the Activity is the
only component which can claim the screen:

Draws some UI

Can be full screen or floating

Typically manage events on the drawn UI
Android launches the application Activity marked as
Default
when it starts the application.
[3]
Alberto Panizzo
18
Service
A Service is a container to execute background tasks
without requiring a UI:

Play an audio file, download a file...
We can distinguish different levels of complexity:

Simple Service
: to Start/Stop

Simple Bound Service
: the caller holds a connection with
the Service and can use the standard Messenger
interface to exchange data.

Complex Bound Service
: the caller holds a connection
with the Service but it exposes a remote interface written
in AIDL, making it available to the caller for complex
interactions
Alberto Panizzo
19
BroadcastReceiver
Components which are able to react to events fired by:

System
es: Boot complete, Network connection available...

Other applications
es: I want to View a .pdf file...
Usually these components are sleeping and are activated
only from the events they are configured to react to.
[5]
Alberto Panizzo
20
ContentProvider
Components used to export and/or share data among the
applications running within the system.
Other entities connected to a content provider receive a
remote interface to query in a
SQL-like
way.
It doesn't matter the way the private data is stored (internal
database, application preferences, plain files)
Alberto Panizzo
21
Lecture 2 Application environment

To start at the beginning: a glance at the boot process

Application model

Foreground entities: Activities

Background entities: Services, Broadcast receivers,
ContentProviders

Component lifecycles

How to trigger code execution: Intents

Lab Activity
Alberto Panizzo
22
Application model - Implementation

Android make extensive use of
callbacks
called in
consequence of events:

Life cycle related events

System events

Callbacks are implemented using Inheritance

All the base components will be superclasses of your
application's ones
Alberto Panizzo
23
Activity Lifecycle
Alberto Panizzo
24
Activity Lifecycle
Alberto Panizzo
25
Service Lifecycle
Alberto Panizzo
26
Service Lifecycle
Alberto Panizzo
27
BroadcastReceiver Lifecycle
Has only one callback:
onReceive

Called in case an event that matches the registered filter is
fired.
Alberto Panizzo
28
Application model - Implementation

All callbacks of the same application are called from the
same thread called: application's
UI Thread
.

Everything which is blocking the UI thread will prevent the
application to react to the user input or draw the screen

So...
All callbacks must run as fast as possible!
Alberto Panizzo
29
Lecture 2 Application environment

To start at the beginning: a glance at the boot process

Application model

Foreground entities: Activities

Background entities: Services, Broadcast receivers,
ContentProviders

Component lifecycles

How to trigger code execution: Intents

Lab Activity
Alberto Panizzo
30
Trigger code execution: Intents
Intents
are objects that describe an action, used by
application components to trigger the execution of another
component. [6]

Explicit intents:

Implicit intents:
Fired with:
Intent in =
new
Intent(MyActivity.
class
);
Intent in =
new
Int
ent(
"ACTION”);
Intent in =
new
Intent(Intent.ACTION_CALL);
Context.startActivity(in);
Context.startService(in);
Context.sendBroadcast(in);
Alberto Panizzo
31
Trigger code execution: Intents
The
ActivityManager
resolves
intents into the correct
component
through
the
PackageManager
.

Explicit intents: straightforward.

Implicit intents: choosing from all the components
registered to receive the intent action.
If this is not enough:

Using the category properties:

Using the data properties (scheme/mimeType)
intent.addCategory(Intent.CATEGORY_BROWSABLE
)
intent.setType(MyClass.MY_MIME_TYPE)
Alberto Panizzo
32
Register to receive intents
IntentFilter
object is the programmatic tool used to enable
BroadcastReceivers to receive filtered intents
BroadcastReceiver r =
new
MyBR();
IntentFilter f =
new
IntentFilter();
f.addAction
(
“ACTION1”);
..
Context.registerReceiver(r, f);
Alberto Panizzo
33
Register to receive intents
Intent filters can be defined also in AndroidManifest.xml:
<
activity

android:name
=
".DemoActivity"
>

<
intent-filter
>

<
action

android:name
=
"android.intent.action.MAIN"

/>

<
category

android:name
=

"android.intent.category.LAUNCHER"

/>

</
intent-filter
>
</
activity
>
Alberto Panizzo
34
Lecture 2 Application environment

To start at the beginning: a glance at the boot process

Application model

Foreground entities: Activities

Background entities: Services, Broadcast receivers,
ContentProviders

Component lifecycles

How to trigger code execution: Intents

Lab Activity
Alberto Panizzo
35
Install the needed software
Use the following link as the base reference to install:

Android SDK

Eclipse

Android ADT
http://developer.android.com/sdk/index.html
Then we'll sync to install the reference application.
Alberto Panizzo
36
Links of interest
[1] https://sites.google.com/site/io/anatomy--physiology-of-an-android
[2] http://developer.android.com/guide/topics/manifest/manifest-intro.html
[3] http://developer.android.com/guide/topics/fundamentals/activities.html
[4] http://developer.android.com/guide/topics/fundamentals/services.html
[5] http://developer.android.com/reference/android/content/BroadcastReceiver.html
[6] http://developer.android.com/guide/topics/intents/intents-filters.html