Phan 06 Service

You might also like

You are on page 1of 19

Pht trin ng

dng
Smartphone
Ti liu lu hnh ni b
y l ti liu tham kho s dng trong mn hc Lp trnh ng dng Smartphone Android
c tng hp, bin son t nhiu ngun bi cc thnh vin ca Nhm nghin cu v ng dng cng
ngh A106-i hc Hoa Sen.
Nhm A106-http://profiles. hoasen. edu. vn/groups/584/
Pht trin ng dng Smartphone Android



Pht trin ng
dng Smartphone

Phn 06: Service
L c Huy
Email: leduchuy89vn@gmail.com
Pht trin ng dng Smartphone Android

Mc lc
1 Service trn Android ............................................................................................... 3
Mt Service c th c s dng theo hai cch............................................................ 3
1.1 Local Service ................................................................................................... 5
1.2 Remote Service ............................................................................................. 11
1.2.1 Hin thc remote service ........................................................................... 11
1.2.2 Hin thc ng dng khch ........................................................................ 14
2 Tham kho ............................................................................................................ 18

Pht trin ng dng Smartphone Android

1 Service trn Android
Service l mt thnh phn ca ng dng Android dng thc thi mt tc v ngm bn di
nn v khng c giao din hin th ni dung. Service cng ging nh cc thnh phn khc ca ng
dng(Activity, BroadcastReceiver), n s chy trn lung chnh ca tin trnh m ng dng ang
chy trn . iu ny c ngha l nu bn cn thc hin mt cng vic no tn nhiu thi gian nh
chi nhc, ti d liu t trn mng v th bn phi a cng vic vo mt lung ring thc thi.
Vic ny s trnh cho cc cng vic ang thc thi trn lung chnh khng b gin on. Chng ta cn
xc nh r cc c trng ca Service:
o Mt Service khng phi l mt tin trnh tch bit. Mt i tng Service khng h chy
trn tin trnh ca ring n m n chy trn tin trnh ca ng dng.
o Mt Service khng phi l mt lung. iu ny c ngha l mi cng vic s c lung
chnh thc thi. Chnh v vy mt i tng Service thng nh ngha mt lung ca ring
n thc hin cc cng vic nhm trnh tnh trng gin on cc cng vic ang thc thi
lung chnh.
Mt Service c th c s dng theo hai cch
o Mt Service c th c dng thc thi mt cng vic di nn m khng cn hin th giao
din ngi dng. Loi service ny c bt u v c cho php hot ng cho n khi mt
ngi no dng n li hoc n t ngt. ch ny, n c bt u bng cch gi
Context.startService() v dng bng lnh Context.stopService(). Mt Service c th t ngt
bng lnh Service.stopSelf() hoc Service.stopSelfResult(). Mi Service ch c mt th hin
duy nht, do ch cn mt lnh stopService() ngng mt Service li cho d lnh
startService() c gi ra bao nhiu ln.
o Mt Service cn c th c s dng cung cp mt tnh nng no cho ng dng khc kt
ni v s dng. Mt ng dng c th thit lp mt ng truyn ti i tng Service v s
dng ng kt ni iu khinService. Kt ni ny c thit lp bng cch gi lnh
Context.bindService() v c ng li bng cch gi lnh Context.unbindService(). Nhiu
ng dng c th kt ni ti cng mt i tng Service. Nu Service c mt ng dng khch
kt ni n vn cha c khi chy th lnh bindService() c th ty khi chy n.
Hai ch ny th khng tch bit ton b. Bn c th kt ni vi mt Service m n c
bt u vi lnh startService().
V d: Mt Service nghe nhc ch nn c th c bt u bng cch gi lnh
startService() cng vi mt i tng Intent xc nh bi ht cn chi. Sau , c th ngi s dng
mun kim sot trnh chi nhc hoc bit thm thng tin v bi ht hin ti ang chi, th s c mt
Activity to lp mt ng truyn ti Service bng cch gi bindService(). Trong trng hp ny,
stopService() s khng thc s ngng Service cho n khi lin kt cui cng c ng li.
S di y th hin vng i ca Service:
Pht trin ng dng Smartphone Android

.
Da vo lc trn ta c hai vng lp quan trng trong vng i ca mt i tng Service
cc b:
o Vng i ton din (entire lifetime):Bt u t lc gi phng thc onCreate() n lc
gi phng thc onDestroy(). Cng ging nh Activity, i tng Service s khi to
cc gi tr ti phng thc onCreate() v dn dp b nh ti phng thc onDestroy().
VD:Mt i tng Service dng chi nhc s to mt lung chi nhc phng
thc onCreate() v ngng lung chi nhc ny phng thc onDestroy() cng nh dn
dp b nh trc khi hy i tng.
o Vng i thc thi(active lifetime): Bt u t lc gi phng thc onStart().
VD: Phng thc onStart() s n i tng Intent c gi i khi khi ng Service
bng phng thc startService(). Vi i tng Service dng chi nhc ta s gi tn
bi ht cn pht trong i tng Intent.
Nu mt i tng Service cho php mt ng dng khc kt ni n n th n phi ci t 03
phng thc sau:
o IBinder onBind(Intent intent): Khi mt i tng mun to kt ni n mt i tng
Servicer th n s gi phng thc Context.bindService() v gi i mt i tng
Pht trin ng dng Smartphone Android

Intent. Phng thc onBind() s c gi s l yu cu kt ni ny. N s tr v cc
knh giao tip m i tng cn kt ni c th s dng tng tc vi Service.
o boolean onUnbind(Intent intent):Phng thc ny tng t nh phng thc onBind().
Tuy nhin n s c gi khi c mt i tng gi phng thc
Context.unbindService() ngt kt ni vi Service. Lc ny phng thc onUnbind()
s c gi s l yu cu ngt kt ni n Service.
o void onRebind(Intent intent):Phng thc ny c gi khi c mt i tng khch mi
mun kt ni n Service.
Nghe c v tht phc tp nn ta s tin hnh lm hai v d v cch s dng local service v
remote service hiu r cch thc hot ng ca chng.
1.1 Local Service
To project vi cc thng s nh sau:
Project name: LocalServiceExample
Build target: Android 2.3.3.
Application name: LocalServiceExample
Package name: niit.android
Create Activity: main
Thm tp BackgroundService.java vi ni dung nh sau:
package niit.android;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

publicclass BackgroundService extends Service{

private NotificationManager notificationMgr;
private ServiceThread serviceThread;

@Override
publicvoid onCreate() {
super.onCreate();

Pht trin ng dng Smartphone Android

notificationMgr
=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

displayNotificationMessage("Starting Background Service");

serviceThread = new ServiceThread();
serviceThread.start();

}
@Override
publicvoid onStart(Intent intent, int startId) {
super.onStart(intent, startId);

}

@Override
public IBinder onBind(Intent intent) {
returnnull;
}

@Override
publicvoid onDestroy()
{
displayNotificationMessage("Stopping Background Service");
super.onDestroy();

}

privatevoid displayNotificationMessage(String message)
{
Notification notification = new
Notification(R.drawable.icon,message,System.currentTimeMillis())
;

PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, new Intent(this, main.class),
0);

notification.setLatestEventInfo(this, "Background
Service",message,contentIntent);
notificationMgr.notify(123456, notification);
}

class ServiceThread extends Thread
{
@Override
publicvoid run() {
Pht trin ng dng Smartphone Android

// Thc thi cc tc v mong mun.
}
}
}
Thay i tp main.xml vi ni dung nh sau:
<?xmlversion="1.0"encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/a
ndroid"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start BackgroupService"
/>

<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop BackgroupService"
/>
</LinearLayout>
Thay i tp main.java vi ni dung nh sau:
package niit.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

publicclass main extends Activity implements OnClickListener{
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button bindBtn = (Button)findViewById(R.id.startBtn);
Pht trin ng dng Smartphone Android

bindBtn.setOnClickListener(this);

Button unbindBtn = (Button)findViewById(R.id.stopBtn);
unbindBtn.setOnClickListener(this);

}

@Override
publicvoid onClick(View v) {
switch (v.getId()) {
case R.id.startBtn:
{
Intent intent = new
Intent(this,BackgroundService.class);
startService(intent);
}
break;
case R.id.stopBtn:
{
Intent intent = new
Intent(this,BackgroundService.class);
stopService(intent);
}
break;

default:
break;
}
}
}
B sung khai bo <service> trn tp AndroidManifest.xml vi ni dung nh sau:
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/andro
id"
package="niit.android"
android:versionCode="1"
android:versionName="1.0">
<applicationandroid:icon="@drawable/icon"android:label="@st
ring/app_name">
<activity
android:name=".main"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.M
AIN"/>
Pht trin ng dng Smartphone Android

<categoryandroid:name="android.intent.catego
ry.LAUNCHER"/>
</intent-filter>
</activity>
<serviceandroid:name="BackgroundService"/>
</application>
</manifest>
Lp BackgroundService l mt lp m rng lp Service ca Android. Trn lp ny ta tin hnh
ghi cc phng thc onCreate(), onStart(), onBind() s l cc s kin lin quan n vng i ca
Service. Nh trnh by phn nh ngha, mt Service chy trn lung chnh ca tin trnh m ng
dng ang chy. Do ta cn xy dng thm mt lung thc thi cc cng vic mong mun nu
khng mun cc tc v ny lm cn tr cng vic ang thc thi lung chnh. Do , trn lp
BackgroundService ta nh lpServiceThread m rng Thread ca Android thc thi cc tc v mong
mun. y phng thc run ca ServiceThread trng, bn c th b sung cc cu lnh thc thi
mt tc v no nh: chi nhc, ti d liu cn thc thi.Ta s tin hnh phn tch lp
BackgroundService thy r cch thc hot ng ca n.
Trn h iu hnh Android h tr mt khi nim Notification cho php mt ng dng gi mt
thng bo n ngi dng thng qua mt on text hin th trn thanh trng thi ca thit b.
VD: Hnh sau th hin mt thng bo c a ra khi c tin nhn c gi n thit b:

Pht trin ng dng Smartphone Android

Ta s s dng hnh thc thng bo ny hin cc thng bo v s thay i trng thi ca
BackgroupService. c th a mt thng bo ln thanh trng thi ta s s dng mt i tng kiu
NotificationManager. Ta c th ly i tng ny bng lnh:
notificationMgr=(NotificationManager)getSystemService(NOTIFICATI
ON_SERVICE);
Gi mt thng bo ln thanh trng thi ca thit b bng cc cu lnh sau:
Notification notification = new
Notification(R.drawable.icon,message,System.currentTimeMillis())
;

PendingIntent contentIntent = PendingIntent.getActivity(this,
0, new Intent(this, main.class), 0);

notification.setLatestEventInfo(this, "Background
Service",message,contentIntent);
notificationMgr.notify(123456, notification);
u tin ta khi to mt i tng notification kiu Notification vi ba thng s:
o Biu tng hin th bn cnh thng bo.
o Chui k t s hin th trn thanh trng thi.
o Thi gian hin th chui k t.
Tip theo ta khi to mt i tng contentIntent kiu PendingIntent. Khi nim ny c
nhc n phn Network ca ti liu ny. i tng PendingIntent ch mt hnh ng s c thc thi
mt thi im trong tng lai. i tng PendingIntent c to bng phng thc
PendingIntent.getActivity()dng to mt PendingIntent dng khi ng mt Activity. Activity ny
s c khi ng bng i tng new Intent(this, main.class) ch vic s khi ng Activity tn main.
Bn c th thay i tn Activity ph hp vi bn thn.
Mi loi thng bo (Thng bo tin nhn mi, mail mi) c th c nhiu thng bo to cc
thi im khc nhau. Tuy nhin khi hin th trn thanh trng thi tit kim khng gian th cng mt
loi thng bo ch hin th thng bo mi nht. Ta s dng phng thc
Notification.setLatestEventInfo() gn thng bo mi nht cho i tng Notification. Phng
thc ny nhn vo 03 tham s ln lt l:
o i tng Context.
o Tiu ca thng bo.
o i tng kiu PendingIntent dng thc thi mt hnh vi khi ngi dng nhn vo
thng bo trang xem chi tit thng bo.
Sau ta a thng bo ln thanh trng thi bng cch gi lnh:
Pht trin ng dng Smartphone Android

notificationMgr.notify(123456, notification);
Phng thc notify() nhn vo hai tham s l id phn bit loi (Gi hai thng bo cng id i
th ch thng bo sau mi c hin th ln thanh trng thi) thng bo v i tng notification cha
thng bo.
T ta dng ln phng thc displayNotificationMessage() dng hin th mt thng bo
v trng thi hin ti ca Service ln thanh trng thi ca thit b. Ta gi phng thc ny onCreate()
v onDestroy() hin thng bo v trng thi ca Service.
Trn Activity main ta c hai nt nhn: mt khi ng service v mt dng service.
1.2 Remote Service
Phn trn ta cp n local service nh l mt dch v lm nhim v thc thi mt tc v
bn di nn v khng c giao din hin th ni dung n ngi dng. Do , local service c khi
ng trn cng tin trnh vi tin trnh ca ng dng khi ng n. Vng i ca mt i tng local
service ph thuc vo vng i ca ng dng. Khc vi local service, remote service chy trn tin
trnh ca ring n. T y xut hin kh khn trong vic giao tip gia cc tin trnh vi nhau. Nu mt
tin trnh mun gi mt i tng n tin trnh khc th ta phi bt mt cy cu ni gia hai tin trnh.
Vi mc ch ny, Android cung cp khi nim AIDL (Android Interface Definition Language) h tr
vic bt cu ni gia hai tin trnh cng nh giao tip gia chng. Ta cn to mt AIDL dng nh
ngha mt giao din lp trnh (interface) cung cp cho ng dng khch. Bng cch nhn ly giao din
lp trnh ny, ng dng khch s bit c cc phng thc m remote service cung cp. Khi ng dng
khch kt ni n remote service, remote service s tr v mt i tng ci t giao din lp trnh
k trn, ng dng khch s dng i tng ny thc thi cc phng thc cn thit. Ta s ln lt tin
hnh nh ngha remote service v ng dng khch thy c cch hot ng ca remote service:
1.2.1 Hin thc remote service
hin thc mt remote service ta thc hin cc bc sau:
1. Vit mt tp tin AIDL dng khai bo giao din lp trnh(interface) cho ng dng khch.
Mt tp tin AIDL s dng c ph java v c phn m rng aidl. S dng cng package vi
package ca ng dng.
2. To mi mt project Android v chp tp .aidl vo th mc scr. Eclise s t ng gi trnh
bin dch AIDL bin dch tp tin .aidl v pht sinh ra mt giao din lp trnh (interface).
3. Ci t mt Service v tr v mt i tng ci t giao din lp trnh k trn trong phng
thc onBind().
4. B xung khai bo service trong tp AndroidManifest.xml ca project.
Pht trin ng dng Smartphone Android

1.2.1.1 To project Android
To mi mt project vi cc thng s sau:
Project name: RemoteServiceExample
Build target: Android 2.3.3.
Application name: RemoteServiceExample
Package name: niit.android.remoteservice
1.2.1.2 To AIDL
Son tp tin IStockQuoteService.aidl vi ni dung nh sau:
package niit.android.remoteservice;
interface IStockQuoteService
{
double getQuote(String ticker);
}
Ghi ch: Package ca tp tin k trn chnh l package ca project m ta s nh ngha Service.
Tp tin ny khai bo mt giao din lp trnh tn l IstockQuoteService ch c mt phng thc
duy nht l double getQuote(String ticker). Phng thc ny s s nhn vo tn m chng khon v tr
v gi tr hin ti ca n.
Ko th tp tin IstockQuoteService.aidl b vo th mc src ca project hin ti. Sau khi ko th
vo project, Eclipse s t ng gi trnh bin dch AIDL bin dch tp tin trn v to ra mt giao
din lp trnh tn l IStockQuoteService t trong th mc gen/[package] ca project. Trong giao din
IstockQuoteService c mt lp tn l Stub extends android.os.Binder v ci t giao din lp trnh
IstockQuoteService. Do Stub m rng lp android.os.Binder nn mi i tng c kiu ny c th
c iu khin t xa bi mt ng dng khc, v do lp Stub ny ci t li giao din lp trnh
IstockQuoteService nn n c nhng phng thc khai bo trong giao din lp trnh ny. Lp Stub l
mt lp tru tng nn ta phi to mt lp ci t li lp ny v dng n cung cp cho ng dng
khch.
1.2.1.3 To Service
To mt tp IstockQuoteService.java vi ni dung sau:
package niit.android.remoteservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
Pht trin ng dng Smartphone Android

import android.os.RemoteException;

public class StockQuoteService extends Service
{
public class StockQuoteServiceImpl extends
IStockQuoteService.Stub
{
@Override
public double getQuote(String ticker) throws
RemoteException
{
return 20.0;
}
}

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onDestroy()
{
super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}

@Override
public IBinder onBind(Intent intent)
{
return new StockQuoteServiceImpl();
}
}
Lp trn nh ngha mt service c tn l IstockQuoteService, trong c mt lp ni
StockQuoteServiceImpl k tha IStockQuoteService.Stub. Trong lp ni ny ci t li cc phng
thc khai bo trong tp .aidl k trn.
Khi mt ng dng kt ni n IStockQuoteService th n s gi phng thc onBind() v tr v
mt i tng kiu StockQuoteServiceIml. ng dng khch s s dng i tng c tr v
onBind() gi cc phng thc cn thit. Cc phng thc ny s c IStockQuoteService thc thi.
Lu : B xung khai bo service trong tp AndroidManifest.xml.
Pht trin ng dng Smartphone Android

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="niit.android.remoteservice"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<service android:name="StockQuoteService">
<intent-filter>
<action
android:name="niit.android.remoteservice.IStockQuoteService" />
</intent-filter>
</service>
<activity
android:name=".NotificationActivity"
></activity>
</application>
</manifest>
Thc thi chng trnh Android khi ng IStockQuoteService.
1.2.2 Hin thc ng dng khch
1.2.2.1 To project Android
To mi mt project vi cc thng s sau:
Project name: RemoteServiceClientExample
Build target: Android 2.3.3.
Application name: RemoteServiceClientExample
Package name: niit.android.remoteserviceclient
Create Activity: main
1.2.2.2 Chp tp tin IStockQuoteService.aidl vo project
To package tn remoteservice trong th mc scr/niit/android/ ca project v chp tp tin
IStockQuoteService vo package mi to. Eclipse s t ng gi trnh bin dch AIDL bin dch tp
tin IStockQuoteService.aidl thnh giao din lp trnh(interface) ging ht nh giao din lp trnh
c to ra project trn.
Pht trin ng dng Smartphone Android

1.2.2.3 Thay i activity main
Thay i tp main.xml vi ni dung sau:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/bindBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Bind"/>

<Button android:id="@+id/callBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Call Again"
/>

<Button android:id="@+id/unbindBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="UnBind"
/>
</LinearLayout>
Thay i tp tin main.java vi ni dung sau:
package niit.android.remoteserviceclient;

// This file is MainActivity.java
import niit.android.remoteservice.IStockQuoteService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
Pht trin ng dng Smartphone Android


public class main extends Activity {

protected static final String TAG = "StockQuoteClient";
private IStockQuoteService stockService = null;

private Button bindBtn;
private Button callBtn;
private Button unbindBtn;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

bindBtn = (Button)findViewById(R.id.bindBtn);
bindBtn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view) {
bindService(new Intent(IStockQuoteService.class
.getName()),
serConn, Context.BIND_AUTO_CREATE);
bindBtn.setEnabled(false);
callBtn.setEnabled(true);
unbindBtn.setEnabled(true);
}});

callBtn = (Button)findViewById(R.id.callBtn);
callBtn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view) {
callService();
}});

callBtn.setEnabled(false);

unbindBtn = (Button)findViewById(R.id.unbindBtn);
unbindBtn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View view) {
unbindService(serConn);
bindBtn.setEnabled(true);
callBtn.setEnabled(false);
Pht trin ng dng Smartphone Android

unbindBtn.setEnabled(false);
}});
unbindBtn.setEnabled(false);
}

private void callService() {
try {
double val = stockService.getQuote("SYH");
Toast.makeText(main.this, "Value from service is
"+val,
Toast.LENGTH_SHORT).show();
} catch (RemoteException ee) {
Log.e("MainActivity", ee.getMessage(), ee);
}
}

private ServiceConnection serConn = new ServiceConnection()
{

@Override
public void onServiceConnected(ComponentName name,
IBinder service)
{
Log.v(TAG, "onServiceConnected() called");
stockService =
IStockQuoteService.Stub.asInterface(service);
callService();
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "onServiceDisconnected() called");
stockService = null;
}
};
}
Trn ta c ba nt nhn dng kt ni service (Bind), gi phng thc trn remote service
(Call Again) v ngt kt ni n service(UnBind).
kt ni n mt service ta s dng phng thc bindService() v truyn vo 03 tham s:
- i tng Intent xc nh service cn kt ni.
- i tng s l s kin service kt ni thnh cng v service ngt kt ni.
- Bin c nhn ba gi tr:
o BIND_AUTO_CREATE: T khi to service nu n cha tn ti.
Pht trin ng dng Smartphone Android

o BIND_DEBUG_UNBIND: Chy di ch g ri. Mi li gi ngt kt ni n
service s c lu li. Khi c mt li gi yu cu ngt kt ni n service b sai th
in ra li gi ng trc . Vic ny gy tn kh nhiu ti nguyn nn ch s dng
g ri.
o BIND_NOT_FOREGROUND: [Link]
Ta tin hnh khai bo tt mt i tng serConn ci t giao din lp trnh ServiceConnection
bng lnh:
private ServiceConnection serConn = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder
service)
{
Log.v(TAG, "onServiceConnected() called");
stockService =
IStockQuoteService.Stub.asInterface(service);
callService();
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "onServiceDisconnected() called");
stockService = null;
}
};
i tng serConn c hai phng thc:
o onServiceConnected: Phng thc ny s c gi khi kt ni thnh cng n service.
Phng thc ny s nhn vo i tng IBinder service. y l i tng c tr v
trn phng thc onBind() ca service khai bo trn. Ta s dng i tng service ny
to thnh mt i tng stockService dng gi cc phng thc c khai bo
trn service.
o onServiceDisconnected: Phng thc ny s c gi khi kt ni n service ngt.
Ta s truyn serConn vo phng thc bindService() thng bo rng i tng serConn s l
i tng nhn cc thng bo khi kt ni c n remote service v ngt kt ni vi service. Ta
cng s dng serConn ngt kt ni n service bng cch gi phng thc unbindService() v
truyn vo serConn.
2 Tham kho
http://saigeethamn.blogspot.com/2009/09/android-developer-tutorial-part-9.html

You might also like