通過startService開啟的服務(wù),當(dāng)訪問者關(guān)閉時(shí),服務(wù)仍然存在;訪問者需要與服務(wù)進(jìn)行通信,則我們需要將訪問者與服務(wù)進(jìn)行綁定;
如果使用Context.bindService()方法啟動(dòng)服務(wù),則在服務(wù)未創(chuàng)建時(shí),系統(tǒng)會(huì)調(diào)用服務(wù)的onCreate()方法,接著調(diào)用onBind()方法,這時(shí)就訪問者與服務(wù)已經(jīng)綁定了,主程序銷毀時(shí)服務(wù)也會(huì)終止。
1)綁定服務(wù)時(shí),會(huì)自動(dòng)創(chuàng)建服務(wù)。
2)如果創(chuàng)建后并啟動(dòng)后再綁定,不會(huì)重新創(chuàng)建,一個(gè)Service只有一個(gè)實(shí)例
3)同時(shí)啟動(dòng)和綁定服務(wù)時(shí),解除綁定服務(wù),但不會(huì)銷毀關(guān)閉服務(wù)的,必須解除綁定并停止服務(wù)。
4)通過StartService啟動(dòng)服務(wù),當(dāng)前Activity銷毀,服務(wù)不會(huì)停止,通過BindService啟動(dòng)服務(wù),當(dāng)前Activity銷毀,服務(wù)停止。
綁定與解綁定服務(wù)
(1)Context.bindService(Intent service,ServiceConnectionconn,BIND_AUTO_CREATE);//綁定服務(wù)
(2)Context.unbindService(ServiceConnectionconn);
ServiceConnection
ServiceConnection為一個(gè)接口,用于綁定和解綁定IBinder,因此需要?jiǎng)?chuàng)建一個(gè)類實(shí)現(xiàn)它;
class XxxServiceConnectionimplements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {
//service為在onBind返回的IBinder//綁定Binder對(duì)象}@Overridepublic void onServiceDisconnected(ComponentName name) {//解綁定Binder對(duì)象}}
Service類
class XxxService extendsService{
private IBinder binder = new
XxxBinder();
public IBinderonBind(Intent intent){
return binder;
}
public int fun(int a){
//服務(wù)提供的方法,但是不能直接調(diào)用
...
private class XxxBinderextends Binder implements IXxxBinder{
//面向接口編程
public return fun1(int a){
//對(duì)外暴露的API
returnfun(a);
//內(nèi)部調(diào)用Service的方法
案例:綁定服務(wù)
主要功能:Service實(shí)現(xiàn)不斷輸出1、2、3……的服務(wù)功能,Activity調(diào)用Service的公開方法,調(diào)出當(dāng)時(shí)的一個(gè)值。繼續(xù)上次服務(wù)的案例,增加綁定等功能。
打開activity_main.xml,添加兩個(gè)命令按鈕,綁定服務(wù)和解除綁定:
android:id="@+id/btnBindService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="綁定服務(wù)" /> android:id="@+id/btnUnbindService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解除綁定" />2) MainActivity.java,添加相應(yīng)代碼:定義兩按鈕: private Button btnBindService,btnUnbindService;通過查找得到這兩個(gè)按鈕: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 MainActivity extends ActionBarActivity implementsOnClickListener, ServiceConnection {同時(shí)產(chǎn)生了兩接口方法,成功綁定的方法:@Override publicvoid onServiceConnected(ComponentName arg0, IBinder bind) { // TODO Auto-generCatedmethod stub System.out.println("onServiceConnected"); }解除綁定或Service崩潰時(shí) @Override publicvoid onServiceDisconnected(ComponentName name) { // TODO Auto-generatedmethod stub System.out.println("onServiceDisconnected"); }5)onBind要指定返回值,否則綁定時(shí),其實(shí)沒有真正綁定,onServiceConnected不會(huì)執(zhí)行定義內(nèi)部類MyServiceBinder擴(kuò)展自Binder:publicclass MyServiceBinder extends 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 Timer timer=null; private TimerTask task=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 MyServiceBinder extends Binder{ public MyService getService() { return MyService.this;//取得服務(wù)的實(shí)例 } }9)公開一個(gè)方法,取得服務(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)添加按鈕android:id="@+id/btnGetCurrentNumber"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="GetCurrentNum" />定義按鈕:private Button btnGetCurrentNumber;取得: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/7772914http://www.cnblogs.com/andriod-html5/archive/2012/02/28/2539457.htmlhttp://blog.163.com/cazwxy_12/blog/static/898763720122106483898/另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。 網(wǎng)站標(biāo)題:10天學(xué)通Android開發(fā)(2-3)-核心組件Service綁定-創(chuàng)新互聯(lián) 文章網(wǎng)址:http://weahome.cn/article/djpspo.html
android:id="@+id/btnBindService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="綁定服務(wù)" />
android:id="@+id/btnUnbindService" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解除綁定" />2) MainActivity.java,添加相應(yīng)代碼:定義兩按鈕: private Button btnBindService,btnUnbindService;通過查找得到這兩個(gè)按鈕: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 MainActivity extends ActionBarActivity implementsOnClickListener, ServiceConnection {同時(shí)產(chǎn)生了兩接口方法,成功綁定的方法:@Override publicvoid onServiceConnected(ComponentName arg0, IBinder bind) { // TODO Auto-generCatedmethod stub System.out.println("onServiceConnected"); }解除綁定或Service崩潰時(shí) @Override publicvoid onServiceDisconnected(ComponentName name) { // TODO Auto-generatedmethod stub System.out.println("onServiceDisconnected"); }5)onBind要指定返回值,否則綁定時(shí),其實(shí)沒有真正綁定,onServiceConnected不會(huì)執(zhí)行定義內(nèi)部類MyServiceBinder擴(kuò)展自Binder:publicclass MyServiceBinder extends 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 Timer timer=null; private TimerTask task=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 MyServiceBinder extends Binder{ public MyService getService() { return MyService.this;//取得服務(wù)的實(shí)例 } }9)公開一個(gè)方法,取得服務(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)添加按鈕android:id="@+id/btnGetCurrentNumber"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="GetCurrentNum" />定義按鈕:private Button btnGetCurrentNumber;取得: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/7772914http://www.cnblogs.com/andriod-html5/archive/2012/02/28/2539457.htmlhttp://blog.163.com/cazwxy_12/blog/static/898763720122106483898/另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。 網(wǎng)站標(biāo)題:10天學(xué)通Android開發(fā)(2-3)-核心組件Service綁定-創(chuàng)新互聯(lián) 文章網(wǎng)址:http://weahome.cn/article/djpspo.html
android:id="@+id/btnUnbindService"
android:text="解除綁定" />
2) MainActivity.java,添加相應(yīng)代碼:
定義兩按鈕:
private Button btnBindService,btnUnbindService;
通過查找得到這兩個(gè)按鈕:
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);
需要當(dāng)前類實(shí)現(xiàn)ServiceConnection,加進(jìn)繼承ServiceConnection:
publicclass MainActivity extends ActionBarActivity implementsOnClickListener, ServiceConnection {
同時(shí)產(chǎn)生了兩接口方法,
成功綁定的方法:
@Override
publicvoid onServiceConnected(ComponentName arg0, IBinder bind) {
// TODO Auto-generCatedmethod stub
System.out.println("onServiceConnected");
解除綁定或Service崩潰時(shí)
publicvoid onServiceDisconnected(ComponentName name) {
// TODO Auto-generatedmethod stub
System.out.println("onServiceDisconnected");
5)onBind要指定返回值,否則綁定時(shí),其實(shí)沒有真正綁定,onServiceConnected不會(huì)執(zhí)行
定義內(nèi)部類MyServiceBinder擴(kuò)展自Binder:
publicclass MyServiceBinder extends Binder{
public MyServicegetService()
{
return MyService.this;//取得服務(wù)的實(shí)例
定義myservicebinder,并返回:
private final MyServiceBindermyservicebinder=new MyServiceBinder();
public IBinder onBind(Intent arg0) {
System.out.println("onBind");
returnmyservicebinder;
6)服務(wù)內(nèi)添加一輸出:
privateinti=0;
publicvoid startTimer(){
if(timer==null){
timer=new Timer();
task=new TimerTask(){
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 Timer timer=null;
private TimerTask task=null;
其中,每一秒鐘執(zhí)行:
7)onCreate、onDestroy添加startTimer、stopTimer:
publicvoid onCreate(){
System.out.println("創(chuàng)建好了");
startTimer();
super.onCreate();
publicvoid onDestroy(){
System.out.println("被銷毀了");
stopTimer();
super.onDestroy();
8)取得服務(wù)實(shí)例:
public MyService getService()
9)公開一個(gè)方法,取得服務(wù)內(nèi)部的數(shù)字(狀態(tài)):
publicint getCurrentNum()
returni;
10)回到主Activity,取得服務(wù)
定義變量:
private MyService myService=null;
取得實(shí)例:
publicvoid onServiceConnected(ComponentNamearg0, IBinder bind) {
myService=((MyService.MyServiceBinder) bind).getService();
11)添加按鈕
android:id="@+id/btnGetCurrentNumber"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="GetCurrentNum" />定義按鈕:private Button btnGetCurrentNumber;取得: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/7772914http://www.cnblogs.com/andriod-html5/archive/2012/02/28/2539457.htmlhttp://blog.163.com/cazwxy_12/blog/static/898763720122106483898/另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。 網(wǎng)站標(biāo)題:10天學(xué)通Android開發(fā)(2-3)-核心組件Service綁定-創(chuàng)新互聯(lián) 文章網(wǎng)址:http://weahome.cn/article/djpspo.html
android:id="@+id/btnGetCurrentNumber"
android:text="GetCurrentNum" />
定義按鈕:
private Button btnGetCurrentNumber;
取得:
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());
文獻(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/
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
全國(guó)免費(fèi)咨詢:
業(yè)務(wù)咨詢:028-86922220 / 13518219792
節(jié)假值班:18980820575 / 13518219792
聯(lián)系地址:成都市太升南路288號(hào)錦天國(guó)際A幢1002號(hào)
在線咨詢
微信咨詢
電話咨詢
028-86922220(工作日)
18980820575(7×24)
提交需求
返回頂部