天天看點

淺談Service

前言

Service是安卓四大元件之一,一般用于開啟背景服務(音樂播放,和背景下載下傳)。

Service的兩種生命周期

一種是通過startService()開啟的服務: onCreate()→onStartCommond()→onDestroy()

另一種是通過bindService()開啟的服務:onCreate()→onBind()→onUnBind()→onDestroy()

如果使用startService()來啟動,由于未和通路者進行綁定,是以可以使用stopService()或者stopSelf()來停止在背景運作的service。

如果使用bindService()來啟動,由于與通路者進行了綁定,是以隻需要将通路者停止掉就可以停止service。

每個生命周期的職責

onCreate():建立Service對象的時候使用,有且隻調用一次。我們一般在此方法中進行一些初始化元件的操作。

onStartCommand():每次在使用startService()的時候都會調用此方法,一般在此方法中我們可以進行一些具體的方法操作。

onBind():這是Service必須實作的方法,該方法傳回一個IBinder對象,我們可以通過這個對象與Service元件進行通信。

onDestroy():Service被關閉之前會調用這個方法。

onUnBind():當Service綁定的是以用戶端都斷開連接配接的時候會會回調這個方法。

1.以startService()開啟的服務

用戶端

public void startMyService(View view) {
        startService(new Intent(MainActivity.this, MyService.class));
    }
           

服務端

public class MyService extends Service {
    public MyService() {
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("Q_M", "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("Q_M", "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("Q_M", "onDestroy");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
           

2.以bindService()開啟的服務

用戶端

//bind button的點選事件
	public void bind(View view) {
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    //unbind button的點選事件
    public void unBind(View view) {
        unbindService(mServiceConnection);
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("Q_M", "onServiceConnected invoked");

            //這裡能這麼寫是因為在同一個程序中
            MyService.MyBinder myBinder = (MyService.MyBinder) service;
            myBinder.invokeTest();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(getLocalClassName(), "service disconnected");
        }
    };
           

服務端

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("Q_M", "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("Q_M", "onCreate");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("Q_M", "onDestroy");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("Q_M", "onBind");
        return new MyBinder();
    }

    class MyBinder extends Binder {

        public void invokeTest() {
            Log.d("Q_M", "MyBinder内的方法被調用");
        }

    }
}


           
兩者的差別

1.通過startservice開啟的服務.一旦服務開啟, 這個服務和開啟他的調用者之間就沒有任何的關系了.

調用者不可以通路 service裡面的方法. 調用者如果被系統回收了或者調用了ondestroy方法, service還會繼續存在

2.通過bindService開啟的服務,服務開啟之後,調用者和服務之間 還存在着聯系 ,

一旦調用者挂掉了.service也會跟着挂掉 .