Introduction to Android

flosssnailsMobile - Wireless

Dec 10, 2013 (3 years and 7 months ago)

88 views

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Introduction to Android


Overview of Android System


Android Components


Component lifecycles



Slides rely heavily on
http://developers.android.com


10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Android Introduction


Android OS is based on Linux Kernel.


Aimed at Mobile devices.


It’s a phone (though tablets are now out there!)


Touch screen (now multi touch).


Small internal memory (~256Mbyte).


Low power devices.


Access to multiple sensor


Accelerometers (up down etc)


Proximity


light


Magnetic


Geo location (GPS etc)


Resources defined in XML documents rather than inside code.


Visible app has precedence over all!


Android market = I
-
Store?

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Android OS Features


Application framework

enabling reuse and replacement of components


Dalvik virtual machine

optimized for mobile devices


Integrated browser

based on the open source
WebKit

engine


Optimized graphics

powered by a custom 2D graphics library; 3D
graphics based on the OpenGL ES 1.0 specification (hardware
acceleration optional)


SQLite

for structured data storage


Media support

for common audio, video, and still image formats
(MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)


GSM Telephony

(hardware dependent)


Bluetooth, EDGE, 3G, and WiFi

(hardware dependent)


Camera, GPS, compass, and accelerometer

(hardware dependent)


Rich development environment

including a device emulator, tools for
debugging, memory and performance profiling, and a plugin for the
Eclipse IDE

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Android OS Structure

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Android Application Components


Components


Activities


Services


Broadcast Receivers



Content Providers


Intents are used to activate components


Can use existing apps within your app!


Intent Filters define what a component can do


The Manifest file


10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Activity


A visual user interface for one action (
Activity

base class
)


Eg a Text messaging app might have activities to


show list of contacts


write message


review old messages


change settings


They work together but each is independent


One of the activities in identified as the first to be
launched.


Moving from one activity to another is accomplished by
the current activity starting the next one.


Each activity has a default window to draw in.


Can be whole screen or float on top.


Can make use of additional windows.

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Views


Content of a window is a hierarchy of views
(
View

Class
).


Each view controls a rectangular space within the
window.


Parent view contain children views.


Ready made views to use include:
-


Buttons


Text Fields


Scroll bars


Menu items


Check boxes etc


View hierarchy added by


Activity.setContentView()
method.

A contains

B Contains

C Contains

D detail

E detail

Fdetail

G detail

H detail

K detail

I detail

J detail

L

M

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Services


A service does not have a visual user interface


Runs in the background for an indefinite period


Eg service might play background music as user does something
else.


Might fetch data over the network


Calculate something


Provide a result to an activity


Each service extends the
Service

base class


Services run in the main thread of the application
process.


Don’t block other components or user interface


Often spawn another thread for time consuming tasks

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Broadcast Receivers


A component that does nothing but receive and react to
broadcast announcements.


Many broadcasts originate in system code


Eg timezone change announcement


Battery low announcement


Picture has been taken announcement


Applications can initiate broadcasts


Data has been downloaded and ready to use


An application can have any number of broadcast receivers


Receivers extend the
BroadcastReceiver

base class
.


They have no user interface but


Might start an activity in response to the information received


Alert the user via the
NotificationManager
.

Flashing the backlight, vibrating the device, playing a sound persistent icon in status
bar

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Content Providers


A
content provider
makes a specific set of the application’s
data to another application.


The data can be stored


in the file system


In an
SQLite

database


Some other manner that makes sense.


The content provider extends the
ContentProvider

base class.


Applications use a
ContentResolver

object to get at a content
provider.


Whenever there is a request to be handled by a particular
component the Android OS ensures


it is running or


starting it or


creating a new instance if necessary



10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Intents


Intents

are asynchronous messages that activate



activities, services and broadcast receivers.


For activities and services it


Names the action being requested


Specifies the URI of the data to act on


Allow user to edit some specific text


For broadcast receivers it


names the action being announced


The camera button has been pressed


Each type of component is activated by sending an intent object to


Activity
-

Context.startActivity()

or
Activity.startActivityForResult()


Android calls the activity’s
onNewIntent()
method and passes it the intent object


Service


Context.startService()


Android calls the services
OnStart()
method and passes it the intent object


Broadcast Receiver


Context.sendBroadcast()


Android delivers the intent to all interested broadcast receivers by calling their
Onreceive()
method.

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

The Manifest File


Applications declare their components in a
manifest file bundled in the Android package
.apk


The manifest is an XML file.


It also


Names any libraries needed to run app


Identify any permissions the app needs


Declares intent filters (what can the app do)


10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Example Manifest document


<?xml version="1.0" encoding="utf
-
8"?>

<manifest . . . >





<application . . . >









<activity android:name="com.example.project.FreneticActivity"



















android:icon="@drawable/small_pic.png"



















android:label="@string/freneticLabel"



















. . .

>













<intent
-
filter . . . >

















<action android:name="android.intent.action.MAIN" />

















<category android:name="android.intent.category.LAUNCHER" />













</intent
-
filter>













<intent
-
filter . . . >

















<action android:name="com.example.project.BOUNCE" />

















<data android:mimeType="image/jpeg" />

















<category android:name="android.intent.category.DEFAULT" />













</intent
-
filter>









</activity>









. . .





</application>

</manifest>

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Activity Lifecycle


An activity has three states


Active

or
running

when in the foreground ie has the focus for the user’s
actions


Paused

if it has lost focus but is still visible


A paused activity is completely alive


Can be killed by the system in extreme low memory situations


Stopped

if completely obscured by another activity.


It still retains all state and member information


Often killed by the system when memory needed elsewhere


As activity state changes various methods called:
-


onCreate()


onStart()


onRestart()


onResume()


onPause()


onStop()


onDestroy()

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Activity Starts

Process is killed

Activity is running

Activity is no longer visible

Activity is shut down

OnCreate
()

OnResume
()

OnDestroy
()

OnStop
()

OnStart()

OnPause
()

Another Activity in front

Activity comes
to the
Foreground

Activity
comes to the
Foreground

User navigates
back to the
activity

Other Applications
need memory

OnRestart
()

Activity Lifecycle

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Service and Broadcast Receiver Lifecycle


A service


can be started or stopped


Methods available


onCreate()


onStart()


onDestroy()


If the service permits others to bind it additional methods are


onBind()


onUnbind()


onRebind()


A Broadcast receiver has on one callback method


onReceive()


10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Service is started by
startService()

Service is running

Service is shut down

OnCreate
()

OnDestroy
()

OnStart()

The
dervice

is stopped

(no
callback
)

Service Lifecycle

Service is started by
bindService()

Service is shut down

OnCreate
()

OnDestroy
()

OnBind
()

Client interacts with the service

OnUnbind
()

OnRebind
()

10/12/2013

E.R.Edwards
10/12/2013


Staffordshire University

School of Computing

Summary


Android is open source


anyone can join in!


Fairly radical change in perspective making
programming interesting!


Apps can use other apps as content providers


Very powerful emulator to develop on integrated with
Eclipse IDE.


Google have launched AppInventor site


interesting way
to get started (maybe?)


Battle for dominance between iPhone and Android


What about Microsoft Windows Mobile, RIM, Symbian