10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Introduction to Android
Overview of Android System
Android Components
Component lifecycles
Slides rely heavily onhttp://developers.android.com
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Android Introduction
FAndroid OS is based the Linux Kernel
FAimed at small-scale devices
Phones, tablets, TVs, games consoles…
Touch screen or mouse/pointer
Small internal memory (256Mbyte to 3GB)
Usually low power devices
FAccess to multiple sensors
Accelerometers (up down etc)
Proximity, Light, Magnetic
Geo location (GPS etc)
FResources defined in XML documents rather than inside code
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Android OS Features
FApplication framework enabling reuse and replacement ofcomponents
FDalvik virtual machine optimized for mobile devices
FIntegrated browser based on the open source WebKit engine
FOptimized graphics powered by a custom 2D graphics library; 3Dgraphics based on (at least) OpenGL ES 1.0 specification, hardwareacceleration optional but common
FSQLite for structured data storage
FMedia support for common audio, video, and still image formats
FTelephony, Bluetooth and WiFi (hardware dependent)
FCamera, GPS, compass, and accelerometer (hardware dependent)
FRich development environment including a device emulator, toolsfor debugging, memory and performance profiling, and a plugin forthe Eclipse IDE
Android OS Structure
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Android Application Components
FComponents
Activities – most important for us
Services, “Broadcast Receivers”, “Content Providers”
F“Intents” are used to activate components
Can use existing apps within your app, very easy todo
FIntent Filters define what a component can do
FThe Manifest file is where most app capabilitiesare declared
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Activity
FA 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
FOne of the activities in identified as the first to belaunched.
FMoving from one activity to another is accomplished bythe current activity starting the next one.
FEach activity has a default window to draw in.
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Views
FContent of a window is a hierarchy of views (View Class).
FEach view controls a rectangular space within  thewindow.
FParent view contain children views.
FReady made views to use include:-
Buttons
Text Fields
Scroll bars
Menu items
Check boxes etc
FBuild in the GUI designer!
A contains
B Contains
C Contains
D detail
E detail
Fdetail
G detail
H detail
K detail
I detail
J detail
L
M
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Services
FA service does not have a visual user interface
FRuns in the background for an indefinite period
Eg service might play background music as user does somethingelse.
Might fetch data over the network
Calculate something
Provide a result to an activity
FEach service extends the Service base class
FServices run in the main thread of the applicationprocess.
Don’t block other components or user interface
Often spawn another thread for time consuming tasks
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Broadcast Receivers
FA component that does nothing but receive and react tobroadcast announcements.
FMany broadcasts originate in system code
Eg timezone change announcement
Battery low announcement
Picture has been taken announcement
FApplications can initiate broadcasts
Data has been downloaded and ready to use
FAn application can have any number of broadcastreceivers
FReceivers extend the BroadcastReceiver base class.
FNotifications are often used by them.
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Content Providers
Fcontent provider makes a specific set of theapplication’s data to another application.
FThe data can be stored
in the file system
In an SQLite database
Some other manner that makes sense.
FThe content provider extends the ContentProvider baseclass.
FApplications  use a ContentResolver object to get at acontent provider.
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Intents
FIntents are asynchronous messages that activate
 activities, services and broadcast receivers.
FFor 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
FEach type of component is activated by sending an intent object to
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 theirOnreceive() method.
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
The Manifest File
FApplications declare their components in amanifest file bundled in the Android package.apk
FThe manifest is an XML file.
FIt also
Names any libraries needed to run app
Identify any permissions the app needs
Declares intent filters  (what can the app do)
Example Manifest document
F<?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/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Activity Lifecycle
FAn activity has three states
Active or running  when in the foreground ie has the focus for the user’sactions
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
FAs activity state changes various methods called:-
onCreate(), onStart(),
onRestart(), onResume(),
onPause(), onStop(),
onDestroy()
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 comesto theForeground
Activitycomes to theForeground
User navigatesback to theactivity
Other Applicationsneed memory
OnRestart()
Activity Lifecycle
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Service and Broadcast ReceiverLifecycle
FA 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()
FA Broadcast receiver has on one callback method
onReceive()
Service is started bystartService()
Service is running
Service is shut down
OnCreate()
OnDestroy()
OnStart()
The dervice is stopped
(no callback)
Service Lifecycle
Service is started bybindService()
Service is shut down
OnCreate()
OnDestroy()
OnBind()
Client interacts with the service
OnUnbind()
OnRebind()
10/10/2015
E.R.Edwards 10/10/2015
Staffordshire University
School of Computing
Summary
FAndroid is open source – anyone can join in!
FFairly radical change in perspective makingprogramming interesting!
FApps can use other apps as content providers
FVery powerful emulator to develop on, integrated withEclipse IDE or Android Studio.
FMIT has the AppInventor2 site – interesting way to getstarted
FBattle for dominance between iPhone and Android?
Anybody else in the running?