source code repository: https://github.com/aporter/coursera-android
Android Platform and Development Environment
Introduction to the Android Platform
Documentation: http://developer.android.com/training
Android Platform: A software stack consisting of several layers, designed primarily to support mobile devices.
linux kernel: provides the core services that any Android computing device will rely on.
It provides generic OS services like any Linux kernel. e.g. security, memory and process management, (low-level details) file and network I/O, device drivers.
It includes several Android-specific components. E.g.
- power management services
- android shared memory, low memory killer -- its own memory sharing and memory management features
- binder – its own interprocess communication
…etc.
libraries: includes a variety of system libraries typically written in C and C++ (thus are often referred to as the native labraries) that handle the core performance-sensitive activities on the device (e.g. quickly rendering web pages and updating display).
e.g.
- System C Library (bionic libc) – implements the standard OS system call, e.g. process and thread creation, mathematical computation, memory allocation, etc.
- Surface Manager – manages the display updating.
- Media Framework
- Webkit
- OpenGL
- SQLite
Android Runtime: supports writing and running Android applications.
consists of 1) the core Java libraries and 2) the Dalvik Virtual Machine.
e.g. libraries that core Java libraries include
- Java.*, Javax.* -- basic java classes.
- Android.* -- android packages specific to the life cycles of mobile applications.
- Org.* -- supports internet or web operations.
- Junit.* -- supports the unit testing.
Dalvik virtual machine: The software that actually executes Android applications. Unlike the Java Virtual Machine, it was specifically designed to run in the resource-constrained environment typical of mobile devices.
Android apps does not run on a standard Java virtual machine but on Dalvik virtual machine instead.
-> Apps are written in Java
-> Java compiler compiles the Java source code into multiple Java bytecode files
-> DX (a tool) transforms Java bytecodes into a single DEX file (a different bytecode format) usually called classes.dex.
-> the DEX file is packaged with other application resources and installed on the device.
-> when the user launches the app, Dalvik virtual machine will execute the classes.dex file.
More about Dalvik: http://www.youtube.com/watch?v=ptjedOZEXPM
application framework: contains reusable software that many applications are likely to need.
e.g.
- Package Manager -- keeps track of all app packages installed
- Window Manager -- manages windows that comprise applications.
- View System -- provides common graphical UI elements. e.g. buttons, icons.
- Resource Manager -- manages non-compiled resources. e.g. strings, graphics, UI layout files.
- Activity Manager -- helps to coordinate and support navigation among activities.
- Content Provider -- allows applications to store and share structured information e.g. Dialer/MMS/Email apps can access contact information beceause it is stored in a Content Provider.
- Location Manager -- allows apps to receive location and movement information (e.g. generated by GPS)
- Notification Manager – allows apps to place information in Notification bar.
Application: some built-in apps. E.g. home screen, contacts, phone dialer, web browser, email reader.
The Android development environment
Android SDK bundle includes 1) Android Platform, 2) Android Studio IDE, 3) key development tools and 4) system image for the emulator.
Download & install Android SDK bundle:
http://developer.android.com/sdk
http://www.youtu.be/jpFll5aw5rA
Android Virtual Device (AVD): an Android emulated phone instances.
Pros & cons of AVD
Pros:
- doesn’t require an actual device;
- hardware is reconfigurable;
- changes are non-destructive;
Cons:
- slow;
- some features are unavailable (e.g. Bluetooth, USB connections);
- performance/user experience can be misleading;
Advanced features of AVD
- can emulate many different devices/user characteristics (thus help to test code responding to environment events)
e.g. emulate different network speed/latency, emulate different battery states, inject mock location coordinates to test location aware apps easily.
configure charateristics via Android IDE’s terminal window,
telnet+localhost+[port number the emulator is listening] %connect to the emulator network spped [new network speed] power capacity [new battery status] power status [new charging status] geo fix [new location]
- can emulate networked interactions
e.g. emulate incoming phone calls or SMS messages
emulate SMS via Android IDE’s terminal window
telnet+localhost+[port number the emulator is listening] %connect to the emulator sms+send+[incoming sms sender’s number]+[text message string]
two emulators interact with each other
the phone number of an emulator is its port number. Use one emulator to dial another.
Debuger
-> Set breakpoints
-> Run->debug
DDMS -- deprecated/removed now
Dalvik Debug Monitor Service (DDMS) provides a number of monitoring tools to monitor system behavior. E.g. file explorer, logcat, traceview, hierarchy viewer.
File explorer: for viewing the device’s file system.
Logcat: for logging and displaying runtime events.
Traceview: graphically displays method traces taken from running application. –subtituted by CPU profiler now.
Hierarchy viewer: allows to examine app’s UI and see how it’s organized.
Application Fundamentals and the Activity Class
Application Fundamentals
Four fundamental building blocks:
1) Activity: provides GUI, to users, primary class for user interaction.
- Usually implements a single, focused task that the user can do. e.g. viewing an email message, showing a login screen.
2) Service: performs background/long-running operations typically away from the main UI thread.
- supports interaction (request operations, share data) with remote processes.
3) Broadcast Receiver: listens for and responds to events (represented by Intent class).
4) Content Provider: allows apps to store and share data.
- uses database-style interface
- handles the low level details of interprocess communication.
each component has its own purpose and APIs.
The process of building an Android app
1. Define resources
- prepare resources* that make up the app.
2. Implement application classes
- implement java classes, usually involves writing at least one activity.
3. Package application
- build tools create application package (.APK file).
- in AndroidManifest.xml file, developers should provide build tools with required information.
- the APK is digitally signed (to identify its developer).
4. Install & run application
- can install the apk on a device/emulator and run.
- can also issue command on ADB tool: adb install <path to apk>
**
Resources
Resources: non-source code entities. E.g. layout files, strings, images, menus, animations.
Pros of Managing resources separately from the app: resources can be altered easily without having to change /recompile the app source code.
String resources
In Android, there are 3 types of string resources – individual strings, arrays of strings, plurals.
Plurals: string arrays that can be used to choose specific strings associated with certain quantities (e.g. one book, two books).
Typically stored in /res/values/*.xml. format e.g. <string name=“hello”>Hello World!</string>.
The actual string can include formatting/styling information, e.g. HTML tags.
Defined strings are accessed by other resources as @string/string_name, by Java code as R.string.string_name.
Android can choose different strings at runtime based on the device’s default language.
e.g. set different values for plural with the same name in string file under res/values and res/values-it, thus provide different @string/loction_string for English/Italian language.
layout files
layout: specify what the UI for some part of the app will look like.
typically stored in res/layout/*.xml.
Defined layouts are accessed by other resources as @layout/layout_name, by Java code as R.layout.layout_name.
Android can choose different layouts at runtime based on the device’s orientation/screen size.
e.g. set different layout file under res/layout and res/layout-land, thus provide different layout for portrait/landscape mode.
R.java
Resources can be accessed in Java as R.something.
R.java: At compilation time, Android uses resources to generate an R.java class, Java code can use this R class to access resources. (thus Resources are referred as R.something in source code.)
e.g. field R.layout.main gives a reference/handle to main.xml file, R.id.mapButton gives a reference to the button defined in the layout file, etc.
**
**
onCreate() method
onCreate(): the entry point for activities, usually include activity initialization code.
Typical onCreate() workflow:
1) restore saved state
2) set content view
3) initialize UI elements
4) link UI elements to specific code actions
e.g.
**
AndroidManifest.xml
AndroidManifest.xml contains packaging information, includes:
- application name;
- list of components;
- others: e.g. required permissions, hardware features requirement, minimum API level.
**