Вы находитесь на странице: 1из 133

DONG NAI UNIVERSITY OF TECHNOLOGY

1
5. Using Android OS / Devices
4. Introduction to Android OS
3. Software Installation
2. Mobile / Tablet OSs
1. Subject introduction
6. Eclipse debug with Android app
7. Components of the Android Application
8. Units of measurement
9. XML Primer
10. Applications Life Cycle
DONG NAI UNIVERSITY OF TECHNOLOGY
2
1. Subject introduction
1.1 Topics
1.2 Learning Objectives
1.3 Textbooks
1.4 Test / Grade
1.5 Assignments
1.6 Rules
DONG NAI UNIVERSITY OF TECHNOLOGY
3
1.1 Topics
Background
Software stack
Activities vs applications
Activity life cycles
Activity states
Resources
Notification Manager
Mobile/Tablet development application
DONG NAI UNIVERSITY OF TECHNOLOGY
4
1.1 Topics
User Interfaces
Views
Layouts
Widgets
UI XML specification
DONG NAI UNIVERSITY OF TECHNOLOGY
5
1.1 Topics
Intents, Receivers, Adapters
Explicit Intents
Implicit Intents
Event Broadcasting with Intents
Event Reception with Broadcast Receivers
Adapters and DataBinding
DONG NAI UNIVERSITY OF TECHNOLOGY
6
1.1 Topics
Files, Content Providers, Databases
Saving and Loading Files
SQLite Databases
Access Exposure to Data Sources through Content
Providers
Content Provider Registration
Native Content Providers
DONG NAI UNIVERSITY OF TECHNOLOGY
7
1.1 Topics
Common Android APIs
Networking APIs
LBS APIs
Web APIs
Multimedia APIs
Telephony APIs
And Sensors
DONG NAI UNIVERSITY OF TECHNOLOGY
8
1.2 Learning Objectives
Have an appreciation of the Android OS
Know how to develop basic UIs
Know how to broadcast and receive events
Know how to manage data storage through files,
databases, and content providers
Know how to implement basic location-based
services
Know how to use audio and video utilities
DONG NAI UNIVERSITY OF TECHNOLOGY
9
1.2 Learning Objectives
Know how to access the Internet
Know how to process URL data encoded in
HTML/XML
Know how to capture touch screen events and
recognize touch screen gestures
Become exposed to Android online resources and
communities
Know how to use android APIs, Sensors
DONG NAI UNIVERSITY OF TECHNOLOGY
10
1.3 Textbooks
[1] Unlocking Android
[2] The Busy Coders Guide to Android Development
[3]Beginning Android Application Development
WeiMengle Wrox -2011
[4] Android Wireless Development -Lauren Darcey ,
Shane Conder
[5] Professional Android Application Development
Malestrom
Web : http://developer.android.com
DONG NAI UNIVERSITY OF TECHNOLOGY
11
1.4 Test / Grade
Attendance
Mid term lab test
Final lab test
Homework
assignments in every week
Final Project
Final project will require technical writing (project
proposal submission, source documentation, description)
Code in java (Eclipse)
There will be reading suggestions
Final test
DONG NAI UNIVERSITY OF TECHNOLOGY
12
1.5 Assignments
All assignments will be submitted via Assembla
All assignments using namespace
vn.edu.dntu.it.Studentname.ID. labNo
vn.edu.dntu.it.lethihoa.0891233.Lab32
Grades will be submitted via Assembla
Late submission will not be accepted
DONG NAI UNIVERSITY OF TECHNOLOGY
13
1.5 Assignments
Server - Tools
MS Source safe
SVN server (open source)
MS Team Foundation server
Host
Server in Lan
Host throught internet
www.Googlecode (code.google.com) (public)
www.Assembla.com (public)
www.Projectlocker.com (public & private)
http://sourceforge.net
Client
TortoiseSVN
Visual studio
DONG NAI UNIVERSITY OF TECHNOLOGY
14
1.5 Assignments
http://tortoisesvn.net/downloads.html
Register account : http://www.assembla.com/ and create space to
submit assignments
Detail
DONG NAI UNIVERSITY OF TECHNOLOGY
15
1.6 Rules
- Attendance 75%
- Do homework
- Do lab project
- Research on internet
DONG NAI UNIVERSITY OF TECHNOLOGY
16
2. Mobile / Tablet OSs
Mobile
Symbian
Ubuntu
BlackBerry
Windows
Phone

Bada
MeeGo
IOS
WebOS
Android
DONG NAI UNIVERSITY OF TECHNOLOGY
17
Bada
Samsung
Closed source
http://developer.bada.com
DONG NAI UNIVERSITY OF TECHNOLOGY
18
C/C++ J2EM
http://www.developer.nokia.com/
DONG NAI UNIVERSITY OF TECHNOLOGY
19
Windows
Phone
http://dev.windowsphone.com
C#
VB.net
XAML
DONG NAI UNIVERSITY OF TECHNOLOGY
20
WebOS
from HP
open sourced
running on the Linux kernel
Visual Studio
https://developer.palm.com/
C/C++

Java
DONG NAI UNIVERSITY OF TECHNOLOGY
21
IOS
Apple
derived from Mac OS X.
Objective C iPhone SDk
https://developer.apple.com
DONG NAI UNIVERSITY OF TECHNOLOGY
22
BlackBerry
https://developer.blackberry.com/
DONG NAI UNIVERSITY OF TECHNOLOGY
23
Android
Google Inc
free and open source
Linux-derived OS
Java (Android SDK)
http://developer.android.com/
DONG NAI UNIVERSITY OF TECHNOLOGY
24
Ubuntu
http://www.ubuntu.com/devices/android
Professional Ubuntu
Mobile Development
DONG NAI UNIVERSITY OF TECHNOLOGY
25
MeeGo
open source
Nokia + Intel
https://meego.com/developers
C++
DONG NAI UNIVERSITY OF TECHNOLOGY
26
Mobile Characterized
Limited resources
Screen
CPU
RAM (no virtual)
No harddisk
Battery
Input data: many ways
Compact
Not much room
Reuse of common component is impotant
Phone functionality always highest priority
Always on
DONG NAI UNIVERSITY OF TECHNOLOGY
27
3. Software Installation
3.1 Introdution to software tools
3.2 Install Android SDK
DONG NAI UNIVERSITY OF TECHNOLOGY
28
3.1 Introdution to software tools
Android Environment:
Eclipse & ADT
The Android Development Tools (ADT) adds extensions to the
Eclipse IDE. It allows: create and debug Android applications easier
and faster.
Advantages:
It gives you access to other Android development tools from inside
the Eclipse IDE: take screenshots, Debug / set breakpoints, and view
thread and process information directly from Eclipse.
It provides a New Project Wizard (you quickly create and set up all
of the basic files you'll need for a new Android application.)
It automates and simplifies the process of building your Android
application.
It provides an Android code editor that helps you write valid XML
for your Android manifest and resource files.
export project into a signed APK, which can be distributed to users.
DONG NAI UNIVERSITY OF TECHNOLOGY
29
3.1 Introdution to software tools Android SDK
Tools that help you develop mobile applications on the
Android platform
Android Development Tools (ADT) Plugin
Android Emulator
Device
emulation tool that you can use to design, debug, and test
your applications in an actual Android run-time environment
without an actual phone
Android Virtual Devices(AVDs)
Virtual device configurations that you create, to model device
characteristics in the Android Emulator
Each AVD functions as an independent device with it's own
storage for user data, SD card, and so on.
DONG NAI UNIVERSITY OF TECHNOLOGY
30
3.1 Introdution to software tools
List of Software Tools
OS: Windows (XP or later), Linux (Ubuntu10.10), Mac
OS (10.x or later)
Java Development Kit (6 or later)
http://java.sun.com/javase/downloads/index.jsp
Android SDK
http://developer.android.com/sdk/index.html
Eclipse IDE
ADT Plugin for Eclipse IDE
DONG NAI UNIVERSITY OF TECHNOLOGY
31
3.1 Introdution to software tools
Java JDK and JRE
Java Development Kit (JDK)
Required to develop applications in Java
Contains a Java compiler, debugger, documentation tools,
and other tools
Java Runtime Environment (JRE)
Allows you to run a Java application
DONG NAI UNIVERSITY OF TECHNOLOGY
32
3.1 Introdution to software tools Why Eclipse
Integrated development environment (IDE)
Similar to Microsoft Visual Studio
Not required for Android development
Alternatives include
Command-line based tools only
Other IDEs, e.g., NetBeans
Google documentation refers to Eclipse extensively
DONG NAI UNIVERSITY OF TECHNOLOGY
33
3.1 Introdution to software tools
The Emulator
DONG NAI UNIVERSITY OF TECHNOLOGY
34
3.2 Install Android SDK
Step 1: Install Java Development Kit
download the JDK from
http://java.sun.com/javase/downloads/index.jsp
Step 2: Install Eclipse IDE
To download and install Eclipse IDE visit
http://www.eclipse.org/downloads/
Step 3: Download the Android SDK Starter Package
The Android SDK Starter Package includes the core SDK tools.
This helps in downloading and installing the rest of SDK
components.
download the Starter Package from
http://developer.android.com/sdk/index.html
Unpack the downloaded .zip file to any suitable location on your
computer. (ex: c:\android-sdk-windows)
DONG NAI UNIVERSITY OF TECHNOLOGY
35
3.2 Install Android SDK
Step 4: Install ADT Plugin for Eclipse
To work with Eclipse IDE you need to install Android
Development Tool (ADT).
To install this plugin Start Eclipse and select Help -> Install
New Software-> Click Add button -> Type URL
http://dl-ssl.google.com/android/eclipse/ for Location.
In the Available Softwares dialog, select the Developer Tools
to install the Android DDMS and Android development Tools.
DONG NAI UNIVERSITY OF TECHNOLOGY
36
3.2 Install Android SDK
Step 5: Set Android Preferences
In Eclipse IDE, select Window -> Preferences. Select
Android and browse to the location where Android SDK was
unpacked. Press OK button.
DONG NAI UNIVERSITY OF TECHNOLOGY
37
3.2 Install Android SDK
Step 6: Add SDK Components and APIs
Android SDK and AVD Manager is used to add or update
the SDK Components and APIs.
In Eclipse IDE, select Window -> Android SDK Manager ->
Available Packages.
Select the APIs you need to
develop your applications.
DONG NAI UNIVERSITY OF TECHNOLOGY
38
3.2 Install Android SDK
Step 6: Add SDK Components and APIs
DONG NAI UNIVERSITY OF TECHNOLOGY
39
3.2 Install Android SDK
Step 7: Create Virtual Devices
To run the emulator to run applications, you need to create
Virtual Devices. You can create multiple devices each with
different configurations as per your need.
To define an AVD, in Eclipse IDE,
select Window ->
Android Virtual Devices Manager.
Press New button and define the
configuration for your new virtual device.
DONG NAI UNIVERSITY OF TECHNOLOGY
40
3.2 Install Android SDK
Step 7: Create Virtual Devices
DONG NAI UNIVERSITY OF TECHNOLOGY
41
3.2 Install Android SDK
http://developer.android.com/sdk/installing/index.html
Read more
DONG NAI UNIVERSITY OF TECHNOLOGY
42
4. Introduction to Android OS
4.1 Reasons to go mobile application development
4.2 Introduction to Android
4.3 Background History
4.4 What is Open Handset Alliance?
4.5 Android vs Iphone
4.6 Android components
DONG NAI UNIVERSITY OF TECHNOLOGY
43
4.1 Reasons to go mobile application development
Smart Phones
Internet access anywhere
Social networking
Millions of mobile users
Open standards
DONG NAI UNIVERSITY OF TECHNOLOGY
44
Open software platform for mobile development
created by Google and the Open Handset Alliance.
A complete stack OS, Middleware, Applications, a
large market
Powered by Linux operating system
Fast application development in Java
Open source under the Apache 2 license
4.2 Introduction to Android
DONG NAI UNIVERSITY OF TECHNOLOGY
45
4.3 Background History
There exists a great variety of mobile operating
systems: Symbian OS, MS Windows Mobile, Mobile
Linux, iPhone OS, Moblin
Great market fragmentation
So far no single OS has become the defact to
standard (there will ever be one mobile OS; more likely
two or three major mobile OS's and a few minor ones)
DONG NAI UNIVERSITY OF TECHNOLOGY
46
4.3 Background History
In 2005, Google acquires the startup company
Android, Inc.
In 2005, the work begins on the Dalvik VM and other
components of the Android Platform
In 2007, the Open Handset Alliance is formed by a
group of companies(Sprint, Motorola, Samsung, Sony
Ericsson, Google, Intel, Texas Instruments, etc)
DONG NAI UNIVERSITY OF TECHNOLOGY
47
4.3 Background History
The Open Handset Alliance releases a lot of IP through
the Apache License, Version2.0
In Nov.2007, the first version of the Android SDK is
released
In Sept.2008, T-Mobile G1 becomes the 1st smartphone
with the Android platform (ver 1.0)
In Feb.2009, Android 1.1 is released
In Apr.2009, Android 1.5 Cupcake is released
In Sept.2009, Android 1.6 Donut is released
In Oct.2009, Android 2.0 Eclair is released
In Jan.2010, Android 2.1 Eclair is released
DONG NAI UNIVERSITY OF TECHNOLOGY
48
4.3 Background History
In May.2010, Android 2.2.x Froyo is released
In Dec.2010, Android 2.3.x Gingerbread is released
In Feb.2011, Android 3.x Honeycomb Froyo is released
In Oct.2011, Android 4.0.x Ice Cream Sandwich
is released
In Jul.2012, Android 4.1 Jelly Bean is released
In Nov.2012, Android 4.2 Jelly Bean is released
In July.2013, Android 4.3 Jelly Bean is released
The next Android 4.4 KitKat
DONG NAI UNIVERSITY OF TECHNOLOGY
49
4.3 Background History
Sept.2008
Feb.2009
Apr.2009
Oct.2009
May.2010
Dec.2010
DONG NAI UNIVERSITY OF TECHNOLOGY
50
4.3 Background History
Feb.2011
Oct.2011
Jul.2012
Nov.2012
DONG NAI UNIVERSITY OF TECHNOLOGY
51
4.3 Background History
24 July 2013
http://en.wikipedia.org/wiki/Android_version_history
Android 4.4
KitKat
(API level 19)
DONG NAI UNIVERSITY OF TECHNOLOGY
52
4.4 What is Open Handset Alliance?
a group of 47 (Maybe now >47) technology and mobile
companies have come together to accelerate innovation
in mobile and offer consumers a richer, less expensive,
and better mobile experience.
developed Android, the first complete, open, and free
mobile platform.
commercial deploy handsets and services using the
Android Platform.
DONG NAI UNIVERSITY OF TECHNOLOGY
53
4.4 What is Open Handset Alliance?
http://en.wikipedia.org/wiki/Open_Handset_Alliance
Open Handset Alliance Members
DONG NAI UNIVERSITY OF TECHNOLOGY
54
4.5 Android vs Iphone
Android Iphone
Operating Systems:
Android apps can be developed on Windows
,Linux ,and MacOS
iPhone apps only on a Mac

Programming Languages:
Android apps can be developed in Java,
C/C++, and increasingly in Python and Perl
Objective C is the only choice for serious
iPhone development for now
Source Code Availability:
Android is mostly open source iPhone is 100% proprietary
Third-party Software Distribution:
There is a small entry fee for publishing
Android apps on the Android market
there is an Apple-centered certification
process to make your apps available in the
AppStore
Different Philosophies:
Bottom-up openness Top-down quality control
DONG NAI UNIVERSITY OF TECHNOLOGY
55
4.5 Android vs Iphone
Hardware Choices: Dual-Core CPU, Camera, Audio
device, Bigger Screens
Pricing
Java for developers
Accessibility: Your Choice of Carriers. several
different devices manufacturers like HTC, Motorola and
Samsung. They are also available with a choice of
carriers and different plans to suit different users' needs
So, why Android?
DONG NAI UNIVERSITY OF TECHNOLOGY
56
4.6 Android components
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 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, 4G, and Wi-Fi (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
DONG NAI UNIVERSITY OF TECHNOLOGY
57
4.6 Android components
Android Software Stack
DONG NAI UNIVERSITY OF TECHNOLOGY
58
4.6 Android components
Linux kernel is a proven core platform.
Reliability is more important than performance when it
comes to a mobile phone, because voice communication
is the primary use of a phone.
Linux provides a hardware abstraction layer, letting the
upper levels remain unchanged despite changes in the
underlying hardware.
As new accessories appear on the market, drivers can
be written at the Linux level to provide support, just as on
other Linux platforms.
Why use Linux for a phone?
DONG NAI UNIVERSITY OF TECHNOLOGY
59
4.6 Android components
DONG NAI UNIVERSITY OF TECHNOLOGY
60
4.6 Android components
Linux Kernel
Works as a HAL
Device drivers
Memory management
Process management
Networking
DONG NAI UNIVERSITY OF TECHNOLOGY
61
4.6 Android components
Libraries
C/C++ libraries
Interface through Java
Surface manager Handling UI Windows
2D and 3D graphics
Media codecs, SQLite, Browser engine
DONG NAI UNIVERSITY OF TECHNOLOGY
62
4.6 Android components
Android Runtime
Dalvik VM
Dex files
Compact and efficient than class files
Limited memory and battery power
Core Libraries
Java 5 Std edition
Collections, I/O etc
DONG NAI UNIVERSITY OF TECHNOLOGY
63
4.6 Android components
Application Framework
API interface
Activity manager manages application life cycle.
DONG NAI UNIVERSITY OF TECHNOLOGY
64
4.6 Android components
Application
Built in and user apps
Can replace built in apps
DONG NAI UNIVERSITY OF TECHNOLOGY
65
4.6 Android components
Dalvik Virtual Machine
Dalvik is the name of a town in Iceland
Dalvik VM is written by Daniel Bornstein
Dalvik VM takes the generated .class files and
combines them in to one or more .dex files (Dalvik
executables)
It reuses the information from .class files to reduce the
size of the footprint
Dalvik VM uses the registers as the primary source of
data storage in stead of the stack (JVM)
DONG NAI UNIVERSITY OF TECHNOLOGY
66
4.6 Android components
Android applications are compiled to Dalvik bytecode
DONG NAI UNIVERSITY OF TECHNOLOGY
67
4.6 Android components
The Dalvik runtime is optimised for mobile applications
DONG NAI UNIVERSITY OF TECHNOLOGY
68
5. Using Android OS / Devices
5.1 Creating Android device without Buying a Phone
5.2 Using Android device
5.3 Using Android virtual device
5.4 Connect and debug on real device
DONG NAI UNIVERSITY OF TECHNOLOGY
69
5.1 Creating Android device without Buying a Phone
Creating Android Virtual Device
Setup account: contact, email
Using market from AVD
DONG NAI UNIVERSITY OF TECHNOLOGY
70
5.2 Using Android device
DONG NAI UNIVERSITY OF TECHNOLOGY
71
5.2 Using Android device
Android Desktops
Call, SMS, Contact, Calendar, Email
Gallery, play audio, video file
Change desktop, add widget, copy shortcut to desktop
Using button: home, back, menu
Copy/Paste text between apps
Setting
Using web browser
Application: Install /Uninstall/Move to SD card
Market
Apk file
Talk manager
Notification
SD card / unmount SD card
DONG NAI UNIVERSITY OF TECHNOLOGY
72
5.3 Using Android virtual device
Ver 1.5
DONG NAI UNIVERSITY OF TECHNOLOGY
73
5.3 Using Android virtual device
Ver 1.6
DONG NAI UNIVERSITY OF TECHNOLOGY
74
5.3 Using Android virtual device
Ver 4.0
DONG NAI UNIVERSITY OF TECHNOLOGY
75
5.3 Using Android virtual device
http://developer.android.com/tools/help/emulator.html
DONG NAI UNIVERSITY OF TECHNOLOGY
76
5.3 Using Android virtual device
Change Android emulator skin
Copy folder in to flatform
Emulator vs Simulator
VirtualPC or Vmware?
DONG NAI UNIVERSITY OF TECHNOLOGY
77
5.3 Using Android virtual device
Emulator looks for the disk
images in the private storage
area of the AVD in use
DONG NAI UNIVERSITY OF TECHNOLOGY
78
5.3 Using Android virtual device
The default storage location for AVDs is in
Users\<user>\.android\avd\
DONG NAI UNIVERSITY OF TECHNOLOGY
79
5.3 Using Android virtual device
android list targets (android-skd\tools)
android list avd
emulator -avd<avd_name>
adb devices
adb shell
Log in AVD shell (1)
adb s emulator-5554 shell
telnet localhost 5554
smssend <Senders phone number> <text message>
gsmcall <callers phone number>
You must setup Environment variables to use adb command line
DONG NAI UNIVERSITY OF TECHNOLOGY
80
5.3 Using Android virtual device
accepts a number of Linux shell commands in Android Emulator:
DONG NAI UNIVERSITY OF TECHNOLOGY
81
5.3 Using Android virtual device
DDMS Emulator Control
DONG NAI UNIVERSITY OF TECHNOLOGY
82
5.3 Using Android virtual device
DDMS-File Explorer
DONG NAI UNIVERSITY OF TECHNOLOGY
83
5.3 Using Android virtual device
3G icon on AVD
Path data/data (permission) on File Explorer in DDMS
Send sms from 2 AVDs
Copy image/media file to AVD
Capture screen
Change screen orientation for AVD
Ctrl + F11
Volume
Ctrl +F5, Ctrl + F6
Full Screen
Alt + Enter
DONG NAI UNIVERSITY OF TECHNOLOGY
84
5.4 Connect and debug on real device
Install from unknown source
USB mode
Setting debug mode
ADB driver
Debug in eclipse
DONG NAI UNIVERSITY OF TECHNOLOGY
85
6. Eclipse debug with Android app
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:debuggable="true">
DONG NAI UNIVERSITY OF TECHNOLOGY
86
7. Components of the Android Application
7.1 View
7.2 Activity
7.3 Intent / IntentFilters
7.8 Android Application
7.4 Content Provider
7.5 Services
7.6 Broadcast Receiver
7.7 AndroidManifest.xml
DONG NAI UNIVERSITY OF TECHNOLOGY
87
7.1 View
A View is a UI building block: a text field, a label, a
button,
Views are hierarchical objects that know how to draw
themselves
J2EE and Swing use the same term-view
In MS GUI lingo, views are controls
DONG NAI UNIVERSITY OF TECHNOLOGY
88
7.2 Activity
An Activity is a single screen in an application
An Activity typically contains at least one view
An application may have several activities
DONG NAI UNIVERSITY OF TECHNOLOGY
89
7.3 Intent / IntentFilters
A Message object that announces an intention
An Intent can broadcast a message
An Intent can start a service
An Intent can launch an activity
Intents are initiated by applications
Intent
DONG NAI UNIVERSITY OF TECHNOLOGY
90
7.3 Intent / IntentFilters
IntentFilters
An Intent is a declaration of need.
An Intent is made up of various pieces including:
desired action or service,
data, and
category of component that should handle the intent
and instructions on how to launch a target activity.
An IntentFilter is a trigger, a declaration of capability
and interest in offering assistance to those in need.
An IntentFilter may be generic or specific with respect
to which Intents it offers to service.
DONG NAI UNIVERSITY OF TECHNOLOGY
91
7.4 Content Provider
A Mechanism (essentially, an API) for applications to
share data without exposing the underlying storage
details
The underlying data can be stored in a file or a database
DONG NAI UNIVERSITY OF TECHNOLOGY
92
7.5 Services
A Background process
not interacting with the user
for an indefinite period of time.
like other application objects, run in the main thread of their
hosting process
Example: an email application that polls for new message
Local services are services accessible to the hosting
application
Remote services are services that can be used by other
applications on the device
DONG NAI UNIVERSITY OF TECHNOLOGY
93
7.6 Broadcast Receiver
If an application wants to receive and respond to a
global event, such as the phone ringing or an incoming
text message, it must register as a BroadcastReceiver.
An application may register at runtime
Or register at the AndroidManfest.xml file
it does not have to be running
When the global event occurs, the application is started
automatically upon notification of the triggering event. All
of this housekeeping is managed by the Android OS
itself.
do not have a UI
DONG NAI UNIVERSITY OF TECHNOLOGY
94
7.7 AndroidManifest.xml
An XML file that defines the contents and behavior of
the application
It names the Java package for the application. (unique
identifier for the application.)
It declares the minimum level of the Android API that
the application requires.
It contains the list of activities in the application
It describes the components of the application the
activities, services, broadcast receivers, and content
providers
DONG NAI UNIVERSITY OF TECHNOLOGY
95
7.7 AndroidManifest.xml
It declares IntentFilter.
It declares which permissions the application must
have in order to access protected parts of the API and
interact with other applications.
It also declares the permissions that others are
required to have in order to interact with the
application's components.
It lists the libraries that the application must be linked
against.
.
DONG NAI UNIVERSITY OF TECHNOLOGY
96
7.8 Android Application
Activities, services,
BroadcastReceivers,
ContentProviders,
Go Here
Auto gen
Android libs
Resource
Layout
Manifest
DONG NAI UNIVERSITY OF TECHNOLOGY
97
8. Units of measurement
8.1 Screen
8.2 Units of measurement
8.3 Example
http://developer.android.com/guide/practices/screens_support.html
Please visit the link below to get more information about Units of Measurement:
DONG NAI UNIVERSITY OF TECHNOLOGY
98
8.1 Screen
Screen size: Actual physical size, measured as the
screen's diagonal: small, normal, large, and extra large
Screen density: The quantity of pixels within a physical
area of the screen - dpi (dots per inch): low, medium,
high, and extra high.
Orientation: landscape or portrait
Resolution: The total number of physical pixels on a
screen.
app do not work directly with resolution; concerned only
with screen size and density
DONG NAI UNIVERSITY OF TECHNOLOGY
99
8.1 Screen
Density-independent pixel (dp) : A virtual pixel unit that
you should use when defining UI layout, to express
layout dimensions or position in a density-independent
way.
DONG NAI UNIVERSITY OF TECHNOLOGY
100
8.2 Units of measurement
In
Inches - based on the physical size of the screen
Mm
Millimeters - based on the physical size of the screen.
Pt
Points - 1/72 of an inch based on the physical size of the screen.
Px
Pixels - corresponds to actual pixels on the screen
Dip /Dp
Density-independent Pixels - an abstract unit that is based on the physical
density of the screen. These units are relative to a 160 dpi screen, so one dp
is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the
screen density, but not necessarily in direct proportion.
Sp/sip
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the
user's font size preference. It is recommend you use this unit when specifying
font sizes, so they will be adjusted for both the screen density and user's
preference.
DONG NAI UNIVERSITY OF TECHNOLOGY
101
8.2 Units of measurement
try to never use anything but sp or dp unless you
absolutely have to. Using sp/dp will make your Android
applications compatible with multiple screen densities
and resolutions
you want to use sp for font sizes and dip for everything
else.
DONG NAI UNIVERSITY OF TECHNOLOGY
102
8.3 Example
Example of two screens that are the same size, diff resolution:
DONG NAI UNIVERSITY OF TECHNOLOGY
103
8.3 Example
app without support for different densities vs density
independent
DONG NAI UNIVERSITY OF TECHNOLOGY
104
http://developer.android.com/about/dashboards/index.html
Screen Sizes and Densities distribution:
Data collected during a 7-day period ending on September 4, 2013
DONG NAI UNIVERSITY OF TECHNOLOGY
105
http://developer.android.com/about/dashboards/index.html
Platform version distribution:
DONG NAI UNIVERSITY OF TECHNOLOGY
106
9. XML Primer
XML in Android: Layout, Menu, Value, Manifest
http://www.w3schools.com/
Please visit the link below to learn more XML:
http://developer.android.com/training/basics/network-ops/xml.html
Please visit the link below to parse xml data:
DONG NAI UNIVERSITY OF TECHNOLOGY
107
10. Applications Life Cycle
10.1 Applications
10.2 Activities
10.3 Activity Stack
10.4 Tasks
10.5 Life Cycle States
DONG NAI UNIVERSITY OF TECHNOLOGY
108
10.1 Applications
typically consists of one or more related, loosely bound
activities for the user to interact with, typically bundled
up in a single file (with an .apk suffix).
Android ships with a rich set of applications:email,
calendar, browser, maps, text messaging, contacts,
camera, dialer, music player.
Android has an application launcher available at the
Home screen
DONG NAI UNIVERSITY OF TECHNOLOGY
109
10.2 Activities
main building blocks of Android applications
Each activity has a lifecycle that is independent of
the other activities in its application or task
each activity is launched (started) independently,
user or system can start, run, pause, resume, stop
and restart it as needed.
can be re-used and replaced by other activities in a
variety of ways.
DONG NAI UNIVERSITY OF TECHNOLOGY
110
10.3 Activity Stack
Activities in the system are managed as an activity
stack.
keeps a linear navigation history of activities the user
has visited
when a user starts a new activity, it is added to the
activity stack, so that pressing Back displays the
previous activity on the stack. However, the user
cannot use the Back button to go back further than the
last visit to Home.
Activities are the only things that can be added to the
activity stack views, windows, menus, and dialogs
cannot.
DONG NAI UNIVERSITY OF TECHNOLOGY
111
10.3 Activity Stack
DONG NAI UNIVERSITY OF TECHNOLOGY
112
10.4 Tasks
sequence of activities the user follows to accomplish
an objective, regardless of which applications the
activities belong to
The activity that starts a task is called the root activity
spanning multiple processes
Interrupting the Task
by a notification
Home button
How Multitasking with Task?
DONG NAI UNIVERSITY OF TECHNOLOGY
113
Activities vs Task
DONG NAI UNIVERSITY OF TECHNOLOGY
114
10.5 Life Cycle States
An activity has essentially
three states:
1. It is active or running
2. It is paused or
3. It is stopped .
DONG NAI UNIVERSITY OF TECHNOLOGY
115
10.5 Life Cycle States
Running
It is active or running when it is in the foreground of the
screen
(at the top of the activity stack for the current task).
This is the activity that is the focus for the user's actions.
DONG NAI UNIVERSITY OF TECHNOLOGY
116
10.5 Life Cycle States
Paused
It is paused if it has lost focus but is still visible to the user.
That is, another activity lies on top of it and that new
activity either is transparent or doesn't cover the full screen.
A paused activity is completely alive (it maintains all state
and member information and remains attached to the
window manager), but can be killed by the system in
extreme low memory situations.
DONG NAI UNIVERSITY OF TECHNOLOGY
117
10.5 Life Cycle States
Stopped
It is stopped if it is completely obscured by another activity.
It still retains all state and member information. However, it
is no longer visible to the user so its window is hidden and it
will often be killed by the system when memory is needed
elsewhere.
DONG NAI UNIVERSITY OF TECHNOLOGY
118
10.5 Life Cycle States
Applications Life Cycle
DONG NAI UNIVERSITY OF TECHNOLOGY
119
10.5 Life Cycle States
Demo
Applications Life Cycle
DONG NAI UNIVERSITY OF TECHNOLOGY
120
10.5 Life Cycle States
Visible Lifetime
Foreground
Lifetime
DONG NAI UNIVERSITY OF TECHNOLOGY
121
10.5 Life Cycle States
Visible Lifetime
The visible lifetime of an activity happens between a
call to onStart() until a corresponding call to onStop().
During this time, the user can see the activity onscreen,
though it may not be in the foreground and interacting
with the user.
The onStart() and onStop() methods can be called
multiple times, as the activity alternates between being
visible and hidden to the user.
Between these two methods, you can maintain
resources that are needed to show the activity to the
user.
DONG NAI UNIVERSITY OF TECHNOLOGY
122
10.5 Life Cycle States
Foreground Lifetime
The foreground lifetime of an activity happens
between a call to onResume() until a corresponding call
to onPause().
During this time, the activity is in front of all other
activities on screen and is interacting with the user.
An activity can frequently transition between the
resumed and paused states for example,
onPause() is called when the device goes to sleep or when
a new activity is started,
onResume() is called when an activity result or a new intent
is delivered.
DONG NAI UNIVERSITY OF TECHNOLOGY
123
10.5 Life Cycle States
Method: onCreate()
Called when the activity is first created.
This is where you should do all of your normal static
set up create views, bind data to lists, and so on.
This method is passed a Bundle object containing
the activity's previous state, if that state was
captured.
Always followed by onStart()
DONG NAI UNIVERSITY OF TECHNOLOGY
124
10.5 Life Cycle States
Method: onRestart()
Called after the activity has been stopped, just prior to it
being started again.
Always followed by onStart()
Method: onStart()
Called just before the activity becomes visible to the user.
Followed by onResume() if the activity comes to the
foreground, or onStop() if it becomes hidden.
DONG NAI UNIVERSITY OF TECHNOLOGY
125
10.5 Life Cycle States
Method: onResume()
1. Called just before the activity starts interacting with the
user.
2. At this point the activity is at the top of the activity
stack, with user input going to it.
3. Always followed by onPause().
DONG NAI UNIVERSITY OF TECHNOLOGY
126
10.5 Life Cycle States
Method: onPause()
1. Called when the system is about to start resuming
another activity.
2. This method is typically used to commit unsaved changes
to persistent data, stop animations and other things that may
be consuming CPU, and so on.
3. It should do whatever it does very quickly, because the
next activity will not be resumed until it returns.
4. Followed either by onResume() if the activity returns back
to the front, or by onStop() if it becomes invisible to the user.
5. The activity in this state is killable by the system.
DONG NAI UNIVERSITY OF TECHNOLOGY
127
10.5 Life Cycle States
Method: onStop()
1. Called when the activity is no longer visible to the user.
2. This may happen because it is being destroyed, or
because another activity (either an existing one or a new
one) has been
resumed and is covering it.
3. Followed either by onRestart() if the activity is coming
back to interact with the user, or by onDestroy() if this
activity is going away.
4. The activity in this state is killable by the system.
DONG NAI UNIVERSITY OF TECHNOLOGY
128
10.5 Life Cycle States
Method: onDestroy()
1. Called before the activity is destroyed.
2. This is the final call that the activity will receive.
3. It could be called either because the activity is finishing
(someone called finish() on it), or because the system is
temporarily destroying this instance of the activity to save
space.
4. The activity in this state is killable by the system.
DONG NAI UNIVERSITY OF TECHNOLOGY
129
10.5 Life Cycle States
Killable States
Activities on killable states can be terminated by the
system at any time after the method returns, without
executing another line of the activity's code.
Three methods (onPause(), onStop(), and onDestroy()) are
killable.
onPause() is the only one that is guaranteed to be called
before the process is killed onStop() and onDestroy()
may not be.
Therefore, you should use onPause() to write any
persistent data (such as user edits) to storage.
DONG NAI UNIVERSITY OF TECHNOLOGY
130
10.5 Life Cycle States
We should write and read data for applications sate in methods:
onPause() and (onCreate() or onResume()) by SharedPreferences
DONG NAI UNIVERSITY OF TECHNOLOGY
131
10.5 Life Cycle States
Android Preferences
Preferences is a lightweight mechanism to store and retrieve
keyvalue pairs of primitive data types. It is typically used to store
application preferences, such as a default greeting or a text font to
be loaded whenever the application is started.
Call Context.getSharedPreferences() to read and write values.
Assign a name to your set of preferences if you want to share
them with other components in the same application, or use
Activity.getPreferences() with no name to keep them private to
the calling activity.
getSharedPreferences() - can be used while using multiple
preference files and getPreferences() - can be used for only one
preference file(default) for that specific activity.
File xml
You cannot share preferences across applications (except by
using a content provider).
DONG NAI UNIVERSITY OF TECHNOLOGY
132
DONG NAI UNIVERSITY OF TECHNOLOGY
END
133

Вам также может понравиться