2. 222
22. Android Services
Services
2
Android Services
Một Service là một component ứng dụng chạy tại 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.
Service, cũng như các đối tượng ứng dụng khác (activitties, broadcast
listeners…), chạy tại thread chính của tiến trình chủ.
Nghĩa là, nếu một service định làm gì đó chiếm CPU (chẳng hạn MP3
playback) hoặc đợi lâu (networking), nó nên tạo một thread riêng để làm
việc đó.
Mỗi service phải có một khai báo <service> tại AndroidManifest.xml của
package.
Service được gọi bằng Context.startService() và Context.bindService().
3. 333
22. Android Services
Services
3
Android Services
• Đối với mỗi service, nhiều lời gọi Context.startService() chỉ dẫn đến việc
phương thức onStartCommand() của Service class được gọi nhiều lần). Tuy
nhiên, chỉ cần một lần Context.stopService() hay stopSelf() được gọi là đủ
để kết thúc service đó.
•Một service có thể được khởi động và chạy cho đến khi nào ai đó dừng nó
hoặc nó tự dừng.
4. 444
22. Android Services
Services
4
Service Life Cycle
CŨng như activity, một service có các phương
thức lifecycle mà ta cài để giám sát các thay đổi
trạng thái của nó. Nhưng service có ít phương
thức life cycle hơn, và chúng ở dạng public chứ
không phải protected:
1. void onCreate ()
2. void onStartCommand (Intent intent,…)
3. void onDestroy ()
onCreate
onStart
onDestroy
5. 555
22. Android Services
Services
5
Service Life Cycle
The entire lifetime of a service happens between the time onCreate() is called
and the time onDestroy() returns.
Toàn bộ cuộc đời của một service bắt đầu khi onCreate() được gọi và kết
thúc khi onDestroy() trả về.
Like an activity, a service does its initial setup in onCreate(), and releases all
remaining resources in onDestroy().
Cũng như một activity, một service thực hiện các công việc khởi tạo tại
onCreate() và trả tất cả các tài nguyên còn lại tại OnDestroy()
For example, a music playback service could create the thread where the
music will be played in onCreate(), and then stop the thread in onDestroy().
Ví dụ, một service chơi nhạc có thể tạo thread phụ chơi nhạc tại phương
thức onCreate(), và dừng thread đó tại onDestroy()
6. 666
22. Android Services
Services
6
Broadcast Receiver Lifecycle
A Broadcast Receiver là lớp ứng dụng nghe các global Intent được gửi cho tất cả
những ai muốn nghe (thay vì chỉ gửi cho một ứng dụng/activity được chỉ định trước).
Hệ thống gửi một broadcast Intent cho tất cả các broadcast
receiver quan tâm, và chúng lần lượt xử lý Intent đó.
7. 777
22. Android Services
Services
7
Registering a Broadcast Receiver
• Ta có thể đăng ký động (tại mã chương trình) một thực thể
BroadcastReceiver bằng lời gọi Context.registerReceiver()
• hoặc đăng kí tĩnh bằng cách khai báo thẻ <receiver> tại
AndroidManifest.xml.
8. 888
22. Android Services
Services
8
Broadcast Receiver Lifecycle
Mỗi broadcast receiver có đúng một callback method:
void onReceive (Context context, Intent broadcastMsg)
1. Khi một broadcast message được gửi đến receiver, Android gọi phương thức
onReceive() của receiver đó và truyền cho nó đối tượng Intent chứa message.
2. Một broadcast receiver chỉ được coi là active (đang chạy) trong khi nó đang
thực thi phương onReceive() của mình.
3. Khi onReceive() kết thúc, receiver quay lại trạng thái inactive.
* callback method là các phương thức dành riêng để hệ thống gọi khi có sự
kiện nào đó xảy ra, nó không được gọi trực tiếp trong mã chương trình.
onReceive
9. 999
22. Android Services
Services
9
Services, BroadcastReceivers and the AndroidManifest
Manifest của các ứng dụng dùng Android Services phải có chứa:
1.Một <service> entry (mục) cho mỗi service dùng trong ứng dụng.
2.Nếu ứng dụng định nghĩa một BroadcastReceiver dưới hình thức một lớp
độc lập, manifest phải có một <receiver> clause (mệnh đề) xác định
component đó. Ngoài ra, còn cần một <intent-filter> entry để khai báo filter
mà service và receiver sử dụng.
See example
11. 111111
22. Android Services
Services
11
Types of Broadcasts
Có hai lớp broadcast chính:
1.Normal broadcast (được gửi bằng Context.sendBroadcast) hoàn toàn
không đồng bộ (asynchronous). Tất cả các receiver của broadcast được chạy
theo thứ tự không xác định, thường là chạy cùng lúc.
2.Ordered broadcast (được gửi bằng Context.sendOrderedBroadcast) được
gửi lần lượt đến cho từng receiver một. Khi đến lượt một receiver thực thi,
nó có thể chuyển tiếp kết quả cho receiver tiếp theo, hoặc nó có thể hủy
toàn bộ broadcast (abortBroadcast()) để broadcast đó sẽ không được gửi
đến cho các receiver khác. Thứ tự thực thi của các receiver được kiểm soát
bởi thuộc tính android:priority của intent-filter tương ứng; các receiver có
cùng priority (độ ưu tiên) sẽ được chạy theo thứ tự bất kì.
12. 121212
22. Android Services
Services
12
Useful Methods – The Driver
Giả sử main activity MyService3Driver muốn tương tác với một service có
tên MyService3. Activity chính có trách nhiệm thực hiện các việc sau:
1. Start the service called MyService3. Bật service
Intent intentMyService = new Intent(this, MyService3.class);
Service myService = startService(intentMyService);
2. Define corresponding receiver’s filter and register local receiver. Định nghĩa
filter của receiver tương ứng và đăng ký receiver nội bộ.
IntentFilter mainFilter = new IntentFilter("matos.action.GOSERVICE3");
BroadcastReceiver receiver = new MyMainLocalReceiver();
registerReceiver(receiver, mainFilter);
3. Implement local receiver and override its main method
public void onReceive(Context localContext, Intent callerIntent)
13. 131313
22. Android Services
Services
13
Useful Methods – The Service
Giả sử main activity MyService3Driver muốn tương tác với một service có tên
MyService3. Activity đó dùng phương thức onStart của nó để làm việc sau:
1. Tạo một Intent với broadcast filter phù hợp (bao nhiêu receiver khớp với nó
cũng được).
Intent myFilteredResponse = new Intent("matos.action.GOSERVICE3");
2. Chuẩn bị dữ liệu extra (‘myServiceData’) để gửi theo intent tới (các) receiver
Object msg = some user data goes here;
myFilteredResponse.putExtra("myServiceData", msg);
3. Gửi intent tới tất cả các receiver khớp với filter đã tạo
sendBroadcast(myFilteredResponse);
14. 141414
22. Android Services
Services
14
Useful Methods – The Driver (again)
Giả sử main activity MyService3Driver muốn tương tác với một service có
tên MyService3. Activity chính có trách nhiệm kết thúc service một cách sạch
sẽ. Thực hiện như sau
1. Giả sử intentMyService là Intent đã được dùng để gọi service. Việc kết
thúc service được làm bằng cách gọi phương thức
stopService(new Intent(intentMyService) );
2. Tại phương thức onDestroy của activity, đảm bảo rằng tất cả các thread
đang chạy của nó được chấm dứt và receiver được hủy đăng ký.
unregisterReceiver(receiver);
15. 151515
22. Android Services
Example 1. A very Simple Service
15
Main activity bật một service. Service đó in vài dòng tại DDMS LogCat cho đến
khi main activity dừng service. Không có liên lạc giữa các tiến trình khác nhau
(IPC).// a simple service is started & stopped
package es.demo;
import android.app.Activity;
import android.os.Bundle;
import android.content.ComponentName;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class ServiceDriver1 extends Activity {
TextView txtMsg;
Button btnStopService;
Intent intentMyService;
ComponentName service;
17. 171717
22. Android Services
Example 1. cont.
17
//non CPU intensive service running the main task in its main thread
package es.demo;
import ...
public class MyService1 extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i ("<<MyService1-onStart>>", "I am alive-1!");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.i ("<<MyService1-onStart>>", "I did something very quickly");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i ("<<MyService1-onDestroy>>", "I am dead-1");
}
}
18. 181818
22. Android Services
Services
18
Example 1. cont.
According to the Log
1. Main Activity is started (no displayed yet)
2. Service is started (onCreate, onStart)
3. Main Activity UI is displayed
4. User stops Service
21. 212121
22. Android Services
Example 2
21
Example 2. A More Realistic Activity-Service Interaction
1.The main activity starts the service and registers a receiver.
Activity chính bật service và đăng kí một receiver.
2.The service is slow, therefore it runs in a parallel thread its time consuming task.
Service chạy chậm, nên nó chạy công việc của nó trong một thread song song.
3.When done with a computing cycle, the service adds a message to an intent.
Sau mỗi chu kì tính toán, service gắn một message vào một intent
4.The intent is broadcasted using the filter: matos.action.GOSERVICE3.
Intent được broadcast bằng filter matos.action.GOSERVICE3
5.A BroadcastReceiver (defined inside the main Activity) uses the previous filter and
catches the message (displays the contents on the main UI ). Receiver (đã được đăng kí
từ trong activity chính) dùng filter để bắt message mà service gửi rồi hiển thị tại UI.
6.At some point the main activity stops the service and finishes executing.
Tại một thời điểm nào đó, activity chính dừng service và kết thúc.
27. 272727
22. Android Services
Services
27
//////////////////////////////////////////////////////////////////////
// local (embedded) RECEIVER
public class MyMainLocalReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context localContext, Intent callerIntent) {
String serviceData = callerIntent.getStringExtra("serviceData");
Log.e ("MAIN>>>", serviceData + " -receiving data "
+ SystemClock.elapsedRealtime() );
String now = "n" + serviceData + " --- "
+ new Date().toLocaleString();
txtMsg.append(now);
}
}//MyMainLocalReceiver
}//MyServiceDriver3
Example 2. Main Activity
Get data
28. 282828
22. Android Services
Example 2. The Service
28
// Service3 uses a thread to run slow operation
package es.demos;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService3 extends Service {
boolean isRunning = true;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
29. 292929
22. Android Services
Example 2. The Service
29
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.e ("<<MyService3-onStart>>", "I am alive-3!");
// we place the slow work of the service in its own thread
// so the caller is not hung up waiting for us
Thread triggerService = new Thread ( new Runnable(){
long startingTime = System.currentTimeMillis();
long tics = 0;
public void run() {
for(int i=0; (i< 120) & isRunning; i++) { //at most 10 minutes
try {
//fake that you are very busy here
tics = System.currentTimeMillis() - startingTime;
Intent myFilteredResponse =
new Intent("es.action.GOSERVICE3");
String msg = i + " value: " + tics;
myFilteredResponse.putExtra("serviceData", msg);
sendBroadcast(myFilteredResponse);
Thread.sleep(1000); //five seconds
} catch (Exception e) { e.printStackTrace(); }
}//for
}//run
});
triggerService.start();
return START_STICKY;
}//onStartCommand
broadcasting
Set filter
30. 303030
22. Android Services
Services
30
@Override
public void onDestroy() {
super.onDestroy();
Log.e ("<<MyService3-onDestroy>>", "I am dead-3");
isRunning = false;
}//onDestroy
}//MyService3
Example 2. The Service
Thread is now stopped