Programming with Android:
System Architecture
Luca Bedogni Marco Di Felice
Dipartimento di Scienze dell’Informazione
Università di Bologna
Outline
Android Architecture: An Overview
Android Dalvik Java Virtual Machine
Android Components: Activities
Android Components: Intents
Android Components: Services
Android Components: Content Providers
Android Application Distribution and Markets
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 2
Android … What?
Android is a Linux-based platform for mobile
devices …
Operating System
Middleware
Applications
Software Development Kit (SDK)
Which kind of mobile devices … (examples)
SMARTPHONES TABLETS EREADERS ANDROID TV GOOGLE GLASSES
?
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 3
Android … When?
2005
Google buys Android from the Android Inch
2006 Open Handset Alliance (OHA) created for open
standards for mobile devices. Partners of OHA:
Google, Motorola, Samsung, Vodafone, T-Mobile, etc
2007
Android 1.0 Released
2008
The first Android smartphone: G1 HTC-Dream
2009
Android 1.1 Released
Android 1.5 (CupCake) Released
Time
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 4
Android … When?
2008 Android 1.6 (Donut) Released
Android 2.0 (Eclair) Released
2009
Android 2.2 (Froyo) Released
2010
Android 2.3 (Gingerbread) Released
2011 Android 3.0 (Honeycomb) Released
(First version for devices with larger screens such as tablets)
2012
Android 4.0 (Ice-Cream Sandwich) Released. (It
merges the 3.x tab centric design and the v2.x phone based design
Time into a single version.)
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 5
Android … When?
2012 Android 4.2 (Jelly Bean) Released
Gesture Mode for Accessibility
2013
Improved browser performance
Easy data-sharing through NFC
Improved camera and face recognition functionalities
KEY LIME PIE …
API Level 17 (Android 4.2):
Daydream: screensaver customization API
Support to multi-user environments
Nested fragments for UI improvements
Time …
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 6
Android … When?
ANDROID DISTRIBUTIONS ANDROID APPLICATIONS
http://developer.android.com/about/dashboards/index.html http://www.appbrain.com/stats/android-market-app-categories
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 7
Android … When?
http://en.wikipedia.org/wiki/Android_version_history
1.6-2.0 ANDROID VERSION
2.2.x HISTORY AND POPULARITY
2.3.x (2009-2013)
4.x
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 8
The Android Architecture
} Stack
Architecture
Open Source Architecture
(Apache/MIT License v. 2.0)
Business-friendly License
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 9
The Android Architecture
Built on top of Linux
kernel (v. 2.6-3.0)
Advantages:
Portability (i.e. easy to
compile on different
harwdare architectures)
Security (e.g. secure
multi-process environment)
Power Management
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 10
The Android Architecture
Native Libraries
(C/C++ code)
Graphics (Surface Manager)
Multimedia (Media Framework)
Database DBMS (SQLite)
Font Management
(FreeType)
WebKit
C libraries (Bionic)
….
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 11
The Android Architecture
Application Libraries
(Core Components of Android)
Activity Manager
Packet Manager
Telephony Manager
Location Manager
Contents Provider
Notification Manager
….
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 12
The Android Architecture
Applications
(Written in Java code)
Android Play Store
Entertainment
Productivity
Personalization
Education
Geo-communication
….
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 13
The Android Architecture
Dalvik Virtual
Machine (VM)
Novel Java Virtual
Machine implementation
(not using the Oracle JVM)
Open License (Oracle
JVM is not open!)
Optimized for memory-
constrained devices
Faster than Oracle JVM
….
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 14
Dalvik Java Virtual Machine (JVM)
Java Source Java Source
Java Standard Edition
Code Code
Java Java
Compiler Compiler
Java Byte Java Byte
Code Code
Dex
Compiler
Stack-based Dalvik Byte
byte-code Code
Register-based
byte-code
Java Virtual Dalvik Virtual
Machine (JVM) Machine (VM)
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 15
Android Applications Design
APPLICATION DESIGN:
GUI Definition
Events Management
Application Data Management
Background Operations
User Notifications
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 16
Android Applications Design
APPLICATION COMPONENTS
Activities
Intents
Services
Content Providers
Broadcast Receivers
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 17
Android Components: Activities
An Activity corresponds to a single screen
of the Application.
Android HelloWorld
An Application can be composed of multiples
Button1
screens (Activities).
Hello World! The Home Activity is shown when the user
launches an application.
Different activities can exhange information
one with each other.
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 18
Android Components: Activities
Each activity is composed by a list of graphics components.
Some of these components (also called Views) can interact
with the user by handling events (e.g. Buttons).
Two ways to build the graphic interface:
PROGRAMMATIC APPROACH
Example:
Button button=new Button (this);
TextView text= new TextView();
text.setText(“Hello world”);
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 19
Android Components: Activities
Each activity is composed by a list of graphics components.
Some of these components (also called Views) can interact
with the user by handling events (e.g. Buttons).
Two ways to build the graphic interface:
DECLARATIVE APPROACH
Example:
< TextView android.text=@string/hello” android:textcolor=@color/blue
android:layout_width=“fill_parent” android:layout_height=“wrap_content” />
< Button android.id=“@+id/Button01” android:textcolor=“@color/blue”
android:layout_width=“fill_parent” android:layout_height=“wrap_content” />
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 20
Android Components: Activities
EXAMPLE
- Build the application layout
through XML files (like HTML)
- Define two different XML layouts
for two different devices
Device 1 Device 2 - At runtime, Android detects the
HIGH screen pixel density LOW screen pixel density
current device configuration and
Java App Code loads the appropriate resources
for the application
- No need to recompile!
- Just add a new XML file if you
need to support a new device
XML Layout File XML Layout File
Device 1 Device 2
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 21
Android Components: Activities
Android applications typically use both the approaches!
DECLARATIVE APPROACH
Define the Application layouts and
XML Code resources used by the Application
(e.g. labels).
PROGRAMMATIC APPROACH
Manages the events, and handles
Java Code the interaction with the user.
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 22
Android Components: Activities
Views can generate events (caused by human interactions)
that must be managed by the Android-developer.
TextEdit
Button
ESEMPIO
public void onClick(View arg0) {
if (arg0 == Button) {
// Manage Button events
}
}
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 23
Android Components: Activities
The Activity Manager is responsible for
creating, destroying, managing activities.
Activities can be on different states:
starting, running, stopped, destroyed,
paused.
Only one activity can be on the running
state at a time.
Activities are organized on a stack, and
have an event-driven life cycle (details later …)
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 24
Android Components: Activities
Main difference between Android-programming and Java
(Oracle) -programming:
Mobile devices have constrained resource capabilities!
Activity lifetime depends on users’ choice (i.e. change of
visibility) as well as on system contraints (i.e. memory
shortage).
Developer must implement lifecycle methods to account
for state changes of each Activity …
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 25
Android Components: Activities
public class MyApp extends Activity { Called when the Activity
is created the first time.
public void onCreate() { ... }
public void onPause() { ... } Called when the Activity
public void onStop() { ... } is partially visible.
public void onDestroy(){ ... }
…. Called when the Activity
} is no longer visible.
Called when the Activity
is dismissed.
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 26
Android Components: Intents
Intents: asynchronous messages to activate core Android
components (e.g. Activities).
Explicit Intent The component (e.g. Activity1) specifies the
destination of the intent (e.g. Activity 2).
LOGIN Welcome Marco!
marco
Activity1
Activity2
PASSWORD
**********
Login Intent
Login
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 27
Android Components: Intents
Intents: asynchronous messages to activate core Android
components (e.g. Activities).
Implicit Intent The component (e.g. Activity1) specifies the
type of the intent (e.g. “View a video”).
Activity2
Multiple choices
might be available
}
Activity1
to the user!
Intent-
View Filters
Activity2
Implicit Intent
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 28
Android Components: Services
Services: like Activities, but run in background and do not
provide an user interface.
Used for non-interactive tasks (e.g. networking).
Service life-time composed of 3 states:
Starting Destroyed
onCreate()
onDestroy()
onStart()
Running
(on background)
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 29
Android Components: Content Providers
Each Android application has its own private set of data
(managed through files or through SQLite database).
Content Providers: Standard interface to access and
share data among different applications.
insert()
APP
update()
Content
delete() Provider DB
query() e.g. Photo Gallery
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 30
Android Components: Broadcast Receivers
Publish/Subscribe
paradigm
Broadcast Receivers:
An application can be
signaled of external
events.
Notification types: Call
incoming, SMS
delivery, Wifi network
detected, etc
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 31
Android Components: Broadcast Receivers
BROADCAST RECEIVER example
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
String s = new StringBuilder();
wifiList = mainWifi.getScanResults();
for(int i = 0; i < wifiList.size(); i++){
s.append(new Integer(i+1).toString() + ".");
s.append((wifiList.get(i)).toString());
s.append("\\n");
}
mainText.setText(sb);
}
}
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 32
Android Components: Broadcast Receivers
BROADCAST RECEIVER example
public class WifiTester extends Activity {
WifiManager mainWifi;
WifiReceiver receiverWifi;
List<ScanResult> wifiList;
public void onCreate(Bundle savedInstanceState) {
…
mainWifi = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
receiverWifi = new WifiReceiver();
registerReceiver(receiverWifi, new
IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
}
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 33
Android Components: System API
Using the components described so far, Android
applications can then leverage the system API …
SOME EXAMPLEs …
Telephony Manager data access (call, SMS, etc)
Sensor management (GPS, accelerometer, etc)
Network connectivity (Wifi, bluetooth, NFC, etc)
Web surfing (HTTP client, WebView, etc)
Storage management (files, SQLite db, etc)
….
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 34
Android Components: Google API
… or easily interface with other Google services:
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 35
Android Application Distribution
Each Android application is
contained on a single APK file.
APK
FILE Java Byte-code (compiled for
Dalvik JVM)
Resources (e.g. images. videos,
XML layout files)
XML C
Files
Libraries (optimal native C/C++
code)
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 36
Android Application Distribution
Each application must be signed
through a key before being
distributed.
Applications can be distributed
via Web or via Stores.
Android Play Store: application
store run by Google … but several
other application stores are
available (they are just normal
applications).
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 37
Android Application Security
Android applications run with a distinct system identity
(Linux user ID and group ID), in an isolated way.
Applications must explicitly share resources and data.
They do this by declaring the permissions they need for
additional capabilities.
Applications statically declare the permissions they require.
User must give his/her consensus during the installation.
ANDROIDMANIFEST.XML
<uses-permission android:name=“android.permission.IACCESS_FINE_LOCATION" />
<uses-permission android:name=“android.permission.INTERNET" />
Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 38