Android Service 兩種啟動方式
1.Context.startService()方式啟動
①Context.startService()方式的生命週期: 啟動時,startService –> onCreate() –> onStart()停止時,stopService –> onDestroy()如果呼叫者直接退出而沒有停止Service,則Service 會一直在後臺執行 Context.startService()方法啟動服務,在服務未被建立時,系統會先呼叫服務的onCreate()方法,接著呼叫onStart()方法。如果呼叫startService()方法前服務已經被建立,多次呼叫startService()方法並不會導致多次建立服務,但會導致多次呼叫onStart()方法。採用startService()方法啟動的服務,只能呼叫Context.stopService()方法結束服務,服務結束時會呼叫onDestroy()方法附程式碼
2.Context.bindService()方式啟動:①Context.bindService()方式的生命週期: 繫結時,bindService -> onCreate() –> onBind()呼叫者退出了,即解繫結時,Srevice就會unbindService –>onUnbind() –> onDestory()Context.bindService()方式啟動 Service的方法:繫結Service需要三個引數:bindService(intent, conn, Service.BIND_AUTO_CREATE);第一個:Intent物件第二個:ServiceConnection物件,建立該物件要實現它的onServiceConnected()和 onServiceDisconnected()來判斷連線成功或者是斷開連線第三個:如何建立Service,一般指定繫結的時候自動建立附程式碼
- package com.dada.test;
- 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.util.Log;
-
import
- import android.widget.Button;
- import com.dada.test.BindService.MyBinder;
- publicclass TestActivity extends Activity {
- privateboolean flag;
- privatestaticfinal String TAG = "TestActivity";
- /** Called when the activity is first created. */
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Button btnStart = (Button) findViewById(R.id.btnStart);
- Button btnStop = (Button) findViewById(R.id.btnStop);
- btnStart.setOnClickListener(new View.OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- //啟動service 方式2
- bindService();
- }
- });
- btnStop.setOnClickListener(new View.OnClickListener() {
- @Override
- publicvoid onClick(View v) {
- //停止service 方式2
- unBindService();
- }
- });
- }
- //啟動service 方式2
- //
- privatevoid bindService(){
- Intent intent = new Intent(TestActivity.this,BindService.class);
- Log.i(TAG, "bindService()");
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
- }
- privatevoid unBindService(){
- Log.i(TAG, "unBindService() start....");
- if(flag == true){
- Log.i(TAG, "unBindService() flag");
- unbindService(conn);
- flag = false;
- }
- }
- private ServiceConnection conn = new ServiceConnection() {
- @Override
- publicvoid onServiceDisconnected(ComponentName name) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onServiceDisconnected()");
- }
- @Override
- publicvoid onServiceConnected(ComponentName name, IBinder service) {
- // TODO Auto-generated method stub
- Log.i(TAG, "onServiceConnected()");
- MyBinder binder = (MyBinder)service;
- BindService bindService = binder.getService1();
- bindService.MyMethod();
- flag = true;
- }
- };
- }
service
- package com.dada.test;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.util.Log;
- publicclass BindService extends Service {
- privatestaticfinal String TAG = "BindService";
- private MyBinder myBinder = new MyBinder();
- publicvoid MyMethod(){
- Log.i(TAG, "BindService-->MyMethod()");
- }
- @Override
- public IBinder onBind(Intent intent) {
- Log.i(TAG, "BindService-->onBind()");
- return myBinder;
- }
- publicclass MyBinder extends Binder{
- public BindService getService1(){
- return BindService.this;
- }
- }
- @Override
- publicvoid onCreate() {
- Log.i(TAG, "BindService-->onCreate()");
- super.onCreate();
- }
- @Override
- publicvoid onStart(Intent intent, int startId) {
- Log.i(TAG, "BindService-->onStart()");
- super.onStart(intent, startId);
- }
- @Override
- publicvoid onDestroy() {
- Log.i(TAG, "BindService-->onDestroy()");
- super.onDestroy();
- }
- @Override
- publicboolean onUnbind(Intent intent) {
- Log.i(TAG, "BindService-->onUnbind()");
- returnsuper.onUnbind(intent);
- }
- }
執行日誌
點選啟動
點選停止
沒有打出onServiceDisconnected的日誌的原因:
注:SDK上是這麼說的:This is called when the connection with the service has beenunexpectedly disconnected -- that is, its process crashed. Because it is running in our same
process, we should never see this happen.
所以說,只有在service因異常而斷開連線的時候,這個方法才會用到
其他
由於Service 的onStart()方法只有在startService()啟動Service的情況下才呼叫,故使用onStart()的時候要注意這點。
與 Service 通訊並且讓它持續執行
如果我們想保持和 Service 的通訊,又不想讓 Service 隨著 Activity 退出而退出呢?你可以先 startService() 然後再 bindService() 。當你不需要繫結的時候就執行 unbindService() 方法,執行這個方法只會觸發 Service 的 onUnbind() 而不會把這個 Service 銷燬。這樣就可以既保持和 Service 的通訊,也不會隨著 Activity 銷燬而銷燬了。