SlideShare uma empresa Scribd logo
1 de 161
1
Lập trình Android
Nguyen Ha Giang
FIT – Hutech
nguyenha.giang@yahoo.com
1
2
Tổng quan hệ điều hành Android
Đặc điểm
Kiến trúc
Phiên bản
Lập trình trên môi trường Android
Môi trường lập trình
AVD
Quy trình phát triển ứng dụng Android.
Ứng dụng cơ bản Android
Tìm hiểu Android project
Các cách tiếp cận trong lập trình Android.
Nội dung
3
Android là hệ điều được thiết kế cho họ hệ thiết bị di động
màn hình cảm ứng (touchscreen mobile devives) bao gồm
Smartphone
Tablet PCs
Phablet
Hệ điều hành mã nguồn mở
Google Android ?
4
Tập phần mềm dành cho thiết bị di động bao gồm
Một hệ điều hành
Midleware
Các ứng dụng cơ sở
Sử dụng Linux để cung cấp dịch vụ hệ thống lõi
Security
Memory management
Process management
Power management
Hardware drivers
Google Android ?
5
Application framework enabling reuse and replacement of
components.
Optimized Java virtual machine (Dalvik virtual machine)
Optimized Graphics Processing, supporting 2D, 3D graphics
(OpenGL ES 1.0)
Integrated open source web browser: Webkit
SQLite for structured data storage
Android Features #1
6
Multimedia capability, supporting varieties of audio, video and
still image format.
GSM Telephony
Bluetooth, EDGE, 3G, and WiFi support
Camera, GPS, compass, accelerometer and other sensors support.
Rich development environment, including an enmulator,
debugging tools, memory probe tools, log tools and powerful
eclipse plugins.
Android Features #2
7
Android architechture
http://developer.android.com/guide/basics/what-is-android.html
8
Note that Android based on a Linux kernel not a Linux OS.
Supplies Security, memory management, process management,
Network stack, and driver model.
Acts as an abstraction layer between the hardware and the rest of
the software stack.
Linux Kernel
9
Run in system background
Using C/C++ Language
Four types of libraries
Bionic Libc, system C libraries
Function Libraries, supporting multimedia, web browser,
SQLite…
Native servers
Hardware abstraction libraries
Libraries
10
System C library, the standard C system library, tuned for
embedded Linux-based devices.
Media Libraries, support playback and recording of many
popular audio/video formats, as well as image files, including
MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG
Surface Manager, manages access to the display subsystem and
seamlessly composites 2D and 3D graphic layers from multiple
apps.
WebKit, a morden web browser engine which powers both the
Android browser and an embedded web view.
SGL, the underlying 2D graphics engine.
3D Libraries, an implementation based on OpenGL ES 1.0 APIs
FreeType, bitmap and vector font rendering
SQLite, a powerful and lightweight relational database engine.
Core Libraries
11
The core of Android platform
Dalvik Virtual Machine
Register-based
Exectutes files in the Dalvik format
Java core Libraries
Provides most of the functionality of the Java
programming language.
Android Runtime
12
The functions of Java core libraries reply on the Dalvik VM and
the underlying Linux kernel.
Multiple Dalvik VMs may run at the same time
Every Android application runs in its own process, with its own
instance of the Dalvik virtual machine.
The “dx” tool in Android SDK can transform compiled JAVA
class into the .dex format.
Android Runtime (cont.)
13
Android custom implementation virtual machine
Provides app portability and runtime consistency
Runs optimized file format (.dex) and Dalvik bytecode
Java .class/ jar files converted to .dex at build time
Designed for embedded environment
Supports multiple virtual machine processes per device
Highly CPU-optimized bytecode interpreter
Efficiently using runtime memory
Core libraries
Core APIs for Java language provide a powerful, yet simple
and familiar development platform.
Dalvik Virtual Machine
14
DVM
Google
Dalvik executable
Only supports a subset of standard Java library
JVM
Sun
Java bytecode
Some worries that Java world may be divided into different
communities, each has its own Java standard.
DVM vs. JVM
15
Vesion code và version name không giống nhau.
Mỗi version name (platform level) thì có duy nhất một version
code (API level) đi kèm.
Android vesioning
Platform Codename API Level
Android 1.5 Cupcake 3
Android 1.6 Donut 4
Android 2.0
Eclair
5
Android 2.0.1 6
Android 2.1 7
Android 2.2 Froyo 8
Android 2.3 , Android 2.3.2
Gingerbread
9
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, 4.2.x, 4.3 Jelly Bean (July – 2012, July-2013) 16, 17, ???
Android 4.4 KitKat (Sep -2013) ???
16
Nguồn: Android developer, Sept 4, 2013
Version distribution
http://developer.android.com/resources/dashboard/platform-versions.html
2.4%
30.7%
0.1%
21.7%
36.6% (4.1.x)
8.5% (4.2.x)
17
Chuẩn bị môi trường trên PC
Cài đặt JDK
http://www.oracle.com/technetwork/java/javase/downloads/index.h
tml
Nếu dùng Eclipse để phát triển ứng dụng thì download một
phiên bản Eclipse
http://www.eclipse.org/downloads/
Download Android SDK Start package
http://developer.android.com/sdk/index.html
Cài đặt Android SDK và nhớ note lại thư mục để tham chiếu
trong bước cài đặt Eclipse.
Installling Android SDK
18
Cài đặt ADT plugin cho Eclipse
Plugin này gọi là Android Development Tool, hỗ trợ công cụ
để phát triển ứng dụng Android
Mở rộng Eclipse với chức năng
Tạo mới Android project
Tạo ứng dụng UI Android
Test ứng dụng trên emulator Android SDK
Debug ứng dụng dùng Android SDK Tool.
Nếu dùng IDE khác thì không cần cài Eclipse + ADT plugin!
Installling Android SDK
19
Cài đặt ADT plugin cho Eclipse: Install New Software
Installling Android SDK
20
Bước Install
Installling Android SDK
21
Add Site, có 2 lựa chọn
Nếu download file zip chứa ADT rồi thì chọn Archive browse
đến file zip (vd: ADT-20.0.3.zip)
Nếu chưa download và muốn cài trực tiếp thì nhập URL sau
vào location
https://dl-ssl.google.com/android/eclipse/
Cuối cùng chọn OK, để thực hiện install ADT
Sau khi cài xong ADT thì restart lại Eclipse!
Installling Android SDK
22
Configuring the ADT Plugin
Bước này cài đặt ADT tham chiếu đến Android SDK mà ở bước
trước đã download và install vào thư mục trên đĩa.
Chọn Window > Preferences trong Eclipse, trong SDK location,
browse đến thư mục Android SDK
Installling Android SDK
Sau bước này
hoàn tất việc
tạo môi
trường để xây
dựng Android
app
23
ADT bundle bô i t nhanh, a t ca môi ng n t
đê t n UD Android. Bao m
Eclipse + ADT Plugin
Android SDK Tools
Android Platform-công cụ
Nền tảng Android mới nhất
Hệ thống hình ảnh giả lập a n ng Android i t
Installing Android dev by bundle
24
Installing Android dev by bundle
http://developer.android.com/sdk/index.html
25
Emulator là thành phần quan trọng để test ứng dụng nhưng
không thể thay thế hoàn toàn thiết bị thật!
Emulator trên Android được gọi là Android Virtual Devices
(AVDs)
Android SDK và AVD manager cho phép tạo các AVDs
hướng tới bất kỳ phiên bản Android API.
AVD cho phép cấu hình:
Resolution
RAM
SD card
Skin
Hardware khác
Android Emulator & AVD
26
Android Emulator: 1.6 Device
Device 1.6
27
Android Emulator: 2.2 Device
Device 2.2
28
Android Emulator: 2.3.3 Device
Device 2.3.3
29
Android Emulator: 3.0 Device
Device 3.0
30
Android Emulator: 4.1 Device
Device 4.1
31
Sử dụng bàn phím của máy tính
Sử dụng pointer máy tính như "finger"
Sử dụng kết nối internet của máy tính
Các button: Home, Menu, Back, Search…
Ctrl + F11: landscape portrait
Alt + Enter: full screen mode
Emulator basic
Hãy bỏ ra ít thời gian để làm quen với
Android Emulator!!!
32
Không hỗ trợ việc gọi và nhận thật sự cuộc gọi
Giả lập cuộc gọi và tin nhắn qua emulator console
Không hỗ trợ
USB connection
Camera/video (input)
Headphone
Battery charge level & AC charging state
Bluetooth
Emulation limitations
Test your app
on an actual
device!
33
Mở chức năng Android SDK và AVD manager trong Eclipse
Setting up an Emulator
SDK/AVD Manager
34
Mở chức năng Android SDK và AVD manager trong Eclipse
Setting up an Emulator
35
Chọn New virtual device
Setting up an Emulator
Đặt tên
Chọn platform
Chọn SD card
nếu cần
Chọn skin
HVGA
36
Với project Android đang làm việc, chọn Run (Ctrl +F11), lúc
này emulator đã thiết lập sẽ khởi động.
Lần đầu tiên thời gian khởi động emulator khá lâu (vài phút )
do phải tạo môi trường virtual Linux system.
Running the app in the emulator
Port number
AVD name
37
Để thuận tiện cho việc coding, build & run thì emulator khởi
động một lần trong suốt quá trình làm việc.
Ta cứ việc code, build và run, khi đó emulator tự động cập nhật
mã lệnh mới, không cần phải restart lại emulator!
Running the app: emulator
Run Emulator
1 lần cho suốt
phiên làm
việc!
38
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
Mobile Devices: Advantages
39
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
40
Được xây dựng trên Java và SDK mới
Không support một số thư viện Java như Swing & AWT
Oracle đang kiện Google vi phạm bản quyền!
Java code được biên dịch vào Dalvik byte code (.dex)
Được tối ưu cho thiết bị di động (memory, battery…)
Dalvik VM chạy những file .dex
Android Apps
Mã Java được
biên dịch
sang Dalvik
byte code!
41
Producing an Android App
Dalvik
exe
Byte
code <xml>
<str>
.java .class
Other .class files
javac
dx
classes.dex
AndroidManifest.xml
Resources
.apk
aapt
Java
code
Byte
code
42
Trong Eclipse chọn Alt +Shift + N.
First Android Application
43
New Android Project
Chọn tên project, build target (platform) và package name
First Android Application
Package name: ký tự
đầu tiên phải chữ
thường, phải có dấu
chấm (2 định danh)
Tên project
platform
Scroll down để thấy
phần package name
44
Toàn bộ Adroid project được khởi tạo từ Eclipse
First Android Application
Chọn Package
Explorer để xem cấu
trúc của ứng dụng
Code Java
Chứa resource
File cấu hình app
45
First Android Application
46
src/NguyenhagiangActivity.java
Activity của ứng dụng, hiển thị khi ứng dụng chạy
res/layout/main.xml
Định nghĩa layout & widgets cho activity
res/values/strings.xml
Khai báo hằng chuỗi dùng trong chương trình
gen/R.java (Don’t touch!)
File được tạo tự động các ID từ các file *.xml
AndroidManifest.xml
Khai báo tất cả thành phần của app (tất cả các activity).
Tên thư viện cần thiết được dùng trong ứng dụng
Xác định những permission mà ứng dụng được cấp.
Phiên bản, cấu hình…
Important Files
47
Activity là thành phần sẽ hiển thị khi chạy ứng dụng.
Do ứng dụng này đơn giản chỉ có 1 activity nên là activity chính
của ứng dụng!
src/NguyenhagiangActivity.java
48
Khai báo layouts & widgets cho activity
res/layout/main.xml
49
Khai báo hằng chuỗi dùng trong app
Hỗ trợ Localization
res/values-es/values/strings.xml (Spanish)
res/values-fr/values/strings.xml (French)
…
res/values/string.xml
Nội dung chuỗiTên truy cập chuỗi
50
File được tự động khởi tạo với các định danh từ main.xml,
string.xml và các phần khác.
gen/R.java
Không
được chỉnh
sửa!
51
Khai báo tất cả các thành phần của app với system
Phiên bản: version code, version name
Application: icon, name, label…
Tham chiếu đến tên các thư viện mà app có sử dụng
Quy định các permission mà ứng dụng được phép.
AndroidManifest.xml
52
Thiết lập UI cho một activity
Create UI
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// load nội dung của layout từ file XML
setContentView(R.layout.main);
}
}
main.xml chứa trong thư mục res/layout
53
Tạo một nội dung trực tiếp từ code không dùng XML
Create UI
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
54
Tạo button đáp ứng sự kiện click
Basic Event handler
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = new Button(this);
btn.setText("Click me!");
setContentView(btn);
}
}
55
Khai báo listener để xử lý sự kiện click
Basic Event handler (2)
// thiết lập event click handler
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(
getApplicationContext(),
"Hello World",
Toast.LENGTH_LONG).show();
}
});
56
Three Main Approaches for
Android programming
57
Java-based
Sử dụng Java để định nghĩa String, layout window, tạo các
GUI, gán event handler, giống như lập trình Swing
XML-based
Sử dụng file XML để định nghĩa String, layout window, tạo
các GUI, gán event handler. Phương thức Java chỉ đọc layout
từ file XML và truyền vào setContentView.
Hybrid
Sử dụng file XML định nghĩa String, layout window, và tạo
GUI control. Sử dụng Java để gán event handler.
Three Main Approaches
58
Java-based approach
public class NguyenHaGiangActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String message="Message 1";
LinearLayout window = new LinearLayout(this);
TextView txt = new TextView(this);
txt.setText(message);
Button btn = new Button(this);
btn.setText("Button Label");
btn.setOnClickListener(new SomeHandler());
window.addView(txt); window.addView(btn);
setContentView(window);
}// end onCreate
… //(next page)
Code Java
59
Java-based approach
... //(previous page)
private class SomeHandler implements OnClickListener{
@Override
public void onClick(View clickedButton)
{
// doSomething();...
Toast.makeText(getApplicationContext(), "Hello World",
Toast.LENGTH_LONG).show();
} // end onClick
} // end class SomeHandler
} // end class NguyenHaGiangActivity
Code Java
60
XML-based approach
Java
XML
public class SomeClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); }
public void handlerMethod(View clickedButton) {
String someName = getResources().getString(R.string.name);
doSomethingWith(someName);
} }
res/values/strings.xml res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="name">…</string>
…
</resource>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout …>
<TextView …/>
<Button …android:onClick="handlerMethod" />
…
</LinearLayout>
61
Java
XML
Lấy control thông qua IDs
Không sử dụng android:onClick để gán handler
Hybrid approach
public class SomeClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.button_id);
b.setOnClickListener(new SomeHandler());
}
private class SomeHandler implements OnClickListener {
@Override
public void onClick(View clickedButton) {
doSomething(...);
}
} }
62
Java-based Layout
63
Approach
Sử dụng Java để định nghĩa chuỗi, layout window, tạo GUI
control, gán trình xử lý sự kiện.
Avantages
Thân thiện với giới Java desktop developer. Giống cách tiếp
cận để xây dựng Swing, SWT, và AWT.
Thuận tiện cho việc xây dựng layout động, tuỳ biến vào logic
lập trình.
Disadvantages
Phần code và phần UI xen lẫn với nhau.
Chỉ thấy được giao diện khi chạy ứng dụng.
Main idea
64
Phần code trong onCreate
Code (Main Method)
String appName = "Demo Java-baed Application";
String windowText = "Press the button";
String buttonLabel = "Show Greeting";
LinearLayout mainWindow = new LinearLayout(this);
mainWindow.setOrientation(LinearLayout.VERTICAL);
setTitle(appName);
TextView label = new TextView(this);
label.setText(windowText);
mainWindow.addView(label);
Button greetingButton = new Button(this);
greetingButton.setText(buttonLabel);
greetingButton.setOnClickListener(new Toaster());
mainWindow.addView(greetingButton);
setContentView(mainWindow);
65
Lớp thực thi interface onClickListener
Code (Main Method)
private class Toaster implements OnClickListener {
@Override
public void onClick(View clickedButton) {
String greetingText = "Hello from Android!";
Toast t = Toast.makeText(getApplicationContext(), greetingText,
Toast.LENGTH_LONG);
t.show();
}
}
66
Results on Emulator
67
XML-based Layout
68
Approach
Sử dụng file XML để định nghĩa chuỗi, layout window, tạo
UI control và gán event handler
Định nghĩa layout và control in res/layout/main.xml
Định nghĩa các chuỗi trong res/values/strings.xml
Advantages
Dễ quản lý
Dùng visual layout editor của eclipse
Đây là cách tiếp cận được khuyến khích
Disaventages
Việc tạo layout động.
Main Idea
69
res/layout/main.xml
Định nghĩa layout và widget với mô tả XML
<LinearLayout …> define widget </LinearLayout>
Tham chiếu đến chuỗi (strings.xml) với
@string/string_name
Gán event handler với android:onClick
res/value/strings.xml
Định nghĩa chuỗi dùng trong GUI
Java code
Tham chiếu đến layout với R.layout.main
Tham chiếu đến chuỗi với
getString(R.string.string_name)
Tham chiếu đến widget với findViewById(R.id.xyz_id)
More details
res/layout/main.xml
70
Project layout
Tham chiếu đến layout được định nghĩa trong
res/layout/main.xml với R.layout.main
Định nghĩa giao diện và widget. Đôi khi có
phần khai báo event handler.
Tham chiếu đến string định nghĩa trong
res/values/strings.xml với cú pháp
@string/string_name
Định nghĩa chuỗi được dùng trong UI
71
Code (res/layout/main.xml)
Các thuộc tính orientation,
layout_width… được định nghĩa
trong JavaDoc API cho LinearLayout
Chuỗi được định nghĩa trong
res/values/strings.xml
Phương thức này là public
khai báo trong main class,
giá trị trả về là void, có
một tham số là View
72
Code (res/values/strings.xml)
Tên của ứng dụng
Các chuỗi được định nghĩa trong strings.xml được
tham chiếu trong main.xml và java code
main.xml: @string/string_name
Java code: getString(R.string.string_name)
73
Java code
Tham chiếu đến main.xml
Tham chiếu đến chuỗi tên
greeting_text được định
nghĩa trong strings.xml
74
Hybrid Layout
75
Approach
Sử dụng XML để định nghĩa string, layout window, và UI
widget
Sử dụng Java để gán event handler
Advantages
Tương tự như cách tiếp cận XML-based
Ngoài ra việc gán event handler từ code có thể dễ hiểu hơn,
do event handler cũng là phần java code.
Disadvantages
Khó tạo layout động.
Main idea
76
Code (res/layout/main.xml)
Định nghĩa id cho button để button có
thể được tham chiếu trong Java code
với findViewById(R.id.greetind_button)
Không gán event handler ở đây, do ta
sẽ làm trong java code!
77
Code (res/values/strings.xml)
File strings.xml tương tự như phần trước!
78
Code (java)
Phải thiết lập setContentView
trước khi gọi findViewById, nếu
không sẽ nhận được null
79
Viết ứng dụng basic calculator cho phép tính các phép toán cơ
bản {cộng,trừ, nhân, chia} theo các cách tiếp cận như sau:
Java based
XML based
Hybrid
Bài tập
80
Basic Layout
Organizing the Screen
81
LinearLayout
Cách thức tổ chức layout nhiều cấp
Sử dụng màu sắc
RelativeLayout
TableLayout
Contents
82
XML-based
Khai báo layout trong res/layouts/some_layout.xml
Thiết lập thuộc tính XML
Sử dụng visual editor trong Eclipse
Load layout với setContentView(R.layout.some_layout)
Java-based
Tạo thể hiện layout, thiết lập thuộc tính, chèn sub-layout
LinearLayout window = new LinearLayout(this)
// thiết lập các thuộc tính cho window
…
window.addView(widgetOrLayout)
Load với phương thức setContentView(window)
Layout Strategies
83
Mỗi lớp Layout đều có lớp inner là LayoutParams định nghĩa
tham số XML cho layout sử dụng.
Các tham số này có tên android:layout_xyz, thường dùng để
định kích thước và canh lề.
VD:
<LinearLayout
android:layout_width = "match_parent"
android:layout_height= "wrap_content"
android:gravity = "center_horizontal"
android:background = "@color/color_1">
…
</LinearLayout>
XML Layout Attributes
84
Size
android:layout_width, android:layout_height
match_parent: fill the parent space (minus padding)
Phiên bản cũ là fill_parent
wrap_content: kích thước bao nội dung
android:layout_weight:
Giá trị số cho biết tỷ lệ phân chia kích thước
Alignment
android:layout_gravity
Cho biết cách thức canh lề của view ở trong containing view
android:gravity
Cho biết cách thức text và thành phần bên trong view được canh
lề.
Commonly Used Attributes
85
Alignment (tt)
Giá trị thường dùng
Top, bottom, left, right, center_vertical, center_horizontal,
center, fill_vertical, fill_horizontal, fill, clip_vertical,
clip_horizontal.
Margin (blank space outside)
Android:layout_marginBottom, android:layout_marginTop,
android:layout_marginLeft, android:layout_marginRight
Units
dp: density-independent pixels (scaled by device resol.)
sp: scaled pixel (font size)
px: pixels, in: inches, mm: milimeters.
Commonly Used Attributes
86
Padding (blank space inside)
android:paddingBottom/Top/Left/Right
Giá trị là con số theo đơn vị unit bên trên
ID
android:id
Sử dụng khi java code cần tham chiếu đến view
Sử dụng trong RelativeLayout khi cần tham chiếu đến view nào
đó để làm mốc.
Colors
android:background (color, image)
android:textColor
Giá trị thường dùng
"#rrggbb", "#aarrggbb", "@color/color_name"
Commonly Used Attributes
87
Ý nghĩa
Cho phép đặt những thành phần theo 1 dòng hay 1 cột
Có thể lồng ghép để tạo thành những dòng với các cột.
Các thuộc tính XML quan trọng.
android:orientation
"horizontal" (row) or "vertical" (column)
Horizontal là mặc định
android:gravity
Cách các view bên trong được canh lề
Basic LinearLayout
88
General Approach
Example (nested layout)
89
Example (nested layout)
Horizontal Linearlayout với
gravity là left
Horizontal linearlayout với
gravity là center_horizontal
90
Example (nested layout)
Horizontal LinearLayout
Chứa 2 layout Horizontal LinearLayout
Layout đầu tiên màu vàng có
layout_width ="wrap_content" và
gravity ="left"
Layout thứ hai màu xanh có
layout_width="match_parent"và
gravity là "right"
91
Example (nested layout)
Horizontal LinearLayout
Chứa 3 vertical bên trong
1. LinearLayout với orientation là
vertical và 4 button bên trong
2. Chứa RadioGroup với orientation
là vertical và 4 RadioButton bên
trong.
3. LinearLayout với orientation là
vertical và 4 LinearLayout bên
trong.
2 cột đầu có layout_width là
wrap_content và cột thứ 3 là
match_parent.
92
Example (nested layout)
Vertical LinearLayout
Layout này chứa 4 horizontal bên
trong.
1. Gravity là center_horizontal
2. Gravity là left
3. Gravity là right
4. Chứa 2 horizontal LinearLayout
bên trong
1. Layout_width là wrap_content
và gravity là left
2. Layout_width là match_parent
và gravity là right
93
Example (nested layout)
Button
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop= "20dp"
Button
Android:layout_width="match_parent"
94
Mặc dù màu có thể định nghĩa cụ thể trong file layout
(background="#ff0000"), tuy nhiên có thể định nghĩa tên màu
trong file riêng biệt, khi đó tham chiếu thông qua tên màu.
Nếu cần thì chỉ thay đổi giá trị của màu, lúc đó sẽ tác động lên
toàn bộ nơi tham chiếu đến tên màu.
Syntax
Convention
Sử dụng file res/values/colors.xml
Có thể đặt với bất cứ tên nào miễn hợp lệ!
Setting Colors
<resources>
<color name="color_name_1">#rrggbb</color>
… <!-- Other colors -->
</resources>
95
Color File
<resources>
<color name="red">#f00</color>
<color name="orange">#ffa500</color>
<color name="yellow">#ffff00</color>
<color name="green">#0f0</color>
<color name="blue">#00f</color>
<color name="indigo">#4b0082</color>
<color name="violet">#ee82ee</color>
<color name="back">#000</color>
<color name="white">#fff</color>
</resources>
colors.xml
96
Tham chiếu màu trong file layout.
Tham chiếu trong code
getResources().getColor(R.color.red)
Layout file
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView1"
android:text="@string/red"
android:layout_marginTop="20px"
android:layout_centerHorizontal="true"
android:padding="25dp“
android:background="@color/red">
</TextView>
97
Ý nghĩa
Gán những con số cho android:layout_weight, kích thước sẽ
được phân chia theo tỷ số này.
Cách dùng (cho height)
Thiết lập android:layout_height = 0dp
Sử dụng những giá trị tương ứng cho layout_weight
Vd: nếu có 3 thành phần với layout_weight tương ứng là 1, 1
và 2 thì tỷ lệ tương ứng là 25%, 25% và 50% của 3 thành
phần so với chiều cao của cha.
Tương tự cho thiết lập width!
Có thể xem giá trị weight là 100, sử dụng những giá trị 25, 25, và
50 cũng tương ứng như 1, 1 và 2!
Layout Weight
98
Layout File
99
Result
100
Ý tưởng
Mỗi view có id xác định
Cho phép xác định vị trí tương đối của view với những view
khác thông qua id.
Thuộc tính XML quan trọng
Align với container
layout_alignParentBottom (Top,Right,Left)
layout_centerInParent (centerHorizontal, centerVertical)
Tất cả giá trị là true hay false
Align với view
layout_alignBottom (Top, Right, Left)
layout_toLeftOf ( toRightOf, above, below)
Tất cả giá trị là id của view nào đó
RelativeLayout
101
Button thứ 1
<Button id = "@+id/button_1"
android:layout_alignParentRight="true"… />
Button thứ 2
<Button android:layout_toLeftOf="@id/button_1"… />
Referring to Existing IDs
@+id: để gán id mới
@id: (không có dấu cộng)
tham chiếu đến button đã có
102
Example
103
Example
<TextView
android:id = "@+id/behind"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ff0000"
android:textColor="#ffff00"
android:textSize="42dp"
android:text="Behind"
android:gravity="center"/>
<TextView
android:layout_alignTop="@id/behind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:textColor="#000000"
android:textSize="18dp"
android:text="On Top"
android:layout_marginTop="25dp"
android:layout_centerHorizontal="true" />
104
Example
<Button
android:id="@+id/image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search Images"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
<Button
android:layout_alignBottom="@id/image_button"
android:layout_toLeftOf="@id/image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Search Web" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/image_button"
android:layout_alignRight="@id/image_button"/>
105
A layout that lets you specify exact location (x/y coordinates) of
its children.
Abs layouts are less flexible and harder to maintain than other
types of layouts without abs pos.
=> So not recommended for most UI development!
AbsoluteLayout
(0,0)
(20,30)
View
android:layout_x="20px"
android:layout_y=“30px"
screen
106
Example
AbsoluteLayout - example
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dp"
android:layout_y="15dp"
android:width="220px"
android:height=“50px"
android:text="Back" />
</AbsoluteLayout>
Tọa độ của view
kích thước của view
107
FL is designed to display a single item at a time.
You can have multiple elements within a FrameLayout but each
element will be positioned based on the top left of the screen.
Elements that overlap will be displayed overlapping.
FL can become more useful when elements are hidden and
displayed programmatically.
You can use the attribute android:visibility in the XML to hide
specific elements. After that can setVisibility from code to show.
FrameLayout
108
Example
FrameLayout - Example
ImageView
TextView
109
Idea
Cho phép widget và layout khác chứa bên trong grid (không
có đường biên)
Tương tự như HTML table, số dòng và cột xác định tự động,
không cần phải khai báo.
Các view được đặt bên trong TableRow
XML attributes
android:stretchColumns
Chứa chỉ số cột sẽ được stretch khi table nhỏ hơn parent
android:shrinkColumns
Liệt kê các cột sẽ co lại khi table lớn hơn parent
android:collapseColumns
Những cột sẽ bị thu lại, có thể lập trình để hiện ra sau.
TableLayout
110
Dùng để định nghĩa một dòng trong TableLayout
Thuộc tính XML quan trọng sử dụng bên trong TableRow
android:layout_column
Có thể xác định chính xác cột của thành phần, cột bỏ qua đó sẽ
rỗng.
android:layout_span
Số cột mà thành phần sẽ chiếm giữ, giống colspan trong HTML
table
Không có thuộc tính tương ứng với rowspan trong HTML, dùng
nested table để thực hiện.
TableRow
111
Example
112
Example
113
Example
114
Example
115
Tạo activity có layout như sau: (chỉ dùng TableLayout)
VD
116
Button Widgets
117
Buttons
ImageButtons với 1 ảnh
ImageButtons với 3 ảnh (normal/focused/pressed)
RadioButtons – OnClickListener
RadioButtons – OnCheckedChangeListener
CheckBoxes
ToggleButtons
Topics in this section
118
Button layout
Horizontal LinearLayout (3 button)
Horizontal LinearLayout (3
Imagebutton)
Horizontal LinearLayout (3
Imagebutton)
Horizontal RadioGroup (3 RadioButton)
Horizontal RadioGroup (3 RadioButton)
Horizontal LinearLayout (3 CheckBox)
Horizontal RadioGroup (3 ToggleButton)
Vertical LinearLayout
119
Là nút nhấn có nhãn bên trên
Listener: View.OnClickListener
Nếu sử dụng phương pháp XML based thì có thể không cần
phải khai báo lớp Listener này.
Thuộc tính XML thường dùng
android:text
Nhãn của button, có thể truy xuất bằng code java thông qua
phương thức setText và getText
android:onClick
Basic Button
120
Là nút nhấn có ảnh hiển thị bên trên
Listener: View.OnClickListener
Thuộc tính XML
android:src
ảnh hiển thị trên button, tham chiếu đến tên (không có phần mở
rộng) của ảnh trong thư mục res/drawable
Hỗ trợ các kiểu ảnh png, jpeg, gif và bmp.
Có thể dùng Java code để thiết lập với setImageDrawable
android:onClick
ImageButton
121
ImageButton
122
Là nút chọn, cho phép chọn một trong số mục chọn cùng nhóm.
Listener
View.OnClickListener
Gán cho mỗi nút nếu quan tâm đến sự kiện chọn trên từng item.
Có thể dùng cách khác là bắt sự kiện trên RadioGroup.
Thuộc tính XML
android:text, android:onClick
RadioButton
123
Tương tự như LinearLayout, cho phép chứa các RadioButton bên
trong.
Quản lý trạng thái RadioButton là exclusive.
Listener: OnCheckedChangeListener
Thuộc tính XML
Tương tự như LinearLayout
Sử dụng android:id để tham chiếu trong chương trình (khi
thiết lập OnCheckedChangeListener)
Không có thuộc tính andoird:onXYZ để thiết lập RadioGroup
listener trong XML.
RadioGroup
124
Minh hoạ
Đáp ứng với mỗi click trên từng radio button bằng cách hiện
thông điệp message nào đó
Cách thực hiện
Đặt các radiobutton vào trong radiogroup
Gán event handler, sử dụng onClick cho mỗi RadioButton.
Không cần id cho RadioGroup, không cần listener cho
RadioGroup.
Event handler cho RadioButton
125
Event handler cho RadioButton
126
Minh hoạ
Mỗi khi nút nào được nhấn thì hiển thị message báo tên nút
được nhấn
Cách thực hiện
Đặt RadioButton bên trong RadioGroup
Trong XML, tạo id cho RadioGroup
Trong Java, xác định RadioGroup và thiết lập Listener
Event handler cho RadioGroup
127
Xử lý trong hàm onCreate
Event handler cho RadioGroup
128
Minh hoạ cách dùng
CheckBox
129
Là dạng tương tự như combo box
Listener
AdapterView.OnItemSelectedListener
AdapterView.OnItemClickedListener
Thuộc tính XML
android:id, android:prompt
android:entry
Định nghĩa mục chọn, có thể định nghĩa trong strings.xml hoặc
arrays.xml
Spinner
130
Khai báo Spinner
Spinner
Strings.xml hay
arrays.xml
131
Tham chiếu đến Spinner
Lớp thực thi Listener
Spinner
Lấy item được chọn
132
Cách tiếp cận khác là tạo spinner thông qua code Java.
Sinh viên tìm hiểu thêm cách tiếp cận này!
Spinner
133
Intent
134
Có thể dùng Intent để gọi một activity từ một activity đang hiển
thị.
Cú pháp
Dùng Intent để gọi Activity
Intent activityIntent = new Intent(this, NewActivity.class);
startActivity(activityIntent);
…
// khai báo trong AndroidManifest.xml
<activity android:name = "NewActivity"
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
135
Cách thực hiện
Tạo đối tượng Bundle, chứa dữ liệu cần gởi cho activity mới,
attach đối tượng Bundle này kèm theo Intent
Cú pháp
Java (original Activity)
Java (new Activity)
Gởi dữ liệu giữa Activity
Intent activityIntent = new Intent(this, NewActivity.class);
Bundle newActivityInfo = new Bundle();
newActivityInfo.putXYZ(…); //putDouble, putString…
activityIntent.putExtras(newActivityInfo);
startActivity(activityIntent);
Intent intent = getIntent();
Bundle info = intent.getExtras();
If (info != null) {
// getXYZ()…
}
136
VD: Truyền dữ liệu từ original activity sang new activity.
Gởi dữ liệu giữa Activity
137
Hàm xử lý submit
Gởi dữ liệu giữa Activity
public void submit(View v)
{
Intent newActivity = new Intent(this,NewActivity.class);
Bundle info = new Bundle();
String sName, sEmail;
EditText ed1 = (EditText)findViewById(R.id.editText1);
EditText ed2 = (EditText)findViewById(R.id.editText2);
info.putString("Name", ed1.getText().toString());
info.putString("Email", ed2.getText().toString() );
newActivity.putExtras(info);
startActivity(newActivity);
}
138
Xử lý trong new activity
Gởi dữ liệu giữa Activity
setContentView(R.layout.new_activity);
// tham chiếu đến intent
String sName, sEmail;
Intent intent = getIntent();
Bundle info = intent.getExtras();
if (info != null)
{
sName = info.getString("Name");
sEmail = info.getString("Email");
TextView tv1 = (TextView)findViewById(R.id.tvHoTen);
tv1.setText(sName);
TextView tv2 = (TextView)findViewById(R.id.tvEmail);
tv2.setText(sEmail);
}
139
Tình huống:
Activity mới sau khi tương tác với user, thu thập được dữ liệu,
kết thúc activity và gởi lại cho activity gọi nó (original
activity).
Gởi dữ liệu giữa Activity
Activity A Activity B
Gọi activity B (có thể
truyền dữ liệu cho B tuỳ ý)
Acvitity B kết thúc và
gởi dữ liệu về cho A
140
Phần xử lý ở lớp activity A (Original Activity)
Phần xử lý trên thường đặt trong hàm xử lý sự kiện của button
hay menu.
Gởi dữ liệu giữa Activity
Intent newActivity = new Intent(this, ActivityB.class);
final int result = 1;
// gọi activity B hiển thị và chờ lấy kết quả trả về
startActivityForResult(newActivity, result);
141
Phần xử lý ở lớp activity A (Original Activity), khi nhận kết quả
trả về từ activity B (new Activity).
Override phương thức onActivityResult trong ActivityA để nhận
kết quả trả về (khi ActivityB đóng).
Gởi dữ liệu giữa Activity
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// lấy Bundle chứa dữ liệu
Bundle bundle = data.getExtras();
int data1 = bundle.getInt("<tên dữ liệu 1>");
int data2 = bundle.getInt("<tên dữ liệu 2>");
String data3 = bundle.getString("<tên dữ liệu 3>");
…
}
142
Phần xử lý trong activity B (activity được gọi từ A)
Gởi dữ liệu giữa Activity
// gởi dữ liệu về activity trước
Intent intent = new Intent();
Bundle bundle = new Bundle();
// gởi dữ liệu vào bundle
bundle.putInt("<tên dữ liệu 1>", data1);
bundle.putInt("<tên dữ liệu 2>", data2);
Bundle.putString("<tên dữ liệu 3>", data3);
intent.putExtras(bundle); // gởi kèm dữ liệu
setResult(RESULT_OK, intent); // gởi kết quả về
finish(); // đóng activity
143
Cung cấp URI tham chiếu đến new activity
New Activity sẽ đăng ký trong androidmanifest có mô tả dùng
URI
Cú pháp
Java (original Activity)
XML (phần khai báo cho activity được gọi)
Gọi Activity thông qua URI
Uri uri = Uri.parse("call://ex.intent.uri/ActivityC");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="call" android:host="ex.intent.uri"/>
</intent-filter>
144
Ý tưởng
Nhúng tham số trong URI, tương tự như query string trong
Web.
Cú pháp
Java (original Activity)
Java (new Activity)
Gởi dữ liệu qua tham số URI
Uri uri = Uri.parse("call://ex.intent.uri/ActivityC?str=hello class");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Uri uri = getIntent().getData();
String str = uri.getQueryParameter("str");
// xử lý với chuỗi, nếu truyền là chuỗi số thì chuyển sang số
…
145
Extras
Pros
Can send data of different types
No parsing required in Activity that receives the data
Cons
More complex for originating activity
Requires parsing in originating activity if values come from
EditText
URI parameters
Pros:
Simpler for originating Activity, especially if EditText used
More consistent with URI usage
Cons
Can send Strings only
Requires parsing in receiving Activity
Extras vs. URI parameter
146
Ý tưởng
Tạo tab window, mỗi tab là một activity
Có thể gọi activity thông qua class hay URI
Gởi dữ liệu thông qua extras Bundle hay URI
Tab Window Activity và các activity phải cùng project.
Cú pháp
Java
Mở rộng từ lớp TabActivity, sử dụng TabHost, TabSpec
XML: (androidManifest.xml)
Tương tự như phần trước.
Activity với Tabbed Window
147
Sử dụng TabActivity: outline
148
Tạo ứng dụng demo như sau:
Activity với Tabbed Window
149
Activity với Tabbed Window
Resources resource = getResources();
TabHost host = getTabHost();
Intent intent1 = new Intent(this, activity1.class);
Drawable tabIcon1 = resource.getDrawable(R.drawable.b1);
TabSpec tab1 = host.newTabSpec("Tab One").setIndicator("Activity 1",tabIcon1)
.setContent(intent1);
host.addTab(tab1);
Intent intent2 = new Intent(this, activity2.class);
Drawable tabIcon2 = resource.getDrawable(R.drawable.b2);
TabSpec tab2 = host.newTabSpec("Tab Two").setIndicator("Activity 2",tabIcon2)
.setContent(intent2);
host.addTab(tab2);
Intent intent3 = new Intent(this, activity3.class);
Drawable tabIcon3 = resource.getDrawable(R.drawable.b3);
TabSpec tab3 = host.newTabSpec("Tab Three").setIndicator("Activity 3",tabIcon3)
.setContent(intent3);
host.addTab(tab3);
150
Service là thành phần nền tảng của Android.
Đôi khi ứng dụng run process với khoảng thời gian lâu và không
có hoặc hiếm khi có sự can thiệp từ người dùng.
Tiến trình nền này có thể xử lý ngay cả khi phone được sử dụng
cho activity/task khác.
Các lớp thực thi service phải mở rộng từ lớp Service trong gói
android.app.Service
Các phương thức override là: onCreate, onDestroy, onStart
Service
151
Minh hoạ:
Tạo một service thực thi việc chạy file nhạc nền.
Service này được gọi thông qua UI trên Activity.
Service
Sử dụng ToggleButton
để on/off service
152
Bước 1: Chuẩn bị file nhạc mp3.
Tạo thư mục raw trong res (nếu chưa có raw)
Import một file nhạc mp3 nào đó vào thư mục res/raw
Service
Ví dụ import file
only_time.mp3 vào thư
mục raw
153
Bước 2: Tạo layout cho activity chính của ứng dụng
Service
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal" >
<TextView android:text="Services Demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35dp" >
</TextView>
<ToggleButton android:text="ToggleButton"
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doService" >
</ToggleButton>
</LinearLayout>
154
Bước 3: Tạo một lớp mới extend từ Service
Service
Tên lớp service sẽ tạo
Extend từ lớp Service
155
Bước 4:
Override các phương
thức onCreate,
onDestroy, onStart của
service.
Tạo đối tượng player là
MediaPlayer
Service
public class MyService extends Service {
private static final String TAG ="HaGService";
private MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
}
}
156
Bước 5: viết các phần xử lý cho onCreate
Hiển thị một messge báo "Service created"
Hiển thị thông tin debug trong LogCat
Load file nhạc vào biến player
Thiết lập cờ looping.
Service
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();
Log.d(TAG,"onCreate");
player = MediaPlayer.create(this, R.raw.only_time);
player.setLooping(false);
}
157
Bước 6: thực thi hàm onStart và onDestroy
Service
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start(); // chạy file nhạc
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
Log.d(TAG,"onDestroy");
player.stop(); // dừng file nhạc
}
158
Bước 7: viết phần xử lý cho sự kiện onClick của ToggleButton
trong activity chính của ứng dụng
Service
public void doService(View view)
{
// tham chiếu đến ToggleButton
ToggleButton btn = (ToggleButton)view;
if (btn.isChecked()==true) {
startService( new Intent(this,MyService.class) );
}
else {
stopService( new Intent(this,MyService.class) );
}
}
159
Bước 8: khai báo service trong AndroidManifest.xml
Việc khai báo service trong file AndroidManifest là bắt buộc.
Nếu thiếu phần khai báo thì service sẽ không chạy được.
Service
<service android:name="MyService" android:enabled="true"></service>
Tên của service Thiết lập trạng thái
của Service là anable
160
Broadcast Receivers
Thành phần có thể respond lại các thông báo của hệ thống.
VD:
Thông báo màn hình bị tắt, pin yếu, ảnh được chụp
Thông báo cuộc gọi đến, tin nhắn đến…
Ngoài ra ứng dụng cũng có thể khởi tạo broadcast để báo cho
ứng dụng khác hay hệ thống biết thông tin gì đó
Ứng dụng báo đã hoàn thành việc download về thiết bị và sẵn
sàng cho việc sử dụng.
Có thể dùng để start một service chạy nền nào đó.
Phần tìm hiểu thêm
161
Content providers
Quản lý sự chia sẻ dữ liệu của ứng dụng. Bao gồm các dạng
Shared preferences
Internal storage
Local cache
External storage
SQLite database (Chapter 12: Android App Dev for Dummies)
Network connection
Phần tìm hiểu thêm

Mais conteúdo relacionado

Mais procurados

Xe hai bánh tự cân bằng.pdf
Xe hai bánh tự cân bằng.pdfXe hai bánh tự cân bằng.pdf
Xe hai bánh tự cân bằng.pdfMan_Ebook
 
Đồ án Thiết kế bộ chỉnh lưu hình cầu 1 pha kép để điều khiển tốc độ động cơ đ...
Đồ án Thiết kế bộ chỉnh lưu hình cầu 1 pha kép để điều khiển tốc độ động cơ đ...Đồ án Thiết kế bộ chỉnh lưu hình cầu 1 pha kép để điều khiển tốc độ động cơ đ...
Đồ án Thiết kế bộ chỉnh lưu hình cầu 1 pha kép để điều khiển tốc độ động cơ đ...Verdie Carter
 
Kiến trúc máy tính và hợp ngữ bài 02
Kiến trúc máy tính và hợp ngữ bài 02Kiến trúc máy tính và hợp ngữ bài 02
Kiến trúc máy tính và hợp ngữ bài 02Nhóc Nhóc
 
Tai lieu lap trinh c cho 8051
Tai lieu lap trinh c cho 8051Tai lieu lap trinh c cho 8051
Tai lieu lap trinh c cho 8051engineertrongbk
 
Phân tích và thiết kế hệ thống quản lý bán hàng
Phân tích và thiết kế hệ thống quản lý bán hàngPhân tích và thiết kế hệ thống quản lý bán hàng
Phân tích và thiết kế hệ thống quản lý bán hàngleemindinh
 
Chuong 4. lap trinh hop ngu
Chuong 4. lap trinh hop nguChuong 4. lap trinh hop ngu
Chuong 4. lap trinh hop ngumituan
 
Thiết kế và chế tạo cánh tay robot 5 bậc tự do phân loại sản phẩm ứng dụng cô...
Thiết kế và chế tạo cánh tay robot 5 bậc tự do phân loại sản phẩm ứng dụng cô...Thiết kế và chế tạo cánh tay robot 5 bậc tự do phân loại sản phẩm ứng dụng cô...
Thiết kế và chế tạo cánh tay robot 5 bậc tự do phân loại sản phẩm ứng dụng cô...Man_Ebook
 
Bài 1 - Làm quen với C# - Lập trình winform
Bài 1 - Làm quen với C# - Lập trình winformBài 1 - Làm quen với C# - Lập trình winform
Bài 1 - Làm quen với C# - Lập trình winformMasterCode.vn
 
Các phương pháp thiết kế bộ điều khiển PID.docx
Các phương pháp thiết kế bộ điều khiển PID.docxCác phương pháp thiết kế bộ điều khiển PID.docx
Các phương pháp thiết kế bộ điều khiển PID.docxhunhlhongthi
 

Mais procurados (20)

Đề tài Quản lý tiền điện
Đề tài Quản lý tiền điệnĐề tài Quản lý tiền điện
Đề tài Quản lý tiền điện
 
Xe hai bánh tự cân bằng.pdf
Xe hai bánh tự cân bằng.pdfXe hai bánh tự cân bằng.pdf
Xe hai bánh tự cân bằng.pdf
 
Đồ án Thiết kế bộ chỉnh lưu hình cầu 1 pha kép để điều khiển tốc độ động cơ đ...
Đồ án Thiết kế bộ chỉnh lưu hình cầu 1 pha kép để điều khiển tốc độ động cơ đ...Đồ án Thiết kế bộ chỉnh lưu hình cầu 1 pha kép để điều khiển tốc độ động cơ đ...
Đồ án Thiết kế bộ chỉnh lưu hình cầu 1 pha kép để điều khiển tốc độ động cơ đ...
 
Đề tài: Triển khai DHCP Server trên mô hình mạng ba lớp, HOT
Đề tài: Triển khai DHCP Server trên mô hình mạng ba lớp, HOTĐề tài: Triển khai DHCP Server trên mô hình mạng ba lớp, HOT
Đề tài: Triển khai DHCP Server trên mô hình mạng ba lớp, HOT
 
luan van thac si tim hieu flutter va ung dung
luan van thac si tim hieu flutter va ung dungluan van thac si tim hieu flutter va ung dung
luan van thac si tim hieu flutter va ung dung
 
Kiến trúc máy tính và hợp ngữ bài 02
Kiến trúc máy tính và hợp ngữ bài 02Kiến trúc máy tính và hợp ngữ bài 02
Kiến trúc máy tính và hợp ngữ bài 02
 
Mạch chỉnh lưu
Mạch chỉnh lưuMạch chỉnh lưu
Mạch chỉnh lưu
 
Tai lieu lap trinh c cho 8051
Tai lieu lap trinh c cho 8051Tai lieu lap trinh c cho 8051
Tai lieu lap trinh c cho 8051
 
Bài giảng Assembly
Bài giảng AssemblyBài giảng Assembly
Bài giảng Assembly
 
Đề tài: Quản lí Tour du lịch, HAY, 9đ
Đề tài: Quản lí Tour du lịch, HAY, 9đĐề tài: Quản lí Tour du lịch, HAY, 9đ
Đề tài: Quản lí Tour du lịch, HAY, 9đ
 
Đề tài: Mô hình điều khiển, giám sát bãi giữ xe ô tô tự động, 9đ
Đề tài: Mô hình điều khiển, giám sát bãi giữ xe ô tô tự động, 9đĐề tài: Mô hình điều khiển, giám sát bãi giữ xe ô tô tự động, 9đ
Đề tài: Mô hình điều khiển, giám sát bãi giữ xe ô tô tự động, 9đ
 
Đề tài: Quản lý hệ thống bán vé máy bay của Vietnam Airline, 9đ
Đề tài: Quản lý hệ thống bán vé máy bay của Vietnam Airline, 9đĐề tài: Quản lý hệ thống bán vé máy bay của Vietnam Airline, 9đ
Đề tài: Quản lý hệ thống bán vé máy bay của Vietnam Airline, 9đ
 
Phân tích và thiết kế hệ thống quản lý bán hàng
Phân tích và thiết kế hệ thống quản lý bán hàngPhân tích và thiết kế hệ thống quản lý bán hàng
Phân tích và thiết kế hệ thống quản lý bán hàng
 
Chuong 4. lap trinh hop ngu
Chuong 4. lap trinh hop nguChuong 4. lap trinh hop ngu
Chuong 4. lap trinh hop ngu
 
Thiết kế và chế tạo cánh tay robot 5 bậc tự do phân loại sản phẩm ứng dụng cô...
Thiết kế và chế tạo cánh tay robot 5 bậc tự do phân loại sản phẩm ứng dụng cô...Thiết kế và chế tạo cánh tay robot 5 bậc tự do phân loại sản phẩm ứng dụng cô...
Thiết kế và chế tạo cánh tay robot 5 bậc tự do phân loại sản phẩm ứng dụng cô...
 
Luận văn: Xây dựng ứng dụng Android xem video trực tuyến, HAY
Luận văn: Xây dựng ứng dụng Android xem video trực tuyến, HAYLuận văn: Xây dựng ứng dụng Android xem video trực tuyến, HAY
Luận văn: Xây dựng ứng dụng Android xem video trực tuyến, HAY
 
Uml hà
Uml hàUml hà
Uml hà
 
Đề tài: Ứng dụng giải trí trên thiết bị di động Android, HOt, 9đ
Đề tài: Ứng dụng giải trí trên thiết bị di động Android, HOt, 9đĐề tài: Ứng dụng giải trí trên thiết bị di động Android, HOt, 9đ
Đề tài: Ứng dụng giải trí trên thiết bị di động Android, HOt, 9đ
 
Bài 1 - Làm quen với C# - Lập trình winform
Bài 1 - Làm quen với C# - Lập trình winformBài 1 - Làm quen với C# - Lập trình winform
Bài 1 - Làm quen với C# - Lập trình winform
 
Các phương pháp thiết kế bộ điều khiển PID.docx
Các phương pháp thiết kế bộ điều khiển PID.docxCác phương pháp thiết kế bộ điều khiển PID.docx
Các phương pháp thiết kế bộ điều khiển PID.docx
 

Destaque

Android chapter 02 - Setup to emulator
Android chapter 02 - Setup to emulatorAndroid chapter 02 - Setup to emulator
Android chapter 02 - Setup to emulatorJustin Hoang
 
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)Techacademy Software
 
Bài 1: Phát triển ứng dụng trên thiết bị di động
Bài 1: Phát triển ứng dụng trên thiết bị di động Bài 1: Phát triển ứng dụng trên thiết bị di động
Bài 1: Phát triển ứng dụng trên thiết bị di động Tuan Nguyen
 
lập trình di động
lập trình di độnglập trình di động
lập trình di độngtruong le hung
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studioParinita03
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android StudioSuyash Srijan
 
Android module 1 - ttth dh khtn
Android   module 1 - ttth dh khtn Android   module 1 - ttth dh khtn
Android module 1 - ttth dh khtn Vo Phong Phu
 

Destaque (9)

Android chapter 02 - Setup to emulator
Android chapter 02 - Setup to emulatorAndroid chapter 02 - Setup to emulator
Android chapter 02 - Setup to emulator
 
Android tutorial
Android tutorialAndroid tutorial
Android tutorial
 
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
 
Bài 1: Phát triển ứng dụng trên thiết bị di động
Bài 1: Phát triển ứng dụng trên thiết bị di động Bài 1: Phát triển ứng dụng trên thiết bị di động
Bài 1: Phát triển ứng dụng trên thiết bị di động
 
lập trình di động
lập trình di độnglập trình di động
lập trình di động
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studio
 
Android studio
Android studioAndroid studio
Android studio
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android Studio
 
Android module 1 - ttth dh khtn
Android   module 1 - ttth dh khtn Android   module 1 - ttth dh khtn
Android module 1 - ttth dh khtn
 

Semelhante a Lập trình android

mp_hag_slide_android_2321.pptx
mp_hag_slide_android_2321.pptxmp_hag_slide_android_2321.pptx
mp_hag_slide_android_2321.pptxKhngNguyn81
 
Cách tối ưu hóa môi trường lập trình ứng dụng cho Android - Tăng tốc máy ảo A...
Cách tối ưu hóa môi trường lập trình ứng dụng cho Android - Tăng tốc máy ảo A...Cách tối ưu hóa môi trường lập trình ứng dụng cho Android - Tăng tốc máy ảo A...
Cách tối ưu hóa môi trường lập trình ứng dụng cho Android - Tăng tốc máy ảo A...Ngo Trung
 
Tim hieu lap_trinh_android_va_tu_xay_dung_ung_dung_minh_hoa_20120809043144_31
Tim hieu lap_trinh_android_va_tu_xay_dung_ung_dung_minh_hoa_20120809043144_31Tim hieu lap_trinh_android_va_tu_xay_dung_ung_dung_minh_hoa_20120809043144_31
Tim hieu lap_trinh_android_va_tu_xay_dung_ung_dung_minh_hoa_20120809043144_31thequocbk
 
Bài 1: Giới thiệu Android
Bài 1: Giới thiệu AndroidBài 1: Giới thiệu Android
Bài 1: Giới thiệu Androidhoccungdoanhnghiep
 
Android chapter01-intro
Android chapter01-introAndroid chapter01-intro
Android chapter01-introluyen91
 
Ứng dụng từ điển cho SmartPhone từ Framework PhoneGap trên nền tảng Android
Ứng dụng từ điển cho SmartPhone từ Framework PhoneGap trên nền tảng Android Ứng dụng từ điển cho SmartPhone từ Framework PhoneGap trên nền tảng Android
Ứng dụng từ điển cho SmartPhone từ Framework PhoneGap trên nền tảng Android Lương Bá Hợp
 
Slide thuyet trinh android
Slide thuyet trinh androidSlide thuyet trinh android
Slide thuyet trinh androidkuto92love
 
Rhodes framework
Rhodes frameworkRhodes framework
Rhodes frameworkCà Mau
 
Lap trinh android_co_ban_eclip
Lap trinh android_co_ban_eclipLap trinh android_co_ban_eclip
Lap trinh android_co_ban_eclipktvinh
 
Tran Minh Thai Bai Giang Vc
Tran Minh Thai   Bai Giang VcTran Minh Thai   Bai Giang Vc
Tran Minh Thai Bai Giang VcNhat Thien
 
Báo cáo tuần 1
Báo cáo tuần 1Báo cáo tuần 1
Báo cáo tuần 1ThongErik
 
Android presentation
Android presentationAndroid presentation
Android presentationNguyen Duong
 
Google Android Security (Basic2Advanced)
Google Android Security (Basic2Advanced)Google Android Security (Basic2Advanced)
Google Android Security (Basic2Advanced)Giap Le Van
 
Mobile programming
Mobile programming Mobile programming
Mobile programming Thinh Huynh
 

Semelhante a Lập trình android (20)

mp_hag_slide_android_2321.pptx
mp_hag_slide_android_2321.pptxmp_hag_slide_android_2321.pptx
mp_hag_slide_android_2321.pptx
 
Cách tối ưu hóa môi trường lập trình ứng dụng cho Android - Tăng tốc máy ảo A...
Cách tối ưu hóa môi trường lập trình ứng dụng cho Android - Tăng tốc máy ảo A...Cách tối ưu hóa môi trường lập trình ứng dụng cho Android - Tăng tốc máy ảo A...
Cách tối ưu hóa môi trường lập trình ứng dụng cho Android - Tăng tốc máy ảo A...
 
Tim hieu lap_trinh_android_va_tu_xay_dung_ung_dung_minh_hoa_20120809043144_31
Tim hieu lap_trinh_android_va_tu_xay_dung_ung_dung_minh_hoa_20120809043144_31Tim hieu lap_trinh_android_va_tu_xay_dung_ung_dung_minh_hoa_20120809043144_31
Tim hieu lap_trinh_android_va_tu_xay_dung_ung_dung_minh_hoa_20120809043144_31
 
Ltgd ch01
Ltgd ch01Ltgd ch01
Ltgd ch01
 
Mobile group
Mobile groupMobile group
Mobile group
 
Android ios wp7
Android ios wp7Android ios wp7
Android ios wp7
 
Bài 1: Giới thiệu Android
Bài 1: Giới thiệu AndroidBài 1: Giới thiệu Android
Bài 1: Giới thiệu Android
 
Android chapter01-intro
Android chapter01-introAndroid chapter01-intro
Android chapter01-intro
 
Bao cao tuan 3
Bao cao tuan 3Bao cao tuan 3
Bao cao tuan 3
 
Ứng dụng từ điển cho SmartPhone từ Framework PhoneGap trên nền tảng Android
Ứng dụng từ điển cho SmartPhone từ Framework PhoneGap trên nền tảng Android Ứng dụng từ điển cho SmartPhone từ Framework PhoneGap trên nền tảng Android
Ứng dụng từ điển cho SmartPhone từ Framework PhoneGap trên nền tảng Android
 
Slide thuyet trinh android
Slide thuyet trinh androidSlide thuyet trinh android
Slide thuyet trinh android
 
Rhodes framework
Rhodes frameworkRhodes framework
Rhodes framework
 
Bao cao tuan 2
Bao cao tuan 2Bao cao tuan 2
Bao cao tuan 2
 
J2 me 07_1
J2 me 07_1J2 me 07_1
J2 me 07_1
 
Lap trinh android_co_ban_eclip
Lap trinh android_co_ban_eclipLap trinh android_co_ban_eclip
Lap trinh android_co_ban_eclip
 
Tran Minh Thai Bai Giang Vc
Tran Minh Thai   Bai Giang VcTran Minh Thai   Bai Giang Vc
Tran Minh Thai Bai Giang Vc
 
Báo cáo tuần 1
Báo cáo tuần 1Báo cáo tuần 1
Báo cáo tuần 1
 
Android presentation
Android presentationAndroid presentation
Android presentation
 
Google Android Security (Basic2Advanced)
Google Android Security (Basic2Advanced)Google Android Security (Basic2Advanced)
Google Android Security (Basic2Advanced)
 
Mobile programming
Mobile programming Mobile programming
Mobile programming
 

Mais de truong le hung

bài giảng phân tích thiết kệ thống thông tin (hutech)
bài giảng phân tích thiết kệ thống thông tin (hutech)bài giảng phân tích thiết kệ thống thông tin (hutech)
bài giảng phân tích thiết kệ thống thông tin (hutech)truong le hung
 
Cach su dung databinding
Cach su dung databindingCach su dung databinding
Cach su dung databindingtruong le hung
 
Thuc thi menh lenh voi co so du lieu
Thuc thi menh lenh voi co so du lieuThuc thi menh lenh voi co so du lieu
Thuc thi menh lenh voi co so du lieutruong le hung
 
Tao mot ket noi toi co so du lieu dung c#
Tao mot ket noi toi co so du lieu dung c#Tao mot ket noi toi co so du lieu dung c#
Tao mot ket noi toi co so du lieu dung c#truong le hung
 
04 chuong 4 - sap xep, tim kiem, loc du lieu
04   chuong 4 - sap xep, tim kiem, loc du lieu04   chuong 4 - sap xep, tim kiem, loc du lieu
04 chuong 4 - sap xep, tim kiem, loc du lieutruong le hung
 
04 chuong 4 - databinding
04   chuong 4 - databinding04   chuong 4 - databinding
04 chuong 4 - databindingtruong le hung
 
04 chuong 4 - cap nhat du lieu
04   chuong 4 - cap nhat du lieu04   chuong 4 - cap nhat du lieu
04 chuong 4 - cap nhat du lieutruong le hung
 
04 chuong 4 - cap nhat du lieu
04   chuong 4 - cap nhat du lieu04   chuong 4 - cap nhat du lieu
04 chuong 4 - cap nhat du lieutruong le hung
 
03 chuong 3 - data sets
03   chuong 3 - data sets03   chuong 3 - data sets
03 chuong 3 - data setstruong le hung
 
02 chuong 2 - lay du lieu theo cach connected
02   chuong 2 - lay du lieu theo cach connected02   chuong 2 - lay du lieu theo cach connected
02 chuong 2 - lay du lieu theo cach connectedtruong le hung
 
01 chuong 1 - gioi thieu ado.net va ket noi den csdl
01   chuong 1 - gioi thieu ado.net va ket noi den csdl01   chuong 1 - gioi thieu ado.net va ket noi den csdl
01 chuong 1 - gioi thieu ado.net va ket noi den csdltruong le hung
 

Mais de truong le hung (12)

Stored procedure
Stored procedureStored procedure
Stored procedure
 
bài giảng phân tích thiết kệ thống thông tin (hutech)
bài giảng phân tích thiết kệ thống thông tin (hutech)bài giảng phân tích thiết kệ thống thông tin (hutech)
bài giảng phân tích thiết kệ thống thông tin (hutech)
 
Cach su dung databinding
Cach su dung databindingCach su dung databinding
Cach su dung databinding
 
Thuc thi menh lenh voi co so du lieu
Thuc thi menh lenh voi co so du lieuThuc thi menh lenh voi co so du lieu
Thuc thi menh lenh voi co so du lieu
 
Tao mot ket noi toi co so du lieu dung c#
Tao mot ket noi toi co so du lieu dung c#Tao mot ket noi toi co so du lieu dung c#
Tao mot ket noi toi co so du lieu dung c#
 
04 chuong 4 - sap xep, tim kiem, loc du lieu
04   chuong 4 - sap xep, tim kiem, loc du lieu04   chuong 4 - sap xep, tim kiem, loc du lieu
04 chuong 4 - sap xep, tim kiem, loc du lieu
 
04 chuong 4 - databinding
04   chuong 4 - databinding04   chuong 4 - databinding
04 chuong 4 - databinding
 
04 chuong 4 - cap nhat du lieu
04   chuong 4 - cap nhat du lieu04   chuong 4 - cap nhat du lieu
04 chuong 4 - cap nhat du lieu
 
04 chuong 4 - cap nhat du lieu
04   chuong 4 - cap nhat du lieu04   chuong 4 - cap nhat du lieu
04 chuong 4 - cap nhat du lieu
 
03 chuong 3 - data sets
03   chuong 3 - data sets03   chuong 3 - data sets
03 chuong 3 - data sets
 
02 chuong 2 - lay du lieu theo cach connected
02   chuong 2 - lay du lieu theo cach connected02   chuong 2 - lay du lieu theo cach connected
02 chuong 2 - lay du lieu theo cach connected
 
01 chuong 1 - gioi thieu ado.net va ket noi den csdl
01   chuong 1 - gioi thieu ado.net va ket noi den csdl01   chuong 1 - gioi thieu ado.net va ket noi den csdl
01 chuong 1 - gioi thieu ado.net va ket noi den csdl
 

Último

cuộc cải cách của Lê Thánh Tông - Sử 11
cuộc cải cách của Lê Thánh Tông -  Sử 11cuộc cải cách của Lê Thánh Tông -  Sử 11
cuộc cải cách của Lê Thánh Tông - Sử 11zedgaming208
 
TỔNG HỢP 30 ĐỀ THI CHỌN HSG CÁC TRƯỜNG THPT CHUYÊN VÙNG DUYÊN HẢI & ĐỒNG BẰNG...
TỔNG HỢP 30 ĐỀ THI CHỌN HSG CÁC TRƯỜNG THPT CHUYÊN VÙNG DUYÊN HẢI & ĐỒNG BẰNG...TỔNG HỢP 30 ĐỀ THI CHỌN HSG CÁC TRƯỜNG THPT CHUYÊN VÙNG DUYÊN HẢI & ĐỒNG BẰNG...
TỔNG HỢP 30 ĐỀ THI CHỌN HSG CÁC TRƯỜNG THPT CHUYÊN VÙNG DUYÊN HẢI & ĐỒNG BẰNG...Nguyen Thanh Tu Collection
 
Luận giải tử vi của 12 con giáp năm 2024 chi tiết và chính xác -...
Luận giải tử vi của 12 con giáp năm 2024 chi tiết và chính xác -...Luận giải tử vi của 12 con giáp năm 2024 chi tiết và chính xác -...
Luận giải tử vi của 12 con giáp năm 2024 chi tiết và chính xác -...Xem Số Mệnh
 
Game-Plants-vs-Zombies để ôn tập môn kinh tế chính trị.pptx
Game-Plants-vs-Zombies để ôn tập môn kinh tế chính trị.pptxGame-Plants-vs-Zombies để ôn tập môn kinh tế chính trị.pptx
Game-Plants-vs-Zombies để ôn tập môn kinh tế chính trị.pptxxaxanhuxaxoi
 
Xem sim phong thủy luận Hung - Cát số điện thoại chính xác nhất.pdf
Xem sim phong thủy luận Hung - Cát số điện thoại chính xác nhất.pdfXem sim phong thủy luận Hung - Cát số điện thoại chính xác nhất.pdf
Xem sim phong thủy luận Hung - Cát số điện thoại chính xác nhất.pdfXem Số Mệnh
 
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...Nguyen Thanh Tu Collection
 
Gieo quẻ kinh dịch, xin xăm,Xin lộc thánh.pdf
Gieo quẻ kinh dịch, xin xăm,Xin lộc thánh.pdfGieo quẻ kinh dịch, xin xăm,Xin lộc thánh.pdf
Gieo quẻ kinh dịch, xin xăm,Xin lộc thánh.pdfXem Số Mệnh
 
chủ nghĩa xã hội khoa học về đề tài cuối kì
chủ nghĩa xã hội khoa học về đề tài cuối kìchủ nghĩa xã hội khoa học về đề tài cuối kì
chủ nghĩa xã hội khoa học về đề tài cuối kìanlqd1402
 
ĐỀ CƯƠNG + TEST ÔN TẬP CUỐI KÌ 2 TIẾNG ANH 11 - GLOBAL SUCCESS (THEO CHUẨN MI...
ĐỀ CƯƠNG + TEST ÔN TẬP CUỐI KÌ 2 TIẾNG ANH 11 - GLOBAL SUCCESS (THEO CHUẨN MI...ĐỀ CƯƠNG + TEST ÔN TẬP CUỐI KÌ 2 TIẾNG ANH 11 - GLOBAL SUCCESS (THEO CHUẨN MI...
ĐỀ CƯƠNG + TEST ÔN TẬP CUỐI KÌ 2 TIẾNG ANH 11 - GLOBAL SUCCESS (THEO CHUẨN MI...Nguyen Thanh Tu Collection
 
đồ án thương mại quốc tế- hutech - KDQTK21
đồ án thương mại quốc tế- hutech - KDQTK21đồ án thương mại quốc tế- hutech - KDQTK21
đồ án thương mại quốc tế- hutech - KDQTK21nguyenthao2003bd
 
ĐẢNG LÃNH ĐẠO HAI CUỘC KHÁNG CHIẾN GIÀNH ĐỘC LẬP HOÀN TOÀN, THỐNG NHẤT ĐẤT NƯ...
ĐẢNG LÃNH ĐẠO HAI CUỘC KHÁNG CHIẾN GIÀNH ĐỘC LẬP HOÀN TOÀN, THỐNG NHẤT ĐẤT NƯ...ĐẢNG LÃNH ĐẠO HAI CUỘC KHÁNG CHIẾN GIÀNH ĐỘC LẬP HOÀN TOÀN, THỐNG NHẤT ĐẤT NƯ...
ĐẢNG LÃNH ĐẠO HAI CUỘC KHÁNG CHIẾN GIÀNH ĐỘC LẬP HOÀN TOÀN, THỐNG NHẤT ĐẤT NƯ...PhcTrn274398
 
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...Nguyen Thanh Tu Collection
 
1第一课:你好.pptx. Chinese lesson 1: Hello.Nỉ hao
1第一课:你好.pptx. Chinese lesson 1: Hello.Nỉ hao1第一课:你好.pptx. Chinese lesson 1: Hello.Nỉ hao
1第一课:你好.pptx. Chinese lesson 1: Hello.Nỉ haoBookoTime
 
50 ĐỀ ĐỀ XUẤT THI VÀO 10 THPT SỞ GIÁO DỤC THANH HÓA MÔN TIẾNG ANH 9 CÓ TỰ LUẬ...
50 ĐỀ ĐỀ XUẤT THI VÀO 10 THPT SỞ GIÁO DỤC THANH HÓA MÔN TIẾNG ANH 9 CÓ TỰ LUẬ...50 ĐỀ ĐỀ XUẤT THI VÀO 10 THPT SỞ GIÁO DỤC THANH HÓA MÔN TIẾNG ANH 9 CÓ TỰ LUẬ...
50 ĐỀ ĐỀ XUẤT THI VÀO 10 THPT SỞ GIÁO DỤC THANH HÓA MÔN TIẾNG ANH 9 CÓ TỰ LUẬ...Nguyen Thanh Tu Collection
 
Xem tử vi miễn phí trực tuyến cho kết quả chính xác cùng luậ...
Xem tử vi miễn phí trực tuyến cho kết quả chính xác cùng luậ...Xem tử vi miễn phí trực tuyến cho kết quả chính xác cùng luậ...
Xem tử vi miễn phí trực tuyến cho kết quả chính xác cùng luậ...Xem Số Mệnh
 
2第二课:汉语不太难.pptx. Chinese lesson 2: Chinese not that hard
2第二课:汉语不太难.pptx. Chinese lesson 2: Chinese not that hard2第二课:汉语不太难.pptx. Chinese lesson 2: Chinese not that hard
2第二课:汉语不太难.pptx. Chinese lesson 2: Chinese not that hardBookoTime
 
Lập lá số tử vi trọn đời có luận giải chi tiết, chính xác n...
Lập lá số tử vi trọn đời có luận giải chi tiết, chính xác n...Lập lá số tử vi trọn đời có luận giải chi tiết, chính xác n...
Lập lá số tử vi trọn đời có luận giải chi tiết, chính xác n...Xem Số Mệnh
 
vat li 10 Chuyen de bai 4 Xac dinh phuong huong.pptx
vat li 10  Chuyen de bai 4 Xac dinh phuong huong.pptxvat li 10  Chuyen de bai 4 Xac dinh phuong huong.pptx
vat li 10 Chuyen de bai 4 Xac dinh phuong huong.pptxlephuongvu2019
 
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA GIỮA HỌC KÌ + CUỐI HỌC KÌ 2 NĂ...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA GIỮA HỌC KÌ + CUỐI HỌC KÌ 2 NĂ...ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA GIỮA HỌC KÌ + CUỐI HỌC KÌ 2 NĂ...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA GIỮA HỌC KÌ + CUỐI HỌC KÌ 2 NĂ...Nguyen Thanh Tu Collection
 
VẬN DỤNG KIẾN THỨC LIÊN MÔN TRONG GIẢI BÀI TẬP ÔN THI THPTQG MÔN SINH HỌC - H...
VẬN DỤNG KIẾN THỨC LIÊN MÔN TRONG GIẢI BÀI TẬP ÔN THI THPTQG MÔN SINH HỌC - H...VẬN DỤNG KIẾN THỨC LIÊN MÔN TRONG GIẢI BÀI TẬP ÔN THI THPTQG MÔN SINH HỌC - H...
VẬN DỤNG KIẾN THỨC LIÊN MÔN TRONG GIẢI BÀI TẬP ÔN THI THPTQG MÔN SINH HỌC - H...Nguyen Thanh Tu Collection
 

Último (20)

cuộc cải cách của Lê Thánh Tông - Sử 11
cuộc cải cách của Lê Thánh Tông -  Sử 11cuộc cải cách của Lê Thánh Tông -  Sử 11
cuộc cải cách của Lê Thánh Tông - Sử 11
 
TỔNG HỢP 30 ĐỀ THI CHỌN HSG CÁC TRƯỜNG THPT CHUYÊN VÙNG DUYÊN HẢI & ĐỒNG BẰNG...
TỔNG HỢP 30 ĐỀ THI CHỌN HSG CÁC TRƯỜNG THPT CHUYÊN VÙNG DUYÊN HẢI & ĐỒNG BẰNG...TỔNG HỢP 30 ĐỀ THI CHỌN HSG CÁC TRƯỜNG THPT CHUYÊN VÙNG DUYÊN HẢI & ĐỒNG BẰNG...
TỔNG HỢP 30 ĐỀ THI CHỌN HSG CÁC TRƯỜNG THPT CHUYÊN VÙNG DUYÊN HẢI & ĐỒNG BẰNG...
 
Luận giải tử vi của 12 con giáp năm 2024 chi tiết và chính xác -...
Luận giải tử vi của 12 con giáp năm 2024 chi tiết và chính xác -...Luận giải tử vi của 12 con giáp năm 2024 chi tiết và chính xác -...
Luận giải tử vi của 12 con giáp năm 2024 chi tiết và chính xác -...
 
Game-Plants-vs-Zombies để ôn tập môn kinh tế chính trị.pptx
Game-Plants-vs-Zombies để ôn tập môn kinh tế chính trị.pptxGame-Plants-vs-Zombies để ôn tập môn kinh tế chính trị.pptx
Game-Plants-vs-Zombies để ôn tập môn kinh tế chính trị.pptx
 
Xem sim phong thủy luận Hung - Cát số điện thoại chính xác nhất.pdf
Xem sim phong thủy luận Hung - Cát số điện thoại chính xác nhất.pdfXem sim phong thủy luận Hung - Cát số điện thoại chính xác nhất.pdf
Xem sim phong thủy luận Hung - Cát số điện thoại chính xác nhất.pdf
 
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
 
Gieo quẻ kinh dịch, xin xăm,Xin lộc thánh.pdf
Gieo quẻ kinh dịch, xin xăm,Xin lộc thánh.pdfGieo quẻ kinh dịch, xin xăm,Xin lộc thánh.pdf
Gieo quẻ kinh dịch, xin xăm,Xin lộc thánh.pdf
 
chủ nghĩa xã hội khoa học về đề tài cuối kì
chủ nghĩa xã hội khoa học về đề tài cuối kìchủ nghĩa xã hội khoa học về đề tài cuối kì
chủ nghĩa xã hội khoa học về đề tài cuối kì
 
ĐỀ CƯƠNG + TEST ÔN TẬP CUỐI KÌ 2 TIẾNG ANH 11 - GLOBAL SUCCESS (THEO CHUẨN MI...
ĐỀ CƯƠNG + TEST ÔN TẬP CUỐI KÌ 2 TIẾNG ANH 11 - GLOBAL SUCCESS (THEO CHUẨN MI...ĐỀ CƯƠNG + TEST ÔN TẬP CUỐI KÌ 2 TIẾNG ANH 11 - GLOBAL SUCCESS (THEO CHUẨN MI...
ĐỀ CƯƠNG + TEST ÔN TẬP CUỐI KÌ 2 TIẾNG ANH 11 - GLOBAL SUCCESS (THEO CHUẨN MI...
 
đồ án thương mại quốc tế- hutech - KDQTK21
đồ án thương mại quốc tế- hutech - KDQTK21đồ án thương mại quốc tế- hutech - KDQTK21
đồ án thương mại quốc tế- hutech - KDQTK21
 
ĐẢNG LÃNH ĐẠO HAI CUỘC KHÁNG CHIẾN GIÀNH ĐỘC LẬP HOÀN TOÀN, THỐNG NHẤT ĐẤT NƯ...
ĐẢNG LÃNH ĐẠO HAI CUỘC KHÁNG CHIẾN GIÀNH ĐỘC LẬP HOÀN TOÀN, THỐNG NHẤT ĐẤT NƯ...ĐẢNG LÃNH ĐẠO HAI CUỘC KHÁNG CHIẾN GIÀNH ĐỘC LẬP HOÀN TOÀN, THỐNG NHẤT ĐẤT NƯ...
ĐẢNG LÃNH ĐẠO HAI CUỘC KHÁNG CHIẾN GIÀNH ĐỘC LẬP HOÀN TOÀN, THỐNG NHẤT ĐẤT NƯ...
 
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
30 ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
 
1第一课:你好.pptx. Chinese lesson 1: Hello.Nỉ hao
1第一课:你好.pptx. Chinese lesson 1: Hello.Nỉ hao1第一课:你好.pptx. Chinese lesson 1: Hello.Nỉ hao
1第一课:你好.pptx. Chinese lesson 1: Hello.Nỉ hao
 
50 ĐỀ ĐỀ XUẤT THI VÀO 10 THPT SỞ GIÁO DỤC THANH HÓA MÔN TIẾNG ANH 9 CÓ TỰ LUẬ...
50 ĐỀ ĐỀ XUẤT THI VÀO 10 THPT SỞ GIÁO DỤC THANH HÓA MÔN TIẾNG ANH 9 CÓ TỰ LUẬ...50 ĐỀ ĐỀ XUẤT THI VÀO 10 THPT SỞ GIÁO DỤC THANH HÓA MÔN TIẾNG ANH 9 CÓ TỰ LUẬ...
50 ĐỀ ĐỀ XUẤT THI VÀO 10 THPT SỞ GIÁO DỤC THANH HÓA MÔN TIẾNG ANH 9 CÓ TỰ LUẬ...
 
Xem tử vi miễn phí trực tuyến cho kết quả chính xác cùng luậ...
Xem tử vi miễn phí trực tuyến cho kết quả chính xác cùng luậ...Xem tử vi miễn phí trực tuyến cho kết quả chính xác cùng luậ...
Xem tử vi miễn phí trực tuyến cho kết quả chính xác cùng luậ...
 
2第二课:汉语不太难.pptx. Chinese lesson 2: Chinese not that hard
2第二课:汉语不太难.pptx. Chinese lesson 2: Chinese not that hard2第二课:汉语不太难.pptx. Chinese lesson 2: Chinese not that hard
2第二课:汉语不太难.pptx. Chinese lesson 2: Chinese not that hard
 
Lập lá số tử vi trọn đời có luận giải chi tiết, chính xác n...
Lập lá số tử vi trọn đời có luận giải chi tiết, chính xác n...Lập lá số tử vi trọn đời có luận giải chi tiết, chính xác n...
Lập lá số tử vi trọn đời có luận giải chi tiết, chính xác n...
 
vat li 10 Chuyen de bai 4 Xac dinh phuong huong.pptx
vat li 10  Chuyen de bai 4 Xac dinh phuong huong.pptxvat li 10  Chuyen de bai 4 Xac dinh phuong huong.pptx
vat li 10 Chuyen de bai 4 Xac dinh phuong huong.pptx
 
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA GIỮA HỌC KÌ + CUỐI HỌC KÌ 2 NĂ...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA GIỮA HỌC KÌ + CUỐI HỌC KÌ 2 NĂ...ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA GIỮA HỌC KÌ + CUỐI HỌC KÌ 2 NĂ...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA GIỮA HỌC KÌ + CUỐI HỌC KÌ 2 NĂ...
 
VẬN DỤNG KIẾN THỨC LIÊN MÔN TRONG GIẢI BÀI TẬP ÔN THI THPTQG MÔN SINH HỌC - H...
VẬN DỤNG KIẾN THỨC LIÊN MÔN TRONG GIẢI BÀI TẬP ÔN THI THPTQG MÔN SINH HỌC - H...VẬN DỤNG KIẾN THỨC LIÊN MÔN TRONG GIẢI BÀI TẬP ÔN THI THPTQG MÔN SINH HỌC - H...
VẬN DỤNG KIẾN THỨC LIÊN MÔN TRONG GIẢI BÀI TẬP ÔN THI THPTQG MÔN SINH HỌC - H...
 

Lập trình android

  • 1. 1 Lập trình Android Nguyen Ha Giang FIT – Hutech nguyenha.giang@yahoo.com 1
  • 2. 2 Tổng quan hệ điều hành Android Đặc điểm Kiến trúc Phiên bản Lập trình trên môi trường Android Môi trường lập trình AVD Quy trình phát triển ứng dụng Android. Ứng dụng cơ bản Android Tìm hiểu Android project Các cách tiếp cận trong lập trình Android. Nội dung
  • 3. 3 Android là hệ điều được thiết kế cho họ hệ thiết bị di động màn hình cảm ứng (touchscreen mobile devives) bao gồm Smartphone Tablet PCs Phablet Hệ điều hành mã nguồn mở Google Android ?
  • 4. 4 Tập phần mềm dành cho thiết bị di động bao gồm Một hệ điều hành Midleware Các ứng dụng cơ sở Sử dụng Linux để cung cấp dịch vụ hệ thống lõi Security Memory management Process management Power management Hardware drivers Google Android ?
  • 5. 5 Application framework enabling reuse and replacement of components. Optimized Java virtual machine (Dalvik virtual machine) Optimized Graphics Processing, supporting 2D, 3D graphics (OpenGL ES 1.0) Integrated open source web browser: Webkit SQLite for structured data storage Android Features #1
  • 6. 6 Multimedia capability, supporting varieties of audio, video and still image format. GSM Telephony Bluetooth, EDGE, 3G, and WiFi support Camera, GPS, compass, accelerometer and other sensors support. Rich development environment, including an enmulator, debugging tools, memory probe tools, log tools and powerful eclipse plugins. Android Features #2
  • 8. 8 Note that Android based on a Linux kernel not a Linux OS. Supplies Security, memory management, process management, Network stack, and driver model. Acts as an abstraction layer between the hardware and the rest of the software stack. Linux Kernel
  • 9. 9 Run in system background Using C/C++ Language Four types of libraries Bionic Libc, system C libraries Function Libraries, supporting multimedia, web browser, SQLite… Native servers Hardware abstraction libraries Libraries
  • 10. 10 System C library, the standard C system library, tuned for embedded Linux-based devices. Media Libraries, support playback and recording of many popular audio/video formats, as well as image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG Surface Manager, manages access to the display subsystem and seamlessly composites 2D and 3D graphic layers from multiple apps. WebKit, a morden web browser engine which powers both the Android browser and an embedded web view. SGL, the underlying 2D graphics engine. 3D Libraries, an implementation based on OpenGL ES 1.0 APIs FreeType, bitmap and vector font rendering SQLite, a powerful and lightweight relational database engine. Core Libraries
  • 11. 11 The core of Android platform Dalvik Virtual Machine Register-based Exectutes files in the Dalvik format Java core Libraries Provides most of the functionality of the Java programming language. Android Runtime
  • 12. 12 The functions of Java core libraries reply on the Dalvik VM and the underlying Linux kernel. Multiple Dalvik VMs may run at the same time Every Android application runs in its own process, with its own instance of the Dalvik virtual machine. The “dx” tool in Android SDK can transform compiled JAVA class into the .dex format. Android Runtime (cont.)
  • 13. 13 Android custom implementation virtual machine Provides app portability and runtime consistency Runs optimized file format (.dex) and Dalvik bytecode Java .class/ jar files converted to .dex at build time Designed for embedded environment Supports multiple virtual machine processes per device Highly CPU-optimized bytecode interpreter Efficiently using runtime memory Core libraries Core APIs for Java language provide a powerful, yet simple and familiar development platform. Dalvik Virtual Machine
  • 14. 14 DVM Google Dalvik executable Only supports a subset of standard Java library JVM Sun Java bytecode Some worries that Java world may be divided into different communities, each has its own Java standard. DVM vs. JVM
  • 15. 15 Vesion code và version name không giống nhau. Mỗi version name (platform level) thì có duy nhất một version code (API level) đi kèm. Android vesioning Platform Codename API Level Android 1.5 Cupcake 3 Android 1.6 Donut 4 Android 2.0 Eclair 5 Android 2.0.1 6 Android 2.1 7 Android 2.2 Froyo 8 Android 2.3 , Android 2.3.2 Gingerbread 9 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, 4.2.x, 4.3 Jelly Bean (July – 2012, July-2013) 16, 17, ??? Android 4.4 KitKat (Sep -2013) ???
  • 16. 16 Nguồn: Android developer, Sept 4, 2013 Version distribution http://developer.android.com/resources/dashboard/platform-versions.html 2.4% 30.7% 0.1% 21.7% 36.6% (4.1.x) 8.5% (4.2.x)
  • 17. 17 Chuẩn bị môi trường trên PC Cài đặt JDK http://www.oracle.com/technetwork/java/javase/downloads/index.h tml Nếu dùng Eclipse để phát triển ứng dụng thì download một phiên bản Eclipse http://www.eclipse.org/downloads/ Download Android SDK Start package http://developer.android.com/sdk/index.html Cài đặt Android SDK và nhớ note lại thư mục để tham chiếu trong bước cài đặt Eclipse. Installling Android SDK
  • 18. 18 Cài đặt ADT plugin cho Eclipse Plugin này gọi là Android Development Tool, hỗ trợ công cụ để phát triển ứng dụng Android Mở rộng Eclipse với chức năng Tạo mới Android project Tạo ứng dụng UI Android Test ứng dụng trên emulator Android SDK Debug ứng dụng dùng Android SDK Tool. Nếu dùng IDE khác thì không cần cài Eclipse + ADT plugin! Installling Android SDK
  • 19. 19 Cài đặt ADT plugin cho Eclipse: Install New Software Installling Android SDK
  • 21. 21 Add Site, có 2 lựa chọn Nếu download file zip chứa ADT rồi thì chọn Archive browse đến file zip (vd: ADT-20.0.3.zip) Nếu chưa download và muốn cài trực tiếp thì nhập URL sau vào location https://dl-ssl.google.com/android/eclipse/ Cuối cùng chọn OK, để thực hiện install ADT Sau khi cài xong ADT thì restart lại Eclipse! Installling Android SDK
  • 22. 22 Configuring the ADT Plugin Bước này cài đặt ADT tham chiếu đến Android SDK mà ở bước trước đã download và install vào thư mục trên đĩa. Chọn Window > Preferences trong Eclipse, trong SDK location, browse đến thư mục Android SDK Installling Android SDK Sau bước này hoàn tất việc tạo môi trường để xây dựng Android app
  • 23. 23 ADT bundle bô i t nhanh, a t ca môi ng n t đê t n UD Android. Bao m Eclipse + ADT Plugin Android SDK Tools Android Platform-công cụ Nền tảng Android mới nhất Hệ thống hình ảnh giả lập a n ng Android i t Installing Android dev by bundle
  • 24. 24 Installing Android dev by bundle http://developer.android.com/sdk/index.html
  • 25. 25 Emulator là thành phần quan trọng để test ứng dụng nhưng không thể thay thế hoàn toàn thiết bị thật! Emulator trên Android được gọi là Android Virtual Devices (AVDs) Android SDK và AVD manager cho phép tạo các AVDs hướng tới bất kỳ phiên bản Android API. AVD cho phép cấu hình: Resolution RAM SD card Skin Hardware khác Android Emulator & AVD
  • 26. 26 Android Emulator: 1.6 Device Device 1.6
  • 27. 27 Android Emulator: 2.2 Device Device 2.2
  • 28. 28 Android Emulator: 2.3.3 Device Device 2.3.3
  • 29. 29 Android Emulator: 3.0 Device Device 3.0
  • 30. 30 Android Emulator: 4.1 Device Device 4.1
  • 31. 31 Sử dụng bàn phím của máy tính Sử dụng pointer máy tính như "finger" Sử dụng kết nối internet của máy tính Các button: Home, Menu, Back, Search… Ctrl + F11: landscape portrait Alt + Enter: full screen mode Emulator basic Hãy bỏ ra ít thời gian để làm quen với Android Emulator!!!
  • 32. 32 Không hỗ trợ việc gọi và nhận thật sự cuộc gọi Giả lập cuộc gọi và tin nhắn qua emulator console Không hỗ trợ USB connection Camera/video (input) Headphone Battery charge level & AC charging state Bluetooth Emulation limitations Test your app on an actual device!
  • 33. 33 Mở chức năng Android SDK và AVD manager trong Eclipse Setting up an Emulator SDK/AVD Manager
  • 34. 34 Mở chức năng Android SDK và AVD manager trong Eclipse Setting up an Emulator
  • 35. 35 Chọn New virtual device Setting up an Emulator Đặt tên Chọn platform Chọn SD card nếu cần Chọn skin HVGA
  • 36. 36 Với project Android đang làm việc, chọn Run (Ctrl +F11), lúc này emulator đã thiết lập sẽ khởi động. Lần đầu tiên thời gian khởi động emulator khá lâu (vài phút ) do phải tạo môi trường virtual Linux system. Running the app in the emulator Port number AVD name
  • 37. 37 Để thuận tiện cho việc coding, build & run thì emulator khởi động một lần trong suốt quá trình làm việc. Ta cứ việc code, build và run, khi đó emulator tự động cập nhật mã lệnh mới, không cần phải restart lại emulator! Running the app: emulator Run Emulator 1 lần cho suốt phiên làm việc!
  • 38. 38 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 Mobile Devices: Advantages
  • 39. 39 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
  • 40. 40 Được xây dựng trên Java và SDK mới Không support một số thư viện Java như Swing & AWT Oracle đang kiện Google vi phạm bản quyền! Java code được biên dịch vào Dalvik byte code (.dex) Được tối ưu cho thiết bị di động (memory, battery…) Dalvik VM chạy những file .dex Android Apps Mã Java được biên dịch sang Dalvik byte code!
  • 41. 41 Producing an Android App Dalvik exe Byte code <xml> <str> .java .class Other .class files javac dx classes.dex AndroidManifest.xml Resources .apk aapt Java code Byte code
  • 42. 42 Trong Eclipse chọn Alt +Shift + N. First Android Application
  • 43. 43 New Android Project Chọn tên project, build target (platform) và package name First Android Application Package name: ký tự đầu tiên phải chữ thường, phải có dấu chấm (2 định danh) Tên project platform Scroll down để thấy phần package name
  • 44. 44 Toàn bộ Adroid project được khởi tạo từ Eclipse First Android Application Chọn Package Explorer để xem cấu trúc của ứng dụng Code Java Chứa resource File cấu hình app
  • 46. 46 src/NguyenhagiangActivity.java Activity của ứng dụng, hiển thị khi ứng dụng chạy res/layout/main.xml Định nghĩa layout & widgets cho activity res/values/strings.xml Khai báo hằng chuỗi dùng trong chương trình gen/R.java (Don’t touch!) File được tạo tự động các ID từ các file *.xml AndroidManifest.xml Khai báo tất cả thành phần của app (tất cả các activity). Tên thư viện cần thiết được dùng trong ứng dụng Xác định những permission mà ứng dụng được cấp. Phiên bản, cấu hình… Important Files
  • 47. 47 Activity là thành phần sẽ hiển thị khi chạy ứng dụng. Do ứng dụng này đơn giản chỉ có 1 activity nên là activity chính của ứng dụng! src/NguyenhagiangActivity.java
  • 48. 48 Khai báo layouts & widgets cho activity res/layout/main.xml
  • 49. 49 Khai báo hằng chuỗi dùng trong app Hỗ trợ Localization res/values-es/values/strings.xml (Spanish) res/values-fr/values/strings.xml (French) … res/values/string.xml Nội dung chuỗiTên truy cập chuỗi
  • 50. 50 File được tự động khởi tạo với các định danh từ main.xml, string.xml và các phần khác. gen/R.java Không được chỉnh sửa!
  • 51. 51 Khai báo tất cả các thành phần của app với system Phiên bản: version code, version name Application: icon, name, label… Tham chiếu đến tên các thư viện mà app có sử dụng Quy định các permission mà ứng dụng được phép. AndroidManifest.xml
  • 52. 52 Thiết lập UI cho một activity Create UI public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // load nội dung của layout từ file XML setContentView(R.layout.main); } } main.xml chứa trong thư mục res/layout
  • 53. 53 Tạo một nội dung trực tiếp từ code không dùng XML Create UI public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android"); setContentView(tv); } }
  • 54. 54 Tạo button đáp ứng sự kiện click Basic Event handler public class HelloWorldActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button btn = new Button(this); btn.setText("Click me!"); setContentView(btn); } }
  • 55. 55 Khai báo listener để xử lý sự kiện click Basic Event handler (2) // thiết lập event click handler btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText( getApplicationContext(), "Hello World", Toast.LENGTH_LONG).show(); } });
  • 56. 56 Three Main Approaches for Android programming
  • 57. 57 Java-based Sử dụng Java để định nghĩa String, layout window, tạo các GUI, gán event handler, giống như lập trình Swing XML-based Sử dụng file XML để định nghĩa String, layout window, tạo các GUI, gán event handler. Phương thức Java chỉ đọc layout từ file XML và truyền vào setContentView. Hybrid Sử dụng file XML định nghĩa String, layout window, và tạo GUI control. Sử dụng Java để gán event handler. Three Main Approaches
  • 58. 58 Java-based approach public class NguyenHaGiangActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String message="Message 1"; LinearLayout window = new LinearLayout(this); TextView txt = new TextView(this); txt.setText(message); Button btn = new Button(this); btn.setText("Button Label"); btn.setOnClickListener(new SomeHandler()); window.addView(txt); window.addView(btn); setContentView(window); }// end onCreate … //(next page) Code Java
  • 59. 59 Java-based approach ... //(previous page) private class SomeHandler implements OnClickListener{ @Override public void onClick(View clickedButton) { // doSomething();... Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_LONG).show(); } // end onClick } // end class SomeHandler } // end class NguyenHaGiangActivity Code Java
  • 60. 60 XML-based approach Java XML public class SomeClass extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void handlerMethod(View clickedButton) { String someName = getResources().getString(R.string.name); doSomethingWith(someName); } } res/values/strings.xml res/layout/main.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="name">…</string> … </resource> <?xml version="1.0" encoding="utf-8"?> <LinearLayout …> <TextView …/> <Button …android:onClick="handlerMethod" /> … </LinearLayout>
  • 61. 61 Java XML Lấy control thông qua IDs Không sử dụng android:onClick để gán handler Hybrid approach public class SomeClass extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button)findViewById(R.id.button_id); b.setOnClickListener(new SomeHandler()); } private class SomeHandler implements OnClickListener { @Override public void onClick(View clickedButton) { doSomething(...); } } }
  • 63. 63 Approach Sử dụng Java để định nghĩa chuỗi, layout window, tạo GUI control, gán trình xử lý sự kiện. Avantages Thân thiện với giới Java desktop developer. Giống cách tiếp cận để xây dựng Swing, SWT, và AWT. Thuận tiện cho việc xây dựng layout động, tuỳ biến vào logic lập trình. Disadvantages Phần code và phần UI xen lẫn với nhau. Chỉ thấy được giao diện khi chạy ứng dụng. Main idea
  • 64. 64 Phần code trong onCreate Code (Main Method) String appName = "Demo Java-baed Application"; String windowText = "Press the button"; String buttonLabel = "Show Greeting"; LinearLayout mainWindow = new LinearLayout(this); mainWindow.setOrientation(LinearLayout.VERTICAL); setTitle(appName); TextView label = new TextView(this); label.setText(windowText); mainWindow.addView(label); Button greetingButton = new Button(this); greetingButton.setText(buttonLabel); greetingButton.setOnClickListener(new Toaster()); mainWindow.addView(greetingButton); setContentView(mainWindow);
  • 65. 65 Lớp thực thi interface onClickListener Code (Main Method) private class Toaster implements OnClickListener { @Override public void onClick(View clickedButton) { String greetingText = "Hello from Android!"; Toast t = Toast.makeText(getApplicationContext(), greetingText, Toast.LENGTH_LONG); t.show(); } }
  • 68. 68 Approach Sử dụng file XML để định nghĩa chuỗi, layout window, tạo UI control và gán event handler Định nghĩa layout và control in res/layout/main.xml Định nghĩa các chuỗi trong res/values/strings.xml Advantages Dễ quản lý Dùng visual layout editor của eclipse Đây là cách tiếp cận được khuyến khích Disaventages Việc tạo layout động. Main Idea
  • 69. 69 res/layout/main.xml Định nghĩa layout và widget với mô tả XML <LinearLayout …> define widget </LinearLayout> Tham chiếu đến chuỗi (strings.xml) với @string/string_name Gán event handler với android:onClick res/value/strings.xml Định nghĩa chuỗi dùng trong GUI Java code Tham chiếu đến layout với R.layout.main Tham chiếu đến chuỗi với getString(R.string.string_name) Tham chiếu đến widget với findViewById(R.id.xyz_id) More details res/layout/main.xml
  • 70. 70 Project layout Tham chiếu đến layout được định nghĩa trong res/layout/main.xml với R.layout.main Định nghĩa giao diện và widget. Đôi khi có phần khai báo event handler. Tham chiếu đến string định nghĩa trong res/values/strings.xml với cú pháp @string/string_name Định nghĩa chuỗi được dùng trong UI
  • 71. 71 Code (res/layout/main.xml) Các thuộc tính orientation, layout_width… được định nghĩa trong JavaDoc API cho LinearLayout Chuỗi được định nghĩa trong res/values/strings.xml Phương thức này là public khai báo trong main class, giá trị trả về là void, có một tham số là View
  • 72. 72 Code (res/values/strings.xml) Tên của ứng dụng Các chuỗi được định nghĩa trong strings.xml được tham chiếu trong main.xml và java code main.xml: @string/string_name Java code: getString(R.string.string_name)
  • 73. 73 Java code Tham chiếu đến main.xml Tham chiếu đến chuỗi tên greeting_text được định nghĩa trong strings.xml
  • 75. 75 Approach Sử dụng XML để định nghĩa string, layout window, và UI widget Sử dụng Java để gán event handler Advantages Tương tự như cách tiếp cận XML-based Ngoài ra việc gán event handler từ code có thể dễ hiểu hơn, do event handler cũng là phần java code. Disadvantages Khó tạo layout động. Main idea
  • 76. 76 Code (res/layout/main.xml) Định nghĩa id cho button để button có thể được tham chiếu trong Java code với findViewById(R.id.greetind_button) Không gán event handler ở đây, do ta sẽ làm trong java code!
  • 77. 77 Code (res/values/strings.xml) File strings.xml tương tự như phần trước!
  • 78. 78 Code (java) Phải thiết lập setContentView trước khi gọi findViewById, nếu không sẽ nhận được null
  • 79. 79 Viết ứng dụng basic calculator cho phép tính các phép toán cơ bản {cộng,trừ, nhân, chia} theo các cách tiếp cận như sau: Java based XML based Hybrid Bài tập
  • 81. 81 LinearLayout Cách thức tổ chức layout nhiều cấp Sử dụng màu sắc RelativeLayout TableLayout Contents
  • 82. 82 XML-based Khai báo layout trong res/layouts/some_layout.xml Thiết lập thuộc tính XML Sử dụng visual editor trong Eclipse Load layout với setContentView(R.layout.some_layout) Java-based Tạo thể hiện layout, thiết lập thuộc tính, chèn sub-layout LinearLayout window = new LinearLayout(this) // thiết lập các thuộc tính cho window … window.addView(widgetOrLayout) Load với phương thức setContentView(window) Layout Strategies
  • 83. 83 Mỗi lớp Layout đều có lớp inner là LayoutParams định nghĩa tham số XML cho layout sử dụng. Các tham số này có tên android:layout_xyz, thường dùng để định kích thước và canh lề. VD: <LinearLayout android:layout_width = "match_parent" android:layout_height= "wrap_content" android:gravity = "center_horizontal" android:background = "@color/color_1"> … </LinearLayout> XML Layout Attributes
  • 84. 84 Size android:layout_width, android:layout_height match_parent: fill the parent space (minus padding) Phiên bản cũ là fill_parent wrap_content: kích thước bao nội dung android:layout_weight: Giá trị số cho biết tỷ lệ phân chia kích thước Alignment android:layout_gravity Cho biết cách thức canh lề của view ở trong containing view android:gravity Cho biết cách thức text và thành phần bên trong view được canh lề. Commonly Used Attributes
  • 85. 85 Alignment (tt) Giá trị thường dùng Top, bottom, left, right, center_vertical, center_horizontal, center, fill_vertical, fill_horizontal, fill, clip_vertical, clip_horizontal. Margin (blank space outside) Android:layout_marginBottom, android:layout_marginTop, android:layout_marginLeft, android:layout_marginRight Units dp: density-independent pixels (scaled by device resol.) sp: scaled pixel (font size) px: pixels, in: inches, mm: milimeters. Commonly Used Attributes
  • 86. 86 Padding (blank space inside) android:paddingBottom/Top/Left/Right Giá trị là con số theo đơn vị unit bên trên ID android:id Sử dụng khi java code cần tham chiếu đến view Sử dụng trong RelativeLayout khi cần tham chiếu đến view nào đó để làm mốc. Colors android:background (color, image) android:textColor Giá trị thường dùng "#rrggbb", "#aarrggbb", "@color/color_name" Commonly Used Attributes
  • 87. 87 Ý nghĩa Cho phép đặt những thành phần theo 1 dòng hay 1 cột Có thể lồng ghép để tạo thành những dòng với các cột. Các thuộc tính XML quan trọng. android:orientation "horizontal" (row) or "vertical" (column) Horizontal là mặc định android:gravity Cách các view bên trong được canh lề Basic LinearLayout
  • 89. 89 Example (nested layout) Horizontal Linearlayout với gravity là left Horizontal linearlayout với gravity là center_horizontal
  • 90. 90 Example (nested layout) Horizontal LinearLayout Chứa 2 layout Horizontal LinearLayout Layout đầu tiên màu vàng có layout_width ="wrap_content" và gravity ="left" Layout thứ hai màu xanh có layout_width="match_parent"và gravity là "right"
  • 91. 91 Example (nested layout) Horizontal LinearLayout Chứa 3 vertical bên trong 1. LinearLayout với orientation là vertical và 4 button bên trong 2. Chứa RadioGroup với orientation là vertical và 4 RadioButton bên trong. 3. LinearLayout với orientation là vertical và 4 LinearLayout bên trong. 2 cột đầu có layout_width là wrap_content và cột thứ 3 là match_parent.
  • 92. 92 Example (nested layout) Vertical LinearLayout Layout này chứa 4 horizontal bên trong. 1. Gravity là center_horizontal 2. Gravity là left 3. Gravity là right 4. Chứa 2 horizontal LinearLayout bên trong 1. Layout_width là wrap_content và gravity là left 2. Layout_width là match_parent và gravity là right
  • 94. 94 Mặc dù màu có thể định nghĩa cụ thể trong file layout (background="#ff0000"), tuy nhiên có thể định nghĩa tên màu trong file riêng biệt, khi đó tham chiếu thông qua tên màu. Nếu cần thì chỉ thay đổi giá trị của màu, lúc đó sẽ tác động lên toàn bộ nơi tham chiếu đến tên màu. Syntax Convention Sử dụng file res/values/colors.xml Có thể đặt với bất cứ tên nào miễn hợp lệ! Setting Colors <resources> <color name="color_name_1">#rrggbb</color> … <!-- Other colors --> </resources>
  • 95. 95 Color File <resources> <color name="red">#f00</color> <color name="orange">#ffa500</color> <color name="yellow">#ffff00</color> <color name="green">#0f0</color> <color name="blue">#00f</color> <color name="indigo">#4b0082</color> <color name="violet">#ee82ee</color> <color name="back">#000</color> <color name="white">#fff</color> </resources> colors.xml
  • 96. 96 Tham chiếu màu trong file layout. Tham chiếu trong code getResources().getColor(R.color.red) Layout file <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView1" android:text="@string/red" android:layout_marginTop="20px" android:layout_centerHorizontal="true" android:padding="25dp“ android:background="@color/red"> </TextView>
  • 97. 97 Ý nghĩa Gán những con số cho android:layout_weight, kích thước sẽ được phân chia theo tỷ số này. Cách dùng (cho height) Thiết lập android:layout_height = 0dp Sử dụng những giá trị tương ứng cho layout_weight Vd: nếu có 3 thành phần với layout_weight tương ứng là 1, 1 và 2 thì tỷ lệ tương ứng là 25%, 25% và 50% của 3 thành phần so với chiều cao của cha. Tương tự cho thiết lập width! Có thể xem giá trị weight là 100, sử dụng những giá trị 25, 25, và 50 cũng tương ứng như 1, 1 và 2! Layout Weight
  • 100. 100 Ý tưởng Mỗi view có id xác định Cho phép xác định vị trí tương đối của view với những view khác thông qua id. Thuộc tính XML quan trọng Align với container layout_alignParentBottom (Top,Right,Left) layout_centerInParent (centerHorizontal, centerVertical) Tất cả giá trị là true hay false Align với view layout_alignBottom (Top, Right, Left) layout_toLeftOf ( toRightOf, above, below) Tất cả giá trị là id của view nào đó RelativeLayout
  • 101. 101 Button thứ 1 <Button id = "@+id/button_1" android:layout_alignParentRight="true"… /> Button thứ 2 <Button android:layout_toLeftOf="@id/button_1"… /> Referring to Existing IDs @+id: để gán id mới @id: (không có dấu cộng) tham chiếu đến button đã có
  • 105. 105 A layout that lets you specify exact location (x/y coordinates) of its children. Abs layouts are less flexible and harder to maintain than other types of layouts without abs pos. => So not recommended for most UI development! AbsoluteLayout (0,0) (20,30) View android:layout_x="20px" android:layout_y=“30px" screen
  • 106. 106 Example AbsoluteLayout - example <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="20dp" android:layout_y="15dp" android:width="220px" android:height=“50px" android:text="Back" /> </AbsoluteLayout> Tọa độ của view kích thước của view
  • 107. 107 FL is designed to display a single item at a time. You can have multiple elements within a FrameLayout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping. FL can become more useful when elements are hidden and displayed programmatically. You can use the attribute android:visibility in the XML to hide specific elements. After that can setVisibility from code to show. FrameLayout
  • 109. 109 Idea Cho phép widget và layout khác chứa bên trong grid (không có đường biên) Tương tự như HTML table, số dòng và cột xác định tự động, không cần phải khai báo. Các view được đặt bên trong TableRow XML attributes android:stretchColumns Chứa chỉ số cột sẽ được stretch khi table nhỏ hơn parent android:shrinkColumns Liệt kê các cột sẽ co lại khi table lớn hơn parent android:collapseColumns Những cột sẽ bị thu lại, có thể lập trình để hiện ra sau. TableLayout
  • 110. 110 Dùng để định nghĩa một dòng trong TableLayout Thuộc tính XML quan trọng sử dụng bên trong TableRow android:layout_column Có thể xác định chính xác cột của thành phần, cột bỏ qua đó sẽ rỗng. android:layout_span Số cột mà thành phần sẽ chiếm giữ, giống colspan trong HTML table Không có thuộc tính tương ứng với rowspan trong HTML, dùng nested table để thực hiện. TableRow
  • 115. 115 Tạo activity có layout như sau: (chỉ dùng TableLayout) VD
  • 117. 117 Buttons ImageButtons với 1 ảnh ImageButtons với 3 ảnh (normal/focused/pressed) RadioButtons – OnClickListener RadioButtons – OnCheckedChangeListener CheckBoxes ToggleButtons Topics in this section
  • 118. 118 Button layout Horizontal LinearLayout (3 button) Horizontal LinearLayout (3 Imagebutton) Horizontal LinearLayout (3 Imagebutton) Horizontal RadioGroup (3 RadioButton) Horizontal RadioGroup (3 RadioButton) Horizontal LinearLayout (3 CheckBox) Horizontal RadioGroup (3 ToggleButton) Vertical LinearLayout
  • 119. 119 Là nút nhấn có nhãn bên trên Listener: View.OnClickListener Nếu sử dụng phương pháp XML based thì có thể không cần phải khai báo lớp Listener này. Thuộc tính XML thường dùng android:text Nhãn của button, có thể truy xuất bằng code java thông qua phương thức setText và getText android:onClick Basic Button
  • 120. 120 Là nút nhấn có ảnh hiển thị bên trên Listener: View.OnClickListener Thuộc tính XML android:src ảnh hiển thị trên button, tham chiếu đến tên (không có phần mở rộng) của ảnh trong thư mục res/drawable Hỗ trợ các kiểu ảnh png, jpeg, gif và bmp. Có thể dùng Java code để thiết lập với setImageDrawable android:onClick ImageButton
  • 122. 122 Là nút chọn, cho phép chọn một trong số mục chọn cùng nhóm. Listener View.OnClickListener Gán cho mỗi nút nếu quan tâm đến sự kiện chọn trên từng item. Có thể dùng cách khác là bắt sự kiện trên RadioGroup. Thuộc tính XML android:text, android:onClick RadioButton
  • 123. 123 Tương tự như LinearLayout, cho phép chứa các RadioButton bên trong. Quản lý trạng thái RadioButton là exclusive. Listener: OnCheckedChangeListener Thuộc tính XML Tương tự như LinearLayout Sử dụng android:id để tham chiếu trong chương trình (khi thiết lập OnCheckedChangeListener) Không có thuộc tính andoird:onXYZ để thiết lập RadioGroup listener trong XML. RadioGroup
  • 124. 124 Minh hoạ Đáp ứng với mỗi click trên từng radio button bằng cách hiện thông điệp message nào đó Cách thực hiện Đặt các radiobutton vào trong radiogroup Gán event handler, sử dụng onClick cho mỗi RadioButton. Không cần id cho RadioGroup, không cần listener cho RadioGroup. Event handler cho RadioButton
  • 125. 125 Event handler cho RadioButton
  • 126. 126 Minh hoạ Mỗi khi nút nào được nhấn thì hiển thị message báo tên nút được nhấn Cách thực hiện Đặt RadioButton bên trong RadioGroup Trong XML, tạo id cho RadioGroup Trong Java, xác định RadioGroup và thiết lập Listener Event handler cho RadioGroup
  • 127. 127 Xử lý trong hàm onCreate Event handler cho RadioGroup
  • 128. 128 Minh hoạ cách dùng CheckBox
  • 129. 129 Là dạng tương tự như combo box Listener AdapterView.OnItemSelectedListener AdapterView.OnItemClickedListener Thuộc tính XML android:id, android:prompt android:entry Định nghĩa mục chọn, có thể định nghĩa trong strings.xml hoặc arrays.xml Spinner
  • 131. 131 Tham chiếu đến Spinner Lớp thực thi Listener Spinner Lấy item được chọn
  • 132. 132 Cách tiếp cận khác là tạo spinner thông qua code Java. Sinh viên tìm hiểu thêm cách tiếp cận này! Spinner
  • 134. 134 Có thể dùng Intent để gọi một activity từ một activity đang hiển thị. Cú pháp Dùng Intent để gọi Activity Intent activityIntent = new Intent(this, NewActivity.class); startActivity(activityIntent); … // khai báo trong AndroidManifest.xml <activity android:name = "NewActivity" <intent-filter> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
  • 135. 135 Cách thực hiện Tạo đối tượng Bundle, chứa dữ liệu cần gởi cho activity mới, attach đối tượng Bundle này kèm theo Intent Cú pháp Java (original Activity) Java (new Activity) Gởi dữ liệu giữa Activity Intent activityIntent = new Intent(this, NewActivity.class); Bundle newActivityInfo = new Bundle(); newActivityInfo.putXYZ(…); //putDouble, putString… activityIntent.putExtras(newActivityInfo); startActivity(activityIntent); Intent intent = getIntent(); Bundle info = intent.getExtras(); If (info != null) { // getXYZ()… }
  • 136. 136 VD: Truyền dữ liệu từ original activity sang new activity. Gởi dữ liệu giữa Activity
  • 137. 137 Hàm xử lý submit Gởi dữ liệu giữa Activity public void submit(View v) { Intent newActivity = new Intent(this,NewActivity.class); Bundle info = new Bundle(); String sName, sEmail; EditText ed1 = (EditText)findViewById(R.id.editText1); EditText ed2 = (EditText)findViewById(R.id.editText2); info.putString("Name", ed1.getText().toString()); info.putString("Email", ed2.getText().toString() ); newActivity.putExtras(info); startActivity(newActivity); }
  • 138. 138 Xử lý trong new activity Gởi dữ liệu giữa Activity setContentView(R.layout.new_activity); // tham chiếu đến intent String sName, sEmail; Intent intent = getIntent(); Bundle info = intent.getExtras(); if (info != null) { sName = info.getString("Name"); sEmail = info.getString("Email"); TextView tv1 = (TextView)findViewById(R.id.tvHoTen); tv1.setText(sName); TextView tv2 = (TextView)findViewById(R.id.tvEmail); tv2.setText(sEmail); }
  • 139. 139 Tình huống: Activity mới sau khi tương tác với user, thu thập được dữ liệu, kết thúc activity và gởi lại cho activity gọi nó (original activity). Gởi dữ liệu giữa Activity Activity A Activity B Gọi activity B (có thể truyền dữ liệu cho B tuỳ ý) Acvitity B kết thúc và gởi dữ liệu về cho A
  • 140. 140 Phần xử lý ở lớp activity A (Original Activity) Phần xử lý trên thường đặt trong hàm xử lý sự kiện của button hay menu. Gởi dữ liệu giữa Activity Intent newActivity = new Intent(this, ActivityB.class); final int result = 1; // gọi activity B hiển thị và chờ lấy kết quả trả về startActivityForResult(newActivity, result);
  • 141. 141 Phần xử lý ở lớp activity A (Original Activity), khi nhận kết quả trả về từ activity B (new Activity). Override phương thức onActivityResult trong ActivityA để nhận kết quả trả về (khi ActivityB đóng). Gởi dữ liệu giữa Activity protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // lấy Bundle chứa dữ liệu Bundle bundle = data.getExtras(); int data1 = bundle.getInt("<tên dữ liệu 1>"); int data2 = bundle.getInt("<tên dữ liệu 2>"); String data3 = bundle.getString("<tên dữ liệu 3>"); … }
  • 142. 142 Phần xử lý trong activity B (activity được gọi từ A) Gởi dữ liệu giữa Activity // gởi dữ liệu về activity trước Intent intent = new Intent(); Bundle bundle = new Bundle(); // gởi dữ liệu vào bundle bundle.putInt("<tên dữ liệu 1>", data1); bundle.putInt("<tên dữ liệu 2>", data2); Bundle.putString("<tên dữ liệu 3>", data3); intent.putExtras(bundle); // gởi kèm dữ liệu setResult(RESULT_OK, intent); // gởi kết quả về finish(); // đóng activity
  • 143. 143 Cung cấp URI tham chiếu đến new activity New Activity sẽ đăng ký trong androidmanifest có mô tả dùng URI Cú pháp Java (original Activity) XML (phần khai báo cho activity được gọi) Gọi Activity thông qua URI Uri uri = Uri.parse("call://ex.intent.uri/ActivityC"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="call" android:host="ex.intent.uri"/> </intent-filter>
  • 144. 144 Ý tưởng Nhúng tham số trong URI, tương tự như query string trong Web. Cú pháp Java (original Activity) Java (new Activity) Gởi dữ liệu qua tham số URI Uri uri = Uri.parse("call://ex.intent.uri/ActivityC?str=hello class"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); Uri uri = getIntent().getData(); String str = uri.getQueryParameter("str"); // xử lý với chuỗi, nếu truyền là chuỗi số thì chuyển sang số …
  • 145. 145 Extras Pros Can send data of different types No parsing required in Activity that receives the data Cons More complex for originating activity Requires parsing in originating activity if values come from EditText URI parameters Pros: Simpler for originating Activity, especially if EditText used More consistent with URI usage Cons Can send Strings only Requires parsing in receiving Activity Extras vs. URI parameter
  • 146. 146 Ý tưởng Tạo tab window, mỗi tab là một activity Có thể gọi activity thông qua class hay URI Gởi dữ liệu thông qua extras Bundle hay URI Tab Window Activity và các activity phải cùng project. Cú pháp Java Mở rộng từ lớp TabActivity, sử dụng TabHost, TabSpec XML: (androidManifest.xml) Tương tự như phần trước. Activity với Tabbed Window
  • 148. 148 Tạo ứng dụng demo như sau: Activity với Tabbed Window
  • 149. 149 Activity với Tabbed Window Resources resource = getResources(); TabHost host = getTabHost(); Intent intent1 = new Intent(this, activity1.class); Drawable tabIcon1 = resource.getDrawable(R.drawable.b1); TabSpec tab1 = host.newTabSpec("Tab One").setIndicator("Activity 1",tabIcon1) .setContent(intent1); host.addTab(tab1); Intent intent2 = new Intent(this, activity2.class); Drawable tabIcon2 = resource.getDrawable(R.drawable.b2); TabSpec tab2 = host.newTabSpec("Tab Two").setIndicator("Activity 2",tabIcon2) .setContent(intent2); host.addTab(tab2); Intent intent3 = new Intent(this, activity3.class); Drawable tabIcon3 = resource.getDrawable(R.drawable.b3); TabSpec tab3 = host.newTabSpec("Tab Three").setIndicator("Activity 3",tabIcon3) .setContent(intent3); host.addTab(tab3);
  • 150. 150 Service là thành phần nền tảng của Android. Đôi khi ứng dụng run process với khoảng thời gian lâu và không có hoặc hiếm khi có sự can thiệp từ người dùng. Tiến trình nền này có thể xử lý ngay cả khi phone được sử dụng cho activity/task khác. Các lớp thực thi service phải mở rộng từ lớp Service trong gói android.app.Service Các phương thức override là: onCreate, onDestroy, onStart Service
  • 151. 151 Minh hoạ: Tạo một service thực thi việc chạy file nhạc nền. Service này được gọi thông qua UI trên Activity. Service Sử dụng ToggleButton để on/off service
  • 152. 152 Bước 1: Chuẩn bị file nhạc mp3. Tạo thư mục raw trong res (nếu chưa có raw) Import một file nhạc mp3 nào đó vào thư mục res/raw Service Ví dụ import file only_time.mp3 vào thư mục raw
  • 153. 153 Bước 2: Tạo layout cho activity chính của ứng dụng Service <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" > <TextView android:text="Services Demo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="35dp" > </TextView> <ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doService" > </ToggleButton> </LinearLayout>
  • 154. 154 Bước 3: Tạo một lớp mới extend từ Service Service Tên lớp service sẽ tạo Extend từ lớp Service
  • 155. 155 Bước 4: Override các phương thức onCreate, onDestroy, onStart của service. Tạo đối tượng player là MediaPlayer Service public class MyService extends Service { private static final String TAG ="HaGService"; private MediaPlayer player; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub } @Override public void onDestroy() { // TODO Auto-generated method stub } @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub } }
  • 156. 156 Bước 5: viết các phần xử lý cho onCreate Hiển thị một messge báo "Service created" Hiển thị thông tin debug trong LogCat Load file nhạc vào biến player Thiết lập cờ looping. Service @Override public void onCreate() { // TODO Auto-generated method stub Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show(); Log.d(TAG,"onCreate"); player = MediaPlayer.create(this, R.raw.only_time); player.setLooping(false); }
  • 157. 157 Bước 6: thực thi hàm onStart và onDestroy Service @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); player.start(); // chạy file nhạc } @Override public void onDestroy() { // TODO Auto-generated method stub Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show(); Log.d(TAG,"onDestroy"); player.stop(); // dừng file nhạc }
  • 158. 158 Bước 7: viết phần xử lý cho sự kiện onClick của ToggleButton trong activity chính của ứng dụng Service public void doService(View view) { // tham chiếu đến ToggleButton ToggleButton btn = (ToggleButton)view; if (btn.isChecked()==true) { startService( new Intent(this,MyService.class) ); } else { stopService( new Intent(this,MyService.class) ); } }
  • 159. 159 Bước 8: khai báo service trong AndroidManifest.xml Việc khai báo service trong file AndroidManifest là bắt buộc. Nếu thiếu phần khai báo thì service sẽ không chạy được. Service <service android:name="MyService" android:enabled="true"></service> Tên của service Thiết lập trạng thái của Service là anable
  • 160. 160 Broadcast Receivers Thành phần có thể respond lại các thông báo của hệ thống. VD: Thông báo màn hình bị tắt, pin yếu, ảnh được chụp Thông báo cuộc gọi đến, tin nhắn đến… Ngoài ra ứng dụng cũng có thể khởi tạo broadcast để báo cho ứng dụng khác hay hệ thống biết thông tin gì đó Ứng dụng báo đã hoàn thành việc download về thiết bị và sẵn sàng cho việc sử dụng. Có thể dùng để start một service chạy nền nào đó. Phần tìm hiểu thêm
  • 161. 161 Content providers Quản lý sự chia sẻ dữ liệu của ứng dụng. Bao gồm các dạng Shared preferences Internal storage Local cache External storage SQLite database (Chapter 12: Android App Dev for Dummies) Network connection Phần tìm hiểu thêm

Notas do Editor

  1. 3 cách cơ bản trong việc xây dựng ứng dụng Android
  2. Density independent pixelsAn 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. Note: The compiler accepts both “dip” and “dp”, though “dp” is more consistent with “sp”.Scale-independent PixelsThis 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.