通過startService開啟的服務(wù),當(dāng)訪問者關(guān)閉時,服務(wù)仍然存在;訪問者需要與服務(wù)進(jìn)行通信,則我們需要將訪問者與服務(wù)進(jìn)行綁定;
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、天壇街道網(wǎng)絡(luò)推廣、小程序開發(fā)、天壇街道網(wǎng)絡(luò)營銷、天壇街道企業(yè)策劃、天壇街道品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供天壇街道建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:chinadenli.net
如果使用Context.bindService()方法啟動服務(wù),則在服務(wù)未創(chuàng)建時,系統(tǒng)會調(diào)用服務(wù)的onCreate()方法,接著調(diào)用onBind()方法,這時就訪問者與服務(wù)已經(jīng)綁定了,主程序銷毀時服務(wù)也會終止。
1)綁定服務(wù)時,會自動創(chuàng)建服務(wù)。
2)如果創(chuàng)建后并啟動后再綁定,不會重新創(chuàng)建,一個Service只有一個實(shí)例
3)同時啟動和綁定服務(wù)時,解除綁定服務(wù),但不會銷毀關(guān)閉服務(wù)的,必須解除綁定并停止服務(wù)。
4)通過StartService啟動服務(wù),當(dāng)前Activity銷毀,服務(wù)不會停止,通過BindService啟動服務(wù),當(dāng)前Activity銷毀,服務(wù)停止。
綁定與解綁定服務(wù)
(1)Context.bindService(Intent service,ServiceConnectionconn,BIND_AUTO_CREATE);//綁定服務(wù)
(2)Context.unbindService(ServiceConnectionconn);
ServiceConnection
ServiceConnection為一個接口,用于綁定和解綁定IBinder,因此需要創(chuàng)建一個類實(shí)現(xiàn)它;
class XxxServiceConnectionimplements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//service為在onBind返回的IBinder
//綁定Binder對象
}
@Override
public void onServiceDisconnected(ComponentName name) {
//解綁定Binder對象
}
}
Service類
class XxxService extendsService{
private IBinder binder = new
XxxBinder();
public IBinder onBind(Intent intent){
return binder;
}
public int fun(int a){
//服務(wù)提供的方法,但是不能直接調(diào)用
...
}
private class XxxBinderextends Binder implements IXxxBinder{
//面向接口編程
public return fun1(int a){
//對外暴露的API
return fun(a);
//內(nèi)部調(diào)用Service的方法
}
}
}
案例:綁定服務(wù)
主要功能:Service實(shí)現(xiàn)不斷輸出1、2、3……的服務(wù)功能,Activity調(diào)用Service的公開方法,調(diào)出當(dāng)時的一個值。繼續(xù)上次服務(wù)的案例,增加綁定等功能。
打開activity_main.xml,添加兩個命令按鈕,綁定服務(wù)和解除綁定:
<Button
android:id="@+id/btnBindService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="綁定服務(wù)" />
<Button
android:id="@+id/btnUnbindService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解除綁定" />
2) MainActivity.java,添加相應(yīng)代碼:
定義兩按鈕:
private ButtonbtnBindService,btnUnbindService;
通過查找得到這兩個按鈕:
btnBindService=(Button)findViewById(R.id.btnBindService); btnUnbindService=(Button)findViewById(R.id.btnUnbindService);
3)添加事件偵聽器:
btnBindService.setOnClickListener(this);
btnUnbindService.setOnClickListener(this);
4)在onClick中添加判斷分支:
case R.id.btnBindService: bindService(serviceIntent,this,Context.BIND_AUTO_CREATE);
break;
case R.id.btnUnbindService:
unbindService(this);
break;
需要當(dāng)前類實(shí)現(xiàn)ServiceConnection,加進(jìn)繼承ServiceConnection:
publicclass MainActivityextends ActionBarActivityimplementsOnClickListener, ServiceConnection {
同時產(chǎn)生了兩接口方法,
成功綁定的方法:
@Override
publicvoid onServiceConnected(ComponentName arg0, IBinder bind) {
//TODO Auto-generCatedmethod stub
System.out.println("onServiceConnected");
}
解除綁定或Service崩潰時
@Override
publicvoid onServiceDisconnected(ComponentName name) {
//TODO Auto-generatedmethod stub
System.out.println("onServiceDisconnected");
}
5)onBind要指定返回值,否則綁定時,其實(shí)沒有真正綁定,onServiceConnected不會執(zhí)行
定義內(nèi)部類MyServiceBinder擴(kuò)展自Binder:
publicclass MyServiceBinderextends Binder{
public MyServicegetService()
{
return MyService.this;//取得服務(wù)的實(shí)例
}
}
定義myservicebinder,并返回:
private final MyServiceBindermyservicebinder=new MyServiceBinder();
public IBinder onBind(Intent arg0) {
//TODO Auto-generatedmethod stub
System.out.println("onBind");
returnmyservicebinder;
}
6)服務(wù)內(nèi)添加一輸出:
privateinti=0;
publicvoid startTimer(){
if(timer==null){
timer=new Timer();
task=new TimerTask(){
@Override
publicvoid run(){
i++;
System.out.println(i);
}
};
timer.schedule(task,1000,1000);
}
}
publicvoid stopTimer(){
if(timer!=null)
{
task.cancel();
timer.cancel();
task=null;
timer=null;
}
}
private Timertimer=null;
private TimerTasktask=null;
其中,每一秒鐘執(zhí)行:
timer.schedule(task,1000,1000);
7)onCreate、onDestroy添加startTimer、stopTimer:
publicvoid onCreate(){
System.out.println("創(chuàng)建好了");
startTimer();
super.onCreate();
}
@Override
publicvoid onDestroy(){
System.out.println("被銷毀了");
stopTimer();
super.onDestroy();
}
8)取得服務(wù)實(shí)例:
publicclass MyServiceBinderextends Binder{
public MyService getService()
{
return MyService.this;//取得服務(wù)的實(shí)例
}
}
9)公開一個方法,取得服務(wù)內(nèi)部的數(shù)字(狀態(tài)):
publicint getCurrentNum()
{
returni;
}
10)回到主Activity,取得服務(wù)
定義變量:
private MyService myService=null;
取得實(shí)例:
publicvoid onServiceConnected(ComponentNamearg0, IBinder bind) {
//TODO Auto-generCatedmethod stub
System.out.println("onServiceConnected");
myService=((MyService.MyServiceBinder) bind).getService();
}
11)添加按鈕
<Button
android:id="@+id/btnGetCurrentNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GetCurrentNum" />
定義按鈕:
private ButtonbtnGetCurrentNumber;
取得:
btnGetCurrentNumber=(Button)findViewById(R.id.btnGetCurrentNumber);
事件:
btnGetCurrentNumber.setOnClickListener(this);
實(shí)現(xiàn):
case R.id.btnGetCurrentNumber:
if(myService!=null)
{
System.out.println("當(dāng)前服務(wù)中的數(shù)字是"+myService.getCurrentNum());
}
break;
文獻(xiàn)參考:
http://blog.csdn.net/xiazdong/article/details/7772914
http://www.cnblogs.com/andriod-html5/archive/2012/02/28/2539457.html
http://blog.163.com/cazwxy_12/blog/static/898763720122106483898/
本文標(biāo)題:10天學(xué)通Android開發(fā)(2-3)-核心組件Service綁定
網(wǎng)站地址:http://chinadenli.net/article30/gioiso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站導(dǎo)航、網(wǎng)站策劃、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站建設(shè)、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)