Nội dung Bài 1: Tổng quan về lập trình mobile Bài 2: Giới thiệu về lập trình Android Bài 3: Các thành phần cơ bản trong lập trình Android Bài 4: User Interface Bài 5: Tài nguyên Bài 6:
Trang 1GV: Lê Thanh Trọng Email: tronglt@uit.edu.vn
Trang 3Nội dung
Bài 1: Tổng quan về lập trình mobile
Bài 2: Giới thiệu về lập trình Android
Bài 3: Các thành phần cơ bản trong lập trình Android
Bài 4: User Interface
Bài 5: Tài nguyên
Bài 6: Intent
Bài 7: Lập trình với CSDL
Trang 5Tài liệu tham khảo
Link: http://tutorialspoint.com/
https://www.tutorialspoint.com/android/inde
x.htm
Trang 12Các thiết bị di động
Phân loại
Điện thoại di động cơ bản (basic phone)
Điện thoại di động thông minh (smartphone)
Máy tính bảng (tablet)
Lai máy tính bảng và smartphone (phablet)
Trang 13Điện thoại cơ bản
Tính năng
Nghe, gọi, nhắn tin
Quản lý danh bạ, thời gian, báo thức,…
Game (nhẹ, đơn giản)
Trang 15Tablet
Trang 16Phablet
smartphone (6-9 inch)
Trang 18Android
Miễn phí
Mã nguồn mở, dựa trên Linux
Phát triển bởi Google
Dành phát triển smartphone và máy tính bảng
Có hỗ trợ thiết bị khác: ôtô, smartwatch,
tivi,…
Ra mắt vào năm 2007 (cùng tuyên bố liên
minh thiết bị cầm tay mở - Open Handset
Alliance)
Điện thoại android đầu tiên: 2008 - T-Mobile G1
Trang 21iOS
Trang 23Windows phone
Phát triển bởi Microsoft (2010)
Phiên bản: 8.1
Trang 24Lập trình trên thiết bị di động
Trang 26 Nhu cầu sử dụng điện thoại di động đã trở nên phổ biến trên toàn thế giới
Sự tiến bộ vượt bậc của công nghệ đã thay đổi hành vi
của con người
Điện thoại di động là lĩnh vực công nghệ phát triển
mạnh mẽ nhất!
sẽ vượt qua số người truy cập Internet qua PC
Tại sao phải lập trình trên thiết bị di động?
Điện thoại di động sẽ trở thành phương tiện giao tiếp và là công cụ làm
việc chủ yếu của con người
Trang 27 Một bảng thống kê từ IDC vào 6/2011:
2005: có 50tr người dùng SP, 200tr người
dùng NetBook PC
2010: có 250tr người dùng SP! NetBook PC là 350tr
2013: dự báo các dòng desktop PC và
NetBook PC sẽ bị SP vượt qua mặt với số
lượng 650tr người dùng!
Tại sao phải lập trình trên thiết bị di động?
Thị trường thiết bị số & công nghệ lập trình trên thiết bị di động đang
là mảnh đất màu mỡ để khai thác!
Trang 28 Do sự phát triển vượt bậc của thiết bị di động
nhiều công ty PM trên máy tính chuyển
hướng sang PM trên di động
rất nhiều công ty dự định tham gia trong
lĩnh vực này!
Tại sao phải lập trình trên thiết bị di động?
Cần rất nhiều nhân lực cho việc phát triển ứng dụng trên
thiết bị di động
Trang 29 Thị phần smartphone Q1-2013
Nền tảng nào đang phổ biến nhất?
Trang 30 Kể từ khi IPhone ra phiên bản đầu tiên 2007, cho đến nay đã là phiên bản 4 Tạo nên cơn sốt SP trên toàn cầu VN cũng ko ngoại lệ!
Sự thành công của dòng máy iPod và thương hiệu Apple chính là nền tảng để IPhone trở
thành SP mà ai cũng mong muốn!
IPhone với thiết kế đẹp mắt, nhỏ gọn, giao
diện dễ sử dụng và đặc biệt là màn hình cảm ứng mượt mà…
lý do để giới trẻ lựa chọn
Thị trường smartphone Việt Nam
Trang 31 Android xuất hiện chậm hơn (2008-2009)
Không ồn ào, đình đám như IPhone, nhưng SP dùng
Android lại hùng hậu về số lượng và kiểu dáng
Do nhiều hãng sản xuất SP tham gia hỗ trợ Android: HTC,
Samsung, LG, Sony Ericsson
Trong khi IPhone là sản phẩm của Apple, với thiết kế đơn nhất
Nguyên nhân thành công của Android ở VN
Số lượng người dùng Google đông
Tích hợp các dịch vụ Google trong nền tảng này giúp user khai thác
và sử dụng thuận tiện hơn
Giới công nghệ đặc biệt ấn tượng với kho ứng dụng khổng lồ của Android (tương tự như AppStore)
Thị trường smartphone Việt Nam
Trang 32 Mặc dù Android có nhiều mẫu mã và giá tiền
rẻ hơn hẳn so với IPhone, tuy nhiên, IPhone lại
"ăn điểm" hơn Android về thương hiệu & kiểu dáng!
Thị trường smartphone Việt Nam
Trang 35Why Hardware?
Trang 37History
Trang 38 45% of American adults own smartphones
In the age range of 18-29, the number is 66%
64% of Americans now own a smartphone, up from
58% in early 2014
Trang 39Architecture - ARM
with ARM
iPhone 6 and iPhone 6 Plus, which were introduced
on September 9, 2014
Trang 40Architecture - ARM
Trang 41Breakdown of IPhone 6
Trang 42Architecture
Integrate a (GPU) PowerVR
GX6450
1 GB RAM DDR3
Trang 43Architecture
Management IC
Trang 44Architecture
Trang 45Architecture
Trang 46Touch screen
• Type:
1- Resistive Touch Screen
2- Capacitive Touch Screen
• Resolution and size
Trang 47Resistive
1 Composed of two flexible sheets coated with
a resistive material
2 When contact is made to the surface of the
touchscreen, the two sheets are pressed
together
3 Two sheets there are horizontal and vertical
lines that when pushed together, register the precise location
Trang 48Resistive (cont )
Pros:
The screens are usually inexpensive
It can be easily operated with any pointing devices
They are much more accurate than capacitive touch
screens
They are easy to use and are more reliable
Cons:
Multi touch support not available
These are highly sensitive to scratches
Poor visibility in sunlight
Trang 49Capacitive
Consists of an insulator such as glass, coated
with a transparent conductor such as indium tin oxide
Human body is also an electrical conductor
Touching the surface of the screen results in a
distortion of the screen's electrostatic field
Trang 50Capacitive(cont )
Pros:
Cons:
capacitive effect
Trang 51Resolution and Size
• The size of the screen which is measured
diagonally across the face screen
• Measured by inches
Trang 52Resolution
• It is the number of distinct pixels in each
dimension that can be displayed
• Represented by the physical number of
columns and rows of pixels creating the
display (e.g., 240 x 320)
• More Pixels == More Realistic screen
• DPI The number of 'dots' or pixels per each
inch
Trang 53Resolution and Size
• More pixels on same size will give brighter
image quality (more clear)
• Eye can detect up to 300 DPI
• As a programmer you concern about
Resolution NOT Screen Size
Trang 54Microprocessor
• Mobile microprocessors must be small and
consume little power
• Size can be solved with System-on-a-Chip
configurations
• Sleep mode during longer pauses (requires
support from the OS and possibly from the
applications)
• Uses ARM structure
• System-on-a-Chip (CPU,GPU,ROM, RAM, Radio,
External interfaces)
Trang 55SoC (NVIDIA Tegra 4)
• CPU: 1–4 per cluster, 1–2 clusters per physical
chip, up to 1.9ghz
• Cache:4 MB
• Memory: Up to 1GB,(DDR3-667)
• GPU : 8 cores
• Supports (12 MP Camera, Display 1680x1050)
• USB 2.0 OTG, USB 2.0 ULPI, HDMI, MIPI
CSI/DSI/HSI, UART, SPI, SDIO, I2C, I2S
• Examples (Microsoft Surface 2, Vizio 10")
Trang 56SoC (Apple A8)
Trang 57Ram, Rom and memory card
• ROM: where the OS (Firm Ware)of mobile
installed, it can be modified (system update) Ranges from (512MB to 1.5GB)
• RAM: read/write from/to any address, fast,
looses data when powered off
Ranges from (128MB to 1GB)
important for Apps programmers
Trang 58Ram, Rom and memory card
• Storage uses Flash Memory to store data
permanently as HDD in computer
• Divide into two types
1 On chip flash memory as (N8 16GB and
Trang 59• Used for auto turn-off
• May used in further application
Trang 60Accelerometer
• Is an instrument for measuring acceleration,
detecting and measuring vibrations
• Sense movement in only one direction, so true 3D position sensing must use three
• Used in all smart phone and devices like Wii
Remote
• First introduced by Apple in iPhone
Trang 61Gyroscope
• A gyroscope is a device for measuring or
maintaining orientation
• Three degrees of rotational freedom
• Introduced by Apple in iPhone4 2010
Trang 64
Giới thiệu Android OS
1 tỉ thiết bị, khắp gần 200 quốc gia)
Trang 65Giới thiệu
Trang 67Kiến trúc
Trang 68Linux kernel
3.x
(camera, keypad, display)
loại thiết bị khác nhau
Trang 69Libraries
SSL responsible for Internet security
Trang 72Applications
Viết
Cài đặt
Trang 73Đặc điểm
Beautiful UI: beautiful and intuitive
Connectivity: GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, Bluetooth, Wi-Fi, LTE, NFC and WiMAX
Storage: SQLite
Media support: H.263, H.264, MPEG-4 SP, AMR, AMR-WB, AAC, HE-AAC, AAC 5.1, MP3, MIDI, Ogg Vorbis, WAV, JPEG, PNG, GIF, and BMP
Messaging: SMS and MMS
Web browser: WebKit layout engine, Chrome's JavaScript engine (HTML5, CSS3)
Trang 75tiến hướng đến UI, tính năng, hiệu năng
Trang 76Phiên bản
Trang 77Ứng dụng Android
Trang 79Plug-in (optional)
Trang 801 Android 4.1, 4.1.1 16 JELLY_BEAN
Android 4.0.3, 4.0.4 15 ICE_CREAM_SAN
Trang 81Android API Platform Version API
Trang 82Environment Setup
Từ Microsoft Windows XP
Từ Mac OS X 10.5.8 (Intel chip)
Linux theo GNU C Library 2.7 trở đi
Trang 84Bài 2: Các thành phần ứng dụng Android
Trang 85 Activities: điều khiển UI
Services: background processing + application
Broadcast Receivers: quản lý giao tiếp giữa Android
OS và applications
Content Providers: quản lý dữ liệu
Khác: Views, Layouts, Intents, Resources, Manifest,…
Trang 86Activities
Single screen with a user interface
shows a list of new emails
Trang 88Broadcast Receivers
Tương tác với các message từ ứng dụng/hệ thống
Ex: Thông báo cho ứng dụng khác biết dữ liệu đã
download và sẵn sàng cho việc sử dụng
Kế thừa BroadcastReceiver
Thông điệp là một đối tượng Intent
public class MyReceiver extends BroadcastReceiver
{
public void onReceive ( context , intent ){}
}
Trang 90Hello World Example
File -> New -> Project -> Android New
Application
Trang 91Workspace
Trang 92Explorer
Trang 93Explorer
R file do trình biên dịch tạo ra
Tham chiếu đến các resource của project
Chứa apk (ADT build)
Trang 94MainActivity.java
Converted to a Dalvik executable
package com example helloworld ;
import android os Bundle ;
import android app Activity ;
import android view Menu ;
import android view MenuItem ;
import android support v4 app NavUtils ;
public class MainActivity extends Activity {
@Override public void onCreate ( Bundle savedInstanceState ) {
super onCreate ( savedInstanceState );
setContentView ( R layout activity_main );
}
@Override public boolean onCreateOptionsMenu ( Menu menu ) { getMenuInflater () inflate ( R menu activity_main , menu );
Trang 96The Manifest File
Trang 97The Manifest File
Trang 98Strings File
strings.xml thuộc res/values
Chứa các chuỗi liên quan ứng dụng: tên button, label, default text,
Ex: file strings.xml mặc định
<resources>
<string name = "app_name" > HelloWorld </string>
<string name = "hello_world" > Hello world! </string>
<string name = "menu_settings" > Settings </string>
<string n ame = "title_activity_main" > MainActivity </string>
</resources>
Trang 99package com example helloworld ;
public final class R {
public static final class attr { }
public static final class dimen {
public static final int padding_large = 0x7f040002 ;
public static final int padding_medium = 0x7f040001 ;
public static final int padding_small = 0x7f040000 ;
}
public static final class drawable {
public static final int ic_action_search = 0x7f020000 ;
public static final int ic_launcher = 0x7f020001 ; }
}
Trang 100Layout File
Chứa định nghĩa nhiều control
android:layout_centerVertical = "true"
android:padding = "@dimen/padding_medium"
android:text = "@string/hello_world" tools:context = ".MainActivity"
/>
Trang 101Running the Application
Trang 102Tạo project với Android Studio
project/File -> New -> New Project
Trang 103Tạo project với Android Studio
Trang 105Tổ chức và truy cập resource
Resource: bitmap, color, layout, string,
animation,
Trang 106Tổ chức và truy cập resource
Eclipse
Trang 107Tổ chức và truy cập resource
Directory Resource Type
anim/ XML - animations Truy cập bởi lớp R.anim
color/ XML - colors Truy cập bởi lớp R.color
drawable/ png, jpg, gif / XML -> bitmaps, state list, shapes,
animation Được truy cập bởi lớp R.drawable
layout/ XML, truy cập bởi lớp R.layout
menu/ XML (Options Menu, Context Menu, Sub Menu)
Được truy cập bởi lớp R.menu
raw/ Chứa các file gốc, cần gọi
Resources.openRawResource() kèm với resource
ID – tương ứng R.raw.filename để mở file
Trang 108Tổ chức và truy cập resource
Directory Resource Type
values/ •XML: strings, integers, colors File XXX xml
sẽ được truy cập bởi lớp R.XXX
xml/ XML – có thể được đọc tại lúc thực thi với
Resources.getXML(): lưu trữ các file config
Trang 109Các resource lựa chọn/thay thế
Cung cấp nhiều nhiều resource khác nhau để đáp ứng tốt các yêu cầu
Ex: nhiều resource kéo thả (drawable) để cung ứng
nhiều loại màn hình (khác nhau độ phân giải/ cung cấp nhiều resource chuỗi (string) cho nhiều ngôn ngữ
Lúc thực thi, android phát hiện các config và yêu cầu resource phù hợp
Trang 110Các resource lựa chọn/thay thế
Trang 111Truy cập resource
Tạo ra lớp R – chứa các ID cho các resource (res/)
ImageView imageView = ( ImageView )
findViewById ( R id myimageview );
Truy cập resource thông qua tên/id của resource imageView setImageResource ( R drawable myimage );
EX: Thiết lập giá trị TextView
TextView msgTextView = ( TextView )
findViewById ( R id msg );
msgTextView setText ( R string hello );
Trang 112Truy cập resource
res/values/strings.xml
Layout file
Trang 113Activity
sổ/khung)
activity bắt đầu với hàm onCreate()
Trang 115Service
Thời gian thực thi dài
hoạt động
Trang 116Trạng thái
(như activity) gọi startService()
dạng client – server cho phép các thành
phần (component) tương tác với dịch vụ, gởi yêu cầu, lấy kết quả
Trang 118 Khi một component khác muốn kết đên dịch vụ bởi bindService()
Nếu hiện thực, phải cung cấp giao diện cho người dùng giao tiếp với dịch vụ bằng cách trả vê đối tượng IBinder
Trang 119Các bước
Thêm service vào “Hello World”
Thêm startService() and stopService() vào
MainActivity.java
Tạo file MyService.java để hiện thực các phương thức
liên quan đến service
Khai báo dịch vụ trong AndroidManifest.xml dùng thẻ
<service /> (một ứng dụng có thể có một hoặc nhiều service)
<service android:name = ".MyService" />
Thêm 2 button vào res/layout/activity_main.xml
Thay đổi nội dung trong res/values/strings.xml
Chạy ứng dụng và kiểm tra kết quả
Trang 121Layout
text field, )
ViewGroup (ViewGroup cũng có thể chứa các ViewGroup khác) và định nghĩa layout riêng
Trang 122Các loại layout
1 Linear Layout: theo một hướng (ngang/dọc)
2 Relative Layout: hiển thị theo các vị trí có
liên hệ nhau
3 Table Layout: theo dòng và cột
4 Absolute Layout: chính xác cho từng thành
phần
5 Frame Layout: chỗ được giữ để hiển thị
single view
6 List View: danh sách các mục có thể cuộc
7 Grid View: theo 2 chiều, có thể cuộn dạng
lưới
Trang 123Linear Layout
Trang 124android:divider TThanh chia (dọc) để ngăn các thành phần Có
thể dùng các màu khác nhau như "#rgb",
"#argb", "#rrggbb", "#aarrggbb"
android:gravity Cách đối tượng định vị nội dung của nó: top,
bottom, left, right, center, center_vertical, center_horizontal,
Trang 125Ví dụ
<? xml version = "1.0" encoding = "utf-8" ?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "fill_parent" android:layout_height = "fill_parent"
Trang 127Relative Layout
Trang 128RelativeLayout Attributes
Thuộc tính Mô tả
android:id ID xác định layout duy nhất
android:gravity Cách đối tượng định vị nội dung của nó
Giá trị: top, bottom, left, right, center, center_vertical, center_horizontal,
android:ignoreGravity View không bị tác động bởi gravity