Bài giảng Lập trình cho thiết bị di động - Chương 4: Xử lý đa tiến trình và dịch vụ - Trần Duy Thanh

DONG NAI UNIVERSITY OF TECHNOLOGY  
1. Multi - Threading  
2. Intent filter  
3. Broadcast Receiver  
4. Android Services  
5. Webservice .Net  
1
DONG NAI UNIVERSITY OF TECHNOLOGY  
1. Multi - Threading  
1.1 Introduction  
1.2 Handler class  
1.3 AsyncTask class  
2
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.1 Introduction  
Threads  
Android’s threads run in a manner similar to common  
Java threads  
A Thread is a concurrent unit of execution.  
not executing in order  
has its own call stack for methods being invoked, their  
arguments and local variables.  
Each virtual machine instance has at least one main  
Thread running when it is started;  
The application might decide to launch additional Threads  
for specific purposes.  
3
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.1 Introduction  
Multi- Threading  
4
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.1 Introduction  
Multi- Threading  
Threads in the same VM interact and synchronize by the  
use of shared objects and monitors associated with  
these objects.  
There are basically two main ways of having a Thread  
execute application code.  
1.Create a new class that extends Thread and override its  
run() method.  
2.Create a new Thread instance passing to it a Runnable  
object.  
In both cases, the start() method must be called to  
actually execute the new Thread.  
5
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.1 Introduction  
Advantages of Multi- Threading  
Threads share the process' resources but are able to  
execute independently.  
Applications responsibilities can be separated main  
thread runs UI, and slow tasks are sent to background  
threads.  
Threading provides an useful abstraction of concurrent  
execution.  
Particularly useful in the case of a single process that  
spawns multiple threads on top of a multiprocessor  
system. In this case real parallelism is achieved.  
Consequently, a multithreaded program operates faster on  
computer systems that have multiple CPUs.  
6
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.1 Introduction  
Disadvantages of Multi- Threading  
Code :more complex  
Need to detect, avoid, resolve deadlocks  
Threads not executing in order  
Runnable v.s Thread?  
What different?  
Deadlock and Atomic type  
7
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
An application may involve a time-consuming operation,  
however we want the UI to be responsive to the user. Android  
offers two ways for dealing with this scenario:  
Do expensive operations in a background service, using  
notifications to inform users about next step  
Do the slow work in a background thread. Interaction  
between Android threads is accomplished using (a)  
Handler objects and (b) posting Runnable objects to the  
main view.  
8
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
When a process is created for your application, its main  
thread is dedicated to running a message queue that takes  
care of managing the top-level application objects (activities,  
intent receivers, etc) and any windows they create.  
You can create your own secondary threads, and  
communicate back with the main application thread through  
a Handler.  
When you create a new Handler, it is bound to the message  
queue of the thread that is creating it -- from that point on, it  
will deliver messages and runnables to that message queue  
and execute them as they come out of the message queue.  
9
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
There are two main uses for a Handler:  
(1)to schedule messages and runnables to be executed as  
some point in the future; and  
(2)to enqueue an action to be performed on another thread  
10  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
Threads and UI Warning  
Background threads are not allowed to interact with  
the UI.  
Only the main process can access the (main)  
activity’s view.  
(Global) class variables can be seen and updated in  
the threads  
11  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
➢ Handler’s MessageQueue  
A secondary thread that wants to communicate with the main  
thread must request a message token using the  
obtainMessage() method.  
Once obtained, the background thread can fill data into the  
message token and attach it to the Handler‟s message queue  
using the sendMessage() method.  
The Handler uses the handleMessage() method to  
continuously attend new messages arriving to the main thread.  
A message extracted from the process’ queue can either  
return some data to the main process or request the execution  
of runnable objects through the post() method.  
12  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
➢ Handler’s MessageQueue  
13  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
Using Messages  
Main Thread  
Background Thread  
…
…
…
…
14  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
15  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
➢ Messages  
To send a Message to a Handler, the thread must first invoke  
obtainMessage() to get the Message object out of the pool.  
There are a few forms of obtainMessage(), allowing you to  
just create an empty Message object, or messages holding  
arguments  
Example  
// thread 1 produces some local data  
String localData = “Greeting from thread 1”;  
// thread 1 requests a message & adds localData to it  
Message mgs = myHandler.obtainMessage (1, localData);  
16  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
➢ sendMessage Method  
You deliver the message using one of the sendMessage...()  
family of methods, such as …  
•sendMessage() puts the message at the end of the  
queue immediately  
• sendMessageAtFrontOfQueue() puts the message at  
the front of the queue immediately (versus the back, as is  
the default), so your message takes priority over all others  
• sendMessageAtTime() puts the message on the queue  
at the stated time, expressed in the form of milliseconds  
based on system uptime (SystemClock.uptimeMillis())  
• sendMessageDelayed() puts the message on the  
queue after a delay, expressed in milliseconds  
17  
DONG NAI UNIVERSITY OF TECHNOLOGY  
1.2 Handler class  
➢ Processing Messages  
To process messages sent by the background  
threads, your Handler needs to implement the listener  
handleMessage( . . . )  
which will be called with each message that appears  
on the message queue.  
There, the handler can update the UI as needed.  
However, it should still do that work quickly, as other  
UI work is suspended until the Handler is done.  
18  
DONG NAI UNIVERSITY OF TECHNOLOGY  
➢ XML Layout  
1.2 Handler class  
➢ Examples  
– Using Message  
19  
DONG NAI UNIVERSITY OF TECHNOLOGY  
➢ Coding  
1.2 Handler class  
msg get from sendMessage in  
background thread  
20  
Tải về để xem bản đầy đủ
pptx 147 trang baolam 13940 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình cho thiết bị di động - Chương 4: Xử lý đa tiến trình và dịch vụ - Trần Duy Thanh", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pptxbai_giang_lap_trinh_cho_thiet_bi_di_dong_chuong_4_xu_ly_da_t.pptx