編輯:關於 Android 程式設計
前面我們已經了解了以 startService 方式啟動服務的用法,現在來看 Service 的另一種啟動方式——bindService。
bindService 的使用場景:
1、在同一個 App 內呼叫(即同一個行程中)
2、在不同 App 之間呼叫(即跨行程通訊)
同一個 App 內呼叫(只啟動該服務一次)
BinderActicityA
public class BinderActicityA extends Activity implements View.OnClickListener { private Button btn1; private Button btn2; private Button btn3; private Button btn4; private BindService bindService = null; private boolean isBound = false; private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { isBound = true; BindService.MyBinder binder = (BindService.MyBinder) service; bindService = binder.getService(); int num = bindService.getRandomNumber(); Log.v("hjz","numA="+num); } //client 和service連接意外丟失時,會調用該方法 @Override public void onServiceDisconnected(ComponentName name) { Log.v("hjz","onServiceDisconnected A"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_binder_main); findLayoutView(); setLister(); initData(); } private void findLayoutView() { btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); btn4 = (Button) findViewById(R.id.btn4); } private void setLister() { btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); } private void initData() { } @Override public void onClick(View v) { Intent intent = null; switch (v.getId()){ case R.id.btn1: intent = new Intent(BinderActicityA.this, BindService.class); intent.putExtra("from", "ActivityA"); bindService(intent,conn,BIND_AUTO_CREATE); break; case R.id.btn2: if (isBound){ isBound = false; Log.v("hjz","ActicityA is unbindService"); unbindService(conn); } break; case R.id.btn3: intent = new Intent(this, BinderActivityB.class); startActivity(intent); break; case R.id.btn4: this.finish(); break; } } @Override protected void onDestroy() { super.onDestroy(); Log.i("hjz", "ActivityA -> onDestroy"); } }
xml
BinderService
public class BindService extends Service { public class MyBinder extends Binder{ public BindService getService(){ return BindService.this; } } //通過binder實現了 調用者(client)與 service之間的通信 private MyBinder binder = new MyBinder(); private final Random generator = new Random(); @Override public void onCreate() { super.onCreate(); Log.i("hjz","BindService -> onCreate, Thread: " + Thread.currentThread().getName()); } @Override public IBinder onBind(Intent intent) { Log.i("hjz", "BindService -> onBind, Thread: " + Thread.currentThread().getName()); return binder; } @Override public boolean onUnbind(Intent intent) { Log.i("hjz", "BindService -> onUnbind, from:" + intent.getStringExtra("from")); return false; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("hjz", "BindService -> onStartCommand, startId: " + startId + ", Thread: " + Thread.currentThread().getName()); return START_NOT_STICKY; } @Override public void onDestroy() { Log.i("hjz", "BindService -> onDestroy, Thread: " + Thread.currentThread().getName()); super.onDestroy(); } //在Service中暴露出去的方法,供client調用 public int getRandomNumber(){ return generator.nextInt(); } }
點擊 BinderService 綁定,再點擊 BinderService 解綁,最後點擊 finish,列印日誌:
07-09 11:46:49.360 29714-29714/com.hh.servicedemo I/hjz: BindService -> onCreate, Thread: main 07-09 11:46:49.360 29714-29714/com.hh.servicedemo I/hjz: BindService -> onBind, Thread: main 07-09 11:46:49.380 29714-29714/com.hh.servicedemo V/hjz: numA=1859239539 07-09 11:46:50.650 29714-29714/com.hh.servicedemo V/hjz: ActicityA is unbindService 07-09 11:46:50.650 29714-29714/com.hh.servicedemo I/hjz: BindService -> onUnbind, from:ActivityA 07-09 11:46:50.650 29714-29714/com.hh.servicedemo I/hjz: BindService -> onDestroy, Thread: main 07-09 11:46:52.290 29714-29714/com.hh.servicedemo I/hjz: ActivityA -> onDestroy
點擊 BinderService 綁定,再點擊 finish:
07-09 11:48:07.260 29714-29714/com.hh.servicedemo I/hjz: BindService -> onCreate, Thread: main 07-09 11:48:07.260 29714-29714/com.hh.servicedemo I/hjz: BindService -> onBind, Thread: main 07-09 11:48:07.270 29714-29714/com.hh.servicedemo V/hjz: numA=-341510490 07-09 11:48:09.290 29714-29714/com.hh.servicedemo I/hjz: ActivityA -> onDestroy 07-09 11:48:09.310 29714-29714/com.hh.servicedemo I/hjz: BindService -> onUnbind, from:ActivityA 07-09 11:48:09.310 29714-29714/com.hh.servicedemo I/hjz: BindService -> onDestroy, Thread: main先從 BinderService 來分析。從列印可以看出,先呼叫 onCreate,接著呼叫 onBind;你會發現,使用 bindService 啟動時,onStartCommand 並不會被呼叫。在 Service 中的執行順序如下:
1、先取得相應的 binder 物件。可以在 Service 內部用內部類取得 binder,如下:
public class MyBinder extends Binder{ public BindService getService(){ return BindService.this; } } //通過binder實現了 調用者(client)與 service之間的通信 private MyBinder binder = new MyBinder();
2、在 onBind 方法中返回 IBinder 實例。返回的實例可以是 Service 本身,也可以是透過 binder 暴露出的 Service 公共方法。通常情況下,我們會把 binder 做成 Service 的內部類,並在 binder 中建立一個類似 getService 的方法,返回包含 binder 的 Service 物件,如上面那種情況。如此一來,client(客戶端)便能透過該方法取得相應的 Service 實例。
接著是呼叫者(client 客戶端)的執行流程。
1、先建立 ServiceConnection 實例,並重寫其方法,如下:
//創建ServiceConnection類型的實例 private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { isBound = true; BindService.MyBinder binder = (BindService.MyBinder) service; bindService = binder.getService(); int num = bindService.getRandomNumber(); Log.v("hjz","numA="+num); } //client 和service連接意外丟失時,會調用該方法 @Override public void onServiceDisconnected(ComponentName name) { Log.v("hjz","onServiceDisconnected A"); } };
之所以需要這個物件,是因為 bindService 用到了它。看 bindService 源碼:
@Override public boolean bindService(Intent service, ServiceConnection conn, int flags) {//調用bindService,發現第二個傳參就是ServiceConnection對象 return mBase.bindService(service, conn, flags); }
2、當 onServiceConnected 回調執行時,我們可以透過 IBinder 實例取得 Service 物件,或直接呼叫 binder 的公共方法,如此便實現了 client 與 Service 的連接。
3、當 client 與 Service 解除綁定時,onServiceDisconnected 並不會被呼叫。onServiceDisconnected 被呼叫的情況,是發生在 client 與 Service 連接意外丟失時,此時兩者必定已斷開連接。
同一個 App 內呼叫(多次呼叫該服務)
BinderAcivityB
public class BinderActivityB extends Activity implements View.OnClickListener { private Button btn1; private Button btn2; private Button btn4; private BindService bindService = null; private boolean isBound = false; private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { isBound = true; BindService.MyBinder binder = (BindService.MyBinder) service; bindService = binder.getService(); int num = bindService.getRandomNumber(); Log.v("hjz","numB="+num); } @Override public void onServiceDisconnected(ComponentName name) { Log.v("hjz","onServiceDisconnected B"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_binder_b); findLayoutView(); setLister(); initData(); } private void findLayoutView() { btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn4 = (Button) findViewById(R.id.btn4); } private void setLister() { btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn4.setOnClickListener(this); } private void initData() { } @Override public void onClick(View v) { Intent intent = null; switch (v.getId()){ case R.id.btn1: intent = new Intent(BinderActivityB.this, BindService.class); intent.putExtra("from", "ActivityB"); bindService(intent,conn,BIND_AUTO_CREATE); break; case R.id.btn2: if (isBound){ isBound = false; Log.v("hjz","ActicityB is unbindService"); unbindService(conn); } break; case R.id.btn4: this.finish(); break; } } @Override protected void onDestroy() { super.onDestroy(); Log.i("hjz", "ActivityB -> onDestroy"); } }
在 binderA 中,操作順序為:點擊 binderA 綁定 → 跳轉 BinderAcitivityB → binderB 綁定 → binderB 解綁 → BinderB finish → BinderA 解綁 → BinderA finish,日誌如下:
07-09 13:16:10.030 8593-8593/com.hh.servicedemo I/hjz: BindService -> onCreate, Thread: main 07-09 13:16:10.030 8593-8593/com.hh.servicedemo I/hjz: BindService -> onBind, Thread: main 07-09 13:16:10.050 8593-8593/com.hh.servicedemo V/hjz: numA=665064614 07-09 13:16:13.650 8593-8593/com.hh.servicedemo V/hjz: numB=1116469133 07-09 13:16:16.550 8593-8593/com.hh.servicedemo V/hjz: ActicityB is unbindService 07-09 13:16:18.680 8593-8593/com.hh.servicedemo I/hjz: ActivityB -> onDestroy 07-09 13:16:22.920 8593-8593/com.hh.servicedemo V/hjz: ActicityA is unbindService 07-09 13:16:22.930 8593-8593/com.hh.servicedemo I/hjz: BindService -> onUnbind, from:ActivityA 07-09 13:16:22.950 8593-8593/com.hh.servicedemo I/hjz: BindService -> onDestroy, Thread: main 07-09 13:16:25.660 8593-8593/com.hh.servicedemo I/hjz: ActivityA -> onDestroy
從日誌中發現,在 client(客戶端)呼叫 Service【同一個 Service】的情況下:
如果 Service 不存在,執行順序是 onCreate → onBind,接著 client 建立 ServiceConnection 實例並執行 onServiceConnected 方法。
如果 Service 已處於執行狀態【說明在此之前已在其他地方啟動過該 Service】,由於先前 onBind 回調已取得 IBinder 實例,而該 IBinder 實例在所有 client 之間是共享的,所以第二次並不會再執行 onBind,而是直接使用上次取得的 IBinder 實例,將其傳入對應的 onServiceConnected 方法,標誌著連接已建立。此時便有兩個或多個 client 與 Service 綁定。
client(客戶端)執行 unbindService 的流程:
client 與 Service 解除綁定時,Service 會先檢測是否還有其他 client 與其連接 →
如果沒有,Service 執行 onUnbind 方法,接著執行 onDestroy 方法。
如果有,Service 不會執行 onUnbind 與 onDestroy 方法(從列印日誌可得出此結論)。
操作順序:點擊 binderA 綁定 → 跳轉 BinderAcitivityB → binderB 綁定 → binderB 解綁 → BinderB finish → BinderA finish,日誌如下:
07-09 13:19:58.340 8297-8297/com.hh.servicedemo I/hjz: BindService -> onCreate, Thread: main 07-09 13:19:58.360 8297-8297/com.hh.servicedemo I/hjz: BindService -> onBind, Thread: main 07-09 13:19:58.360 8297-8297/com.hh.servicedemo V/hjz: numA=2026321547 07-09 13:20:02.190 8297-8297/com.hh.servicedemo V/hjz: numB=-1586530569 07-09 13:20:04.140 8297-8297/com.hh.servicedemo V/hjz: ActicityB is unbindService 07-09 13:20:05.820 8297-8297/com.hh.servicedemo I/hjz: ActivityB -> onDestroy 07-09 13:20:07.080 8297-8297/com.hh.servicedemo I/hjz: ActivityA -> onDestroy 07-09 13:20:07.110 8297-8297/com.hh.servicedemo I/hjz: BindService -> onUnbind, from:ActivityA 07-09 13:20:07.110 8297-8297/com.hh.servicedemo I/hjz: BindService -> onDestroy, Thread: main
結論:當 client 與 Service 透過 bindService 連接起來後,如果 client(客戶端)執行 onDestroy 銷毀,那麼 client 會自動與 Service 解除綁定。
操作順序:點擊 binderA 綁定 → 跳轉 BinderAcitivityB → binderB 綁定 → BinderB finish → BinderA 解綁 → BinderA finish,日誌如下:07-09 13:17:40.320 8297-8297/com.hh.servicedemo I/hjz: BindService -> onCreate, Thread: main 07-09 13:17:40.320 8297-8297/com.hh.servicedemo I/hjz: BindService -> onBind, Thread: main 07-09 13:17:40.340 8297-8297/com.hh.servicedemo V/hjz: numA=-1117645606 07-09 13:17:43.460 8297-8297/com.hh.servicedemo V/hjz: numB=1774346322 07-09 13:17:44.770 8297-8297/com.hh.servicedemo I/hjz: ActivityB -> onDestroy 07-09 13:17:56.780 8297-8297/com.hh.servicedemo V/hjz: ActicityA is unbindService 07-09 13:17:56.780 8297-8297/com.hh.servicedemo I/hjz: BindService -> onUnbind, from:ActivityA 07-09 13:17:56.780 8297-8297/com.hh.servicedemo I/hjz: BindService -> onDestroy, Thread: main 07-09 13:17:58.060 8297-8297/com.hh.servicedemo I/hjz: ActivityA -> onDestroy操作順序:點擊 binderA 綁定 → 跳轉 BinderAcitivityB → binderB 綁定 → BinderB finish → BinderA 解綁 → BinderA finish,日誌如下:
07-09 13:21:22.890 8297-8297/com.hh.servicedemo I/hjz: BindService -> onCreate, Thread: main 07-09 13:21:22.890 8297-8297/com.hh.servicedemo I/hjz: BindService -> onBind, Thread: main 07-09 13:21:22.900 8297-8297/com.hh.servicedemo V/hjz: numA=1947324308 07-09 13:21:26.630 8297-8297/com.hh.servicedemo V/hjz: numB=-1343649013 07-09 13:21:27.730 8297-8297/com.hh.servicedemo I/hjz: ActivityB -> onDestroy 07-09 13:21:29.040 8297-8297/com.hh.servicedemo I/hjz: ActivityA -> onDestroy 07-09 13:21:29.060 8297-8297/com.hh.servicedemo I/hjz: BindService -> onUnbind, from:ActivityA 07-09 13:21:29.060 8297-8297/com.hh.servicedemo I/hjz: BindService -> onDestroy, Thread: main
上面的日誌列印,用一張圖來總結 client 與 Service 的操作流程:

在不同 App 之間呼叫這裡就不做介紹了,有興趣的可自行深入研究。
友盟 5.0 推送與分享踩坑總結
友盟 5.0 的變化 友盟 5.0 相較於舊版,推送與分享可以透過 Module 形式整合,資源與程式碼都更精簡。不過剛改版時,常會遇到與舊版不同的配置方式,因此需要重新
用 ExpandableListView 嵌套實作三級樹選單
標準的 ExpandableListView 只有兩層(群組與子項),要做三級樹(公司 → 部門 → 員工)就需要在子項的位置再嵌一個 ExpandableListVie
Android Drawable 完整梳理:從 ColorDrawable 到 StateListDrawable
在 Android 裡,凡是能畫到 Canvas 上的東西幾乎都脫離不了 Drawable。它常被直譯成「圖像」,但這個說法其實不太精準——Drawable 更像是「一組
Android FrameLayout 幀布局介紹
FrameLayout 是 Android 最簡單的布局之一。它會在螢幕上提供一個空白區域,所有加入的子元件都會疊放在同一層,預設從左上角開始排列。 基本特性 由於子元件