Android Programming
Faculty of IT - UEF
1
1
Table of contents
Introduction to Android OS
Features
Architecture
Version
Preparing for Android developing
Development Environment
AVD
Android app development process.
The simple Android app
Structure of Android project
How to code the Android app.
2
Google Android?
Android OS is a Linux-based platform for
mobile phones (touchscreen mobile devices)
Smartphone
Tablet PCs
Wear, TV, Auto, Glass…
The Android OS is an open source operating
system!
3
Google Android?
A set of program for mobile device such as
OS
Middle ware
Build-in software
Based on Linux core for
Security
Memory management
Process management
Power management
Hardware drivers
4
Android architecture
5 http://developer.android.com/guide/basics/what-is-android.html
Linux kernel
At the heart of the whole system
Provide:
Hardware abstraction
Memory Management Programs
Security Settings
Power Management Software
6
Native Libraries & Android Runtime
Android’s native and core libraries, also includes the Dalvik Virtual
Machine
Guide the devices in handling different types of data:
Playback and recording of various audio, video formats
DVM same as JVM but it is optimized for mobile device
7
Application framework layer
Our application directly interact with
Manage basic function:
Resource management
Voice call management
…
8
Application layer
An average user of Android device would mostly interact with
Several standard application:
SMS client app
Dialer
Web browser
…
9
Android platform components
10
Android versioning
Platform Codename API Level
Android 1.5 Cupcake 3
Android 1.6 Donut 4
Android 2.0 5
Android 2.0.1 Eclair 6
Android 2.1 7
Android 2.2 Froyo 8
Android 2.3 , Android 2.3.2 9
Gingerbread
Android 2.3.3, Android 2.3.7 10
Android 3.0, 3.1,3.2 Honeycomb 11, 12, 13
Android 4.0.x Ice Cream Sandwich (Oct - 2011) 14,15
Android 4.1.x Jelly Bean (July – 2012) 16
Android 4.2.x Jelly Bean (11/2012) 17
Android 4.3 Jelly Bean (7/2013) 18
11
Android vesioning
Platform Codename API Level
Android 4.4 KitKat (10/2013) 19
Android 5.x Lollipop (7/2014) 21-22
Android 6.x Marshmallow (10/2015) 23
Android 7.x Nougat (8/2016) 24-25
Android 8.x Oreo (8/2017) 26-27
Android 9 Pie (8/2018) 28
Android 10 Android 10 (9/2019) 29
Android 11 Android 11 (9/2020) 30
Android 12 Android 12 (10/2021) 31
Android 12L Android 12.1 (3/2022) 32
Android 13 Android 13 (8/2022) 33
12
Android versioning
13
Version distribution
Source: April, 2017
14
https://en.wikipedia.org/wiki/Android_version_history
Android ecosystem
15
Android SDK
Preparing for PC
JDK
Android SDK
IDE: Android Studio Chipmunk, Eclipse (not support).
16
Android Emulator & AVD
Emulator is the important software for testing Android app on
PC, suggest to use real device for the best testing.
Emulator for Android is called Android Virtual Devices
(AVDs)
Android SDK and AVD manager allow to make any version of
Android API.
AVD can custom:
Resolution
RAM
SD card
Skin
Other hardwares
17
Android Emulator: 7.1 Device
Device 7.1 - Nougat
18
Emulator basic
Using PC keyboard
Using PC’ mouse pointer as "finger"
Using PC internet
Some button: Home, Menu, Back, Search…
Ctrl + F11: landscape Þ portrait
Alt + Enter: full screen mode
Take the time for familiar with Android
Emulator!!!
19
Emulation limitations
Can not call or receive the phone in reality
Just on emulator software
Not support
USB connection
Camera/video (input)
Headphone
Battery charge level & AC charging state
Bluetooth
Test your app
on an actual
device!
20
Mobile Devices: Advantages
Always with the user
Typically have Internet access
Typically GPS enabled
Typically have accelerometer & compass
Many have cameras & microphones
Many apps are free or low-cost
21
Mobile Devices: Disadv
Limited screen size
Limited battery life
Limited processor speed
Limited and sometimes slow network access
Limited or awkward input: soft keyboard, phone keypad, touch
screen, or stylus
Limited web browser functionality
Range of platforms & configurations across devices
22
Android application development process
1. Use Android Studio, choose an appropriate template.
2. UI elements - using the layout editor or write code directly in
XML.
3. Use Java language, create source code for all app’s
components
4. Build on real or virtual devices
5. Test UI and logic and debug
6. Assembling the final APK, distribute it through channel such
as Google Play
23
Producing an Android App (Step 3, 4)
Java javac Byte
code code
dx Dalvik
.java .class exe
classes.dex aapt
Byte
code <xml>
Other .class files AndroidManifest.xml .apk
<str>
Resources
24
Android Apps
Java code compile to Dalvik byte code (.dex)
Optimize for mobile device (memory, battery…)
Dalvik VM run the .dex file
Java code
compile to
Dalvik byte
code!
25
Android application lifecycle
Android has no control over their own life cycle
Android aggressively manages its resource by killing without
warning if necessary to free resource for higher-priority
application.
26
Manage the application lifecycle
The out-of-memory
Process Description killer Priorit
status y
Active(Fore Active apps which consume data and are currently 1
ground) running on the mobile.
1.onCreate()
2.onStart() (++activityReferences == 1) (App enters
Foreground)
3.OnResume()
Visible User is not interacting with activity (inactive) but it is still 2
visible or the application has a service which is used by an
inactive but visible activity.
Service Application with service that does not qualify for 1 or 2 3
Background The app is doing some activity in the background, which 4
is not active right now.
onPause() / onResume() is called
Empty Application without any active component 5
27
Example for
Android programming
28
Hello world
Java
public class SomeClass extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main); }
public void handlerMethod(View clickedButton) {
String someName = getResources().getString(R.string.name);
doSomethingWith(someName);
} }
XML
res/values/strings.xml res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <LinearLayout …>
<string name="name">…</string> <TextView …/>
… <Button …android:onClick="handlerMethod" />
</resource> …
</LinearLayout>
29
Main Idea
Approach
Use XML files to define Strings, lay out window, create GUI
controls, and assign event handlers
Define layout and controls in res/layout/activity_main.xml
Define Strings in res/values/strings.xml
Advantages
Easier to maintain
Can use visual layout editor in eclipse
Recommended approach
Disadvantages
Works poorly for dynamic layouts.
30
More details
res/layout/activity_main.xml
Define layout and widget with XML description
<LinearLayout …> define widget </LinearLayout>
Refer to strings (strings.xml) with @string/string_name
Assign event handler with android:onClick
res/value/strings.xml
Define strings used in GUI res/layout/activity_main.xml
Java code
Refer to layout with R.layout.activity_main
Refer to strings with
getString(R.string.string_name)
Refer to widget with findViewById(R.id.xyz_id)
31
Project layout
Refer to layout defined in res/layout/main.xml
with R.layout.activity_main
Defines screen layout and GUI control.
Optionally assigns event handler to control.
Refer to string defined in
res/values/strings.xml with
@string/string_name
Defines string that are either used in GUI
controls or that might change with
internationalization
32
Code (res/layout/activity_main.xml)
These attributes (orientation,
layout_width…) defined in JavaDoc
API for LinearLayout
These strings are defined in
res/values/strings.xml
33
Code (res/values/strings.xml)
Title of screen
activity_main.xml: @string/string_name
Java code: getString(R.string.string_name)
34
Java code
35
Exercise
Write an application for basic calculation {addition, subtraction,
multiplication, division}
36