Intro to Android OS - Staffordshire University

Download Report

Transcript Intro to Android OS - Staffordshire University

Introduction to Android
•
•
•
•
Overview of Android System
Android Components
Component lifecycles
Slides rely heavily on
http://developers.android.com
E.R.Edwards 03/04/2016
03/04/2016
Staffordshire University
School of Computing
Android Introduction


Android OS is based the Linux Kernel
Aimed at small-scale devices
–
–
–
–

Phones, tablets, TVs, games consoles…
Touch screen or mouse/pointer
Small internal memory (256Mbyte to 3GB)
Usually low power devices
Access to multiple sensors
– Accelerometers (up down etc)
– Proximity, Light, Magnetic
– Geo location (GPS etc)

Resources defined in XML documents rather than inside code
E.R.Edwards 03/04/2016
03/04/2016
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 (at least) OpenGL ES 1.0 specification, hardware
acceleration optional but common
SQLite for structured data storage
Media support for common audio, video, and still image formats
Telephony, Bluetooth 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
E.R.Edwards 03/04/2016
03/04/2016
Staffordshire University
School of Computing
Android OS Structure
Android Application Components

Components
– Activities – most important for us
– Services, “Broadcast Receivers”, “Content Providers”

“Intents” are used to activate components
– Can use existing apps within your app, very easy to
do
Intent Filters define what a component can do
 The Manifest file is where most app capabilities
are declared

E.R.Edwards 03/04/2016
03/04/2016
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.
E.R.Edwards 03/04/2016
03/04/2016
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.
A contains
Parent view contain children views.
B Contains
C Contains
Ready made views to use include:H detail
D detail
–
–
–
–
–

Buttons
Text Fields
Scroll bars
Menu items
Check boxes etc
I detail
E detail
J detail
Fdetail
K detail
G detail
L
M
Build in the GUI designer!
E.R.Edwards 03/04/2016
03/04/2016
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
E.R.Edwards 03/04/2016
03/04/2016
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.
Notifications are often used by them.
E.R.Edwards 03/04/2016
03/04/2016
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

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.
E.R.Edwards 03/04/2016
03/04/2016
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)
E.R.Edwards 03/04/2016
03/04/2016
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>
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()
E.R.Edwards 03/04/2016
03/04/2016
Staffordshire University
School of Computing
Activity Starts
User navigates
back to the
activity
Activity Lifecycle
OnCreate()
OnStart()
OnRestart()
OnResume()
Process is killed
Activity is running
Activity comes
to the
Foreground
Another Activity in front
Other Applications
need memory
OnPause()
Activity is no longer visible
OnStop()
OnDestroy()
Activity is shut down
Activity
comes to the
Foreground
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 or Android Studio.
MIT has the AppInventor2 site – interesting way to get
started
Battle for dominance between iPhone and Android?
– Anybody else in the running?
E.R.Edwards 03/04/2016
03/04/2016
Staffordshire University
School of Computing