android服務unbind之後再想綁定問題

來源:互聯網
上載者:User

標籤:android   des   style   http   java   color   


突然遇到個問題, 問題描述:

我按照順序來綁定一個服務:start->bind 最後在退出activity的時候unbind一下, 現在我有這樣的業務需求,就是當我再次進入該activity時需要再次bind, 我發現再調用bind方法並不能綁定服務(不知道google工程師為啥要設計成這樣。)


寫了一段測試代碼驗證一下:

Service:

package org.load.testservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class MyService extends Service {@Overridepublic IBinder onBind(Intent intent) {System.out.println("bind....");return null;}@Overridepublic void onCreate() {System.out.println("oncreate....");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("onstart....");return super.onStartCommand(intent, flags, startId);}@Overridepublic boolean onUnbind(Intent intent) {System.out.println("unbind....");return super.onUnbind(intent);}@Overridepublic void onDestroy() {System.out.println("destroy...");super.onDestroy();}}


activity:

package org.load.testservice;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.view.View;public class MainActivity extends Activity {private MyConn conn = new MyConn();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void startService(View view) {startService(new Intent(this, MyService.class));}public void bindService(View view) {bindService(new Intent(this, MyService.class), conn, Context.BIND_AUTO_CREATE);}public void unbindService(View view) {unbindService(conn);}private class MyConn implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {}@Overridepublic void onServiceDisconnected(ComponentName name) {}}}


 
第一次按照順序點擊按鈕發現沒多大問題, 服務依次進行了create、start、bind、unbind。

但是當我再次點擊“綁定服務”的時候問題出現了onBind方法沒有執行!!也就是說沒有成功綁定!!!

查看文檔看到Service還有一個生命週期方法onRebind,好像看到希望了, 添加上再試試:

       

 @Overridepublic void onRebind(Intent intent) {System.out.println("rebind...");super.onRebind(intent);}

懷著激動心情再次測試,logcat遺憾的告訴我還是不行!!!!

仔細查看onRebind的官方說明:

Called when new clients have connected to the service, after it had previously been notified that all had disconnected in its onUnbind.

 This will only be called if the implementation of onUnbind was overridden to return true.

最重要的是最後一句話,意思是:這個方法只有當onUnbind返回true的時候才能被調用。

現在改寫onUnbind:

       

 @Overridepublic boolean onUnbind(Intent intent) {System.out.println("unbind....");return true;}

再次查看logcat, 別的不多說了,:



總結:

多次bind、unbind一個服務,只有在第一次的時候才會調用服務的onBind方法, 解除綁定後繼續綁定並不會調用onBind而是調用了onRebind, 但是要想讓onRebind順利的調用還有一個條件就是onUnbind必須返回true。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.