Tài liệu Lập trình Android bằng tiếng việt được làm bằng Powerpoint khá dễ đọc, gồm 694 trang.
Trang 1Android Developers http://developer.android.com/index.html
Trang 3Chapter 1 - Resources
Android’s web page
http://www.android.com/
Trang 4What is Android?
• Android is an open-source software platform
created by Google and the Open Handset
Alliance.
• It is primarily used to power mobile phones.
• It has the capability to make inroads in many other (non-phone) embedded application
markets.
4
Trang 5What is Android?
• Android™ consists of a complete set of
software components for mobile devices including:
– an operating system,
– middleware, and
– embedded key mobile applications
– a large market
Trang 6Why Android?
Listen from the project creators/developers (2.19 min)
– Nick Sears Co-founder of Android
– Steve Horowitz Engineering Director
– Dam Morrill Developer
– Peisun Wu Engineering Project Manager
– Erick Tseng Project Manager
– Iliyan Malchev Engineer
– Mike Cleron Software Manager
– Per Gustafsson Graphics Designer.
– etc…
• http://www.youtube.com/watch?v=6rYozIZOgDk&eurl=http://www.android.com/about/&feature=player_embedded
“…currently it is too difficult to make new products … open software brings more innovation
… choices … lower costs … more applications such as family planner, my taxes, understand my wife better, … ”
6
Trang 7What is Open Handset Alliance?
• Quoting from www.OpenHandsetAlliance.com page
mobile companies have come together to accelerate
innovation in mobile and offer consumers a richer, less
expensive, and better mobile experience
• Together we have developed Android™, the first complete, open, and free mobile platform.
• We are committed to commercially deploy handsets and
Trang 8Open Handset Alliance Members
Operators Software Co Commercializat Semiconductor Handset Manf
Nuance Comm.
PacketVideo SkyPop SONiVOX
… Borqs
Aplix Noser Engineering Astonishing Tribe Wind River Systems Omron Software
… Teleca
Audience Broadcom Corp.
Intel Corp.
Marvell Tech
Group Nvidia Corp.
Qualcomm SiRF Tech Holdings Synaptics
Texas Instr.
AKM Semicond.
ARM Atheros Comm
EMP
ACER ASUS HTC LG Motorola Samsung ASUSTek Garmin Huawei Tech LG
Samsung
… Sony Ericsson Toshiba
8
Trang 9See Android Developers
Short video (4 min.)
Showing Dave Bort
and Dan Borstein,
two members of the
Android Open Source
Project talk about the
project.
http://www.youtube.com/watch?v=7Y4thikv-OM
Trang 10The Android Platform
Again, what did they say about Android?
• Android is a software environment built for mobile devices
• It is not a hardware platform.
Trang 11Android’s Context: Mobile Market Player $
Stakeholders:
Mobile network operators want to
lock down their networks, controlling and metering traffic
Device manufacturers want to
differentiate themselves with features, reliability, and price points
Software vendors want complete
access to the hardware to deliver cutting-edge applications
Trang 12The Maturing Mobile Experience
Trang 13The Maturing Mobile Experience
I want my 2015 Smartphone to act as …
1 Phone
2 Pager
3 PDA Organizer
4 High Quality Camera (still & video)
5 Portable music player
6 Portable TV / Video Player / Radio
7 Laptop
8 Play Station
9 GPS
10 Golf Caddy (ball retriever too)
11 Book Reader (I don’t read, It reads to me)
12 Car / Home / Office Key
13 Remote Control (Garage, TV, …)
14 Credit Card / Driver’s License / Passport
Trying to answer: Tomorrow ?
Trang 14Android vs Competitors
14
1.Apple Inc.
2.Microsoft 3.Nokia
4.Palm 5.Research In Motion 6.Symbian
Trang 16Android Components (Stack)
• The Android stack includes a large array of features for mobile applications.
general purpose computing environment
• All of the major components of a computing platform are included.
16
Trang 17Android Components
• Application framework enabling reuse and replacement of components
• Dalvik virtual machine optimized for mobile devices
• Integrated browser based on the open source WebKit engine
• Optimized graphics powered by a custom 2D graphics library; 3D graphics
based on the OpenGL ES specification (hardware acceleration optional)
• SQLite for structured data storage
• Media support for common audio, video, and still image formats (MPEG4,
H.264, MP3, AAC, AMR, JPG, PNG, GIF)
• GSM Telephony (hardware dependent)
• Bluetooth, EDGE, 3G, 4G, and Wi-Fi (hardware dependent)
• Camera, GPS, compass, and accelerometer (hardware dependent)
• Rich development environment including a device emulator, tools for
Trang 18Android Components
18
Trang 19Android Components
Video 1/3: Android’s Architecture
Presented by Mike Cleron, Google Corp (13 min)
Trang 20Android Components
20
Video 2/3: Application’s Life Cycle
Presented by Mike Cleron, Google Corp (8 min)
Available at: http://www.youtube.com/watch?v=fL6gSd4ugSI&feature=channel
Trang 21Android Components
Video 3/3: Android’s API
Presented by Mike Cleron, Google Corp (7 min)
Trang 22Android Application Framework
Video:
Inside the
Android Application Framework
(about 52 min)
Presented by Dan Morrill – Google
At Google Developer Conference
machinery behind the application framework.
explains the life-cycle of an android apk very good!
Trang 23Android Components
Video:
An Introduction to Android
(about 52 min)
Presented by Jason Chen – Google
At Google Developer Conference
San Francisco - 2008
Available at:
http://www.youtube.com/watch?v=x1ZZ-R3p_w8
Trang 24Why use Linux for a phone?
• Linux kernel is a proven core platform
• Reliability is more important than performance when it
comes to a mobile phone, because voice communication is the primary use of a phone.
• Linux provides a hardware abstraction layer, letting the upper
levels remain unchanged despite changes in the underlying hardware
• As new accessories appear on the market, drivers can be
written at the Linux level to provide support, just as on other Linux platforms.
24
Trang 25Dalvik Virtual Machine
• User application, cũng như các ứng dụng core Android, được
• Android byte code được thông dịch tại thời gian chạy bởi máy
• Tại sao dùng một máy ảo khác?
bytecode, nhưng chúng cho phép Android
rằng buộc bởi các hạn chế trong giấy phép của Sun
Trang 26Dalvik Virtual Machine
Trang 27Inside Android: Intents
• Chủ đề quan trọng hay gặp trong phát triển ứng dụng Android là Intent
• Một Intent trong Android mô tả cái ta muốn làm
• Có thể là
– “Tôi muốn tra cứu một mục trong contact record,” hoặc
– “Hãy mở website này,” hoặc
– “Mở màn hình khẳng định đơn đặt hàng.”
• Tầm quan trọng của intent : chúng cho phép di chuyển điều khiển trong ứng dụng và là khía cạnh quan trọng
Trang 28Intents & IntentFilters
• An Intent is a declaration of need.
• Một Intent bao gồm các mẩu thông tin sau:
– action hay service muốn thực hiện,
– data – dữ liệu vào cho action/service đó, và
– category – loại component nên xử lý intent cùng với các hướng
dẫn về cách gọi activity cần gọi
• Một IntentFilter là một trigger, một khai báo về năng lực
cung cấp hỗ trợ cho những nơi cần.
• Một IntentFilter có thể khai báo tổng quát hoặc cụ thể về
các intent mà nó có thể phục vụ.
28
Trang 29Intents & IntentFilters
• Một intent là một mô tả trừu tượng về một thao tác cần thực hiện.
• Ứng dụng quan trọng nhất của nó là cho việc gọi activity (launching of
activities), có thể coi intent là keo dán giữa các activity
• Các tham số chính của một intent:
Trang 30Intents & IntentFilters
30
Ví dụ về các cặp action/data của intent:
ACTION_VIEW content://contacts/1 – Hiện thông tin về người có id là "1" ACTION_DIAL content://contacts/1 – Hiện ứng dụng gọi điện thoại cho
người có id là “1”
ACTION_VIEW tel:123 Hiện ứng dụng gọi điện thoại với số điện thoại
điền sẵn là 123
ACTION_DIAL tel:123 – Thực hiện quay số cho số điện thoại 123.
ACTION_EDIT content://contacts/1 – Soạn thông tin về người có id là “1” ACTION_VIEW content://contacts/ Hiện danh sách contact, cho phép
duyệt danh sách
Trang 31Dissecting Intents
1 Component name Tên component cần xử lý intent ( ví dụ
2 Action Một string là tên của action cần thực hiện — hoặc, đối với các broadcast intent,
action đã xảy ra và đang được báo cáo kết quả (ví dụ: ACTION_VIEW, ACTION_CALL,
Trang 32Delivering Intents
• Một đối tượng Intent được truyền cho
Context.startActivity() hoặc Activity.startActivityForResult()
để gọi một activity (đồng bộ) hoặc yêu cầu một activity sẵn có làm gì đó (không đồng bộ).
• Một đối tượng Intent được truyền cho
Context.startService() để bật một service hoặc gửi lệnh mới cho một service đang chạy
kết nối giữa component đang gọi và component bị gọi Nó có thể bật service nếu service đó chưa phải đang chạy.
32
Trang 33Intent Resolution
Có hai loại Intent:
• Explicit intent chỉ định component đích bằng tên, thường được dùng khi một activity bật một service con hoặc chạy một activity khác.
• Implicit intent không gọi tên component đích (trường dành cho tên component để trống), thường được dùng để kích hoạt các component trong các ứng dụng khác Late binding được áp dụng ở đây.
Mỗi khi có thể, Android sẽ gửi explicit intent cho một thực thể
Trang 34Example of Intent (1)
• Following fragments calls an Intent whose job is to invoke a
built-in task (ACTION_VIEW) and explore the Contacts
available in the phone.
Intent myIntent = new Intent(
Intent.ACTION_VIEW,
startActivity(myIntent);
34
Trang 35Example of Intent (1)
Intent uses ACTION_VIEW
to see Contacts
Trang 36public class AndDemo1 extends Activity {
/** show contact list */
Trang 37Example of Intent (2)
• Following Intent uses built-in task (ACTION_VIEW) to
explore a web page
(see new Uri value)
Intent myIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(myIntent);
Trang 38Example of Intent (3)
• Following Intent uses built-in task (ACTION_VIEW) to
make a phone call
(see new Uri value)
Intent myIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("tel:/216 555-1234"));
startActivity(myIntent);
38
Trang 39• IntentFilter định nghĩa quan hệ giữa Intent và ứng
dụng.
• IntentFilter có thể quy định cụ thể đến phần data,
hoặc phần action của intent, hoặc cả hai
• IntentFilter còn chứa một trường được gọi là một
category Category giúp phân loại các action
• Ví dụ, category có tên CATEGORY_LAUNCHER
chỉ dẫn Android rằng Activity chứa IntentFilter này cần được nhìn thấy tại home screen.
Trang 40• Khi một Intent được phát đi, hệ thống sẽ đánh giá các
Activity, Service hiện có, và các BroadcastReceiver đã được đăng kí, rồi gửi Intent đó tới nơi nhận phù hợp nhất (Xem hình sau).
40
Trang 41IntentFilters
Trang 4242
activity, service, và broadcast receiver có thể có một hoặc nhiều intent
filter
• Mỗi filter mô tả một năng lực mà component đó muốn nhận
trong; không cần hỏi đến các filter
• Nhưng một implicit intent chỉ được gửi đến một component khi và chỉ
khi nó lọt qua một trong các filter của component
Trang 43• IntentFilters are often defined in an application’s
AndroidManifest.xml with the <intent-filter> tag.
<intent-filter >
<action android:name="code android.intent.action.MAIN" />
<category android:name="code android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:type="video/mpeg" android:scheme="http" />
<data android:type="audio/mpeg" android:scheme="http" />
</intent-filter>
Trang 46Android Services
• Service là một component chạy trong background,
không tương tác với người dùng, chạy trong
khoảng thời gian không xác định
• Mỗi lớp service phải có một khai báo <service>
tương ứng trong AndroidManifest.xml
Trang 47Android Services
• Service, cũng như các đối tượng ứng dụng
khác, chạy trong thread chính của tiến trình
chủ
• Nghĩa là, nếu một service định làm thao tác gì chiếm nhiều thời gian CPU (chẳng hạn MP3
playback) hoặc blocking (chẳng hạn
networking, RSS exchange), nó cần sinh thread riêng để làm công việc đó
Trang 48public class Service1 extends Service implements Runnable {
private int counter = 0;
Log.i( " service1 " , "service1 firing : # " + counter ++);
Thread.sleep(10000); //this is where the heavy-duty computing occurs
} catch (Exception ee) {
Log.e( " service1 " , ee.getMessage());
} }
Trang 49// invoking the service
Intent service1Intent = new Intent( this, Service1.class );
startService( service1Intent );
// do some work here….
Trang 51Android Services
Debugging - Log Cat
07-01 02:49:46.097: INFO/ActivityManager(583): Displayed activity matos.service /.Service1 Driver 07-01 02:49:51.277: DEBUG/dalvikvm(724): GC freed 1575 objects / 81280 bytes in 138ms
07-01 02:49:55.831: INFO/service1(767): service1 firing : # 1
07-01 02:50:05.839: INFO/service1(767): service1 firing : # 2
07-01 02:50:15.847: INFO/service1(767): service1 firing : # 3
07-01 02:50:25.857: INFO/service1(767): service1 firing : # 4
Trang 52Android Broadcast Receiver
What is a BROADCASTRECEIVER?
• Nếu một ứng dụng muốn nhận và đáp ứng một global event, chẳng hạn
điện thoại reo hoặc một tin nhắn đến, nó phải đăng kí làm một
BroadcastReceiver
nó tại file AndroidManfest.xml
• Nếu receiver được đăng kí tại file AndroidManifest.xml, nó sẽ được kích hoạt khi cần chứ không nhất thiết phải đang trong trạng thái hoạt động khi có Intent xảy ra
• Khi xảy ra global event, ứng dụng sẽ được bật tự động khi có thông báo
về event Hệ điều hành Android chịu trách nhiệm thực hiện việc này
• Một ứng dụng có thể đăng kí trong khi đang chạy bằng phương thức
registerReceiver của lớp Context
52
Trang 53Android Broadcast Receiver
BROADCASTRECEIVER and UI.
• Cũng như Service, BroadcastReceiver không có giao diện
người dùng
• Đối tượng BroadcastReceiver chỉ tồn tại khi phần mã trong
không tồn tại một khi phương thức đó kết thúc
• Nếu BroadcastReceiver cần thực thi mã nhiều hơn mức tầm thường, phần mã đó nên yêu cầu một Service thực
Trang 54Android Broadcast Receiver
Trang 55Android Broadcast Receiver
Intents vs Broadcasts
– Dùng một Intent để bật một Activity là một hoạt động tại foreground , làm thay đổi phần mà user đang tương tác
– Broadcasting một Intent là một hoạt động tại
background mà user thường không nhận biết
Trang 56Android Broadcast Receiver
Type of Broadcasts
There are two major classes of broadcasts that can be received:
• Normal broadcasts (sent with sendBroadcast) are completely asynchronous
All receivers of the broadcast are run in an undefined order, often at the
same time This is more efficient, but means that receivers cannot use the result or abort APIs included here
• Ordered broadcasts (sent with sendOrderedBroadcast) are delivered to one
receiver at a time As each receiver executes in turn, it can propagate a result
to the next receiver, or it can completely abort the broadcast so that it won't
be passed to other receivers The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order
56
Trang 57Android Broadcast Receiver
Broadcast Receiver Life Cycle
• A process that is currently executing a BroadcastReceiver (that is, currently
running the code in its onReceive(Context, Intent) method) is considered to be a foreground process and will be kept running by the system except under cases of extreme memory pressure
• Once you return from onReceive(), the BroadcastReceiver is no longer active, and its hosting process is only as important as any other application components that are running in it
• This means that for longer-running operations you will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for