1. 程式人生 > >Android-Service生命週期

Android-Service生命週期

MyService重寫的方法:

public class MyService extends Service {

    private final static String TAG = MyService.class.getSimpleName();

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

    @Override
    public void onStart(Intent intent, int
startId) { super.onStart(intent, startId); Log.d(TAG, "onStart()..."); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand()..."); return super.onStartCommand(intent, flags, startId); } @Override
public IBinder onBind(Intent intent) { Log.d(TAG, "onBind()..."); return null; } @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "onUnbind()..."); return super.onUnbind(intent); } @Override public void onDestroy() { super
.onDestroy(); Log.d(TAG, "onDestroy()..."); } }

 MainActivity呼叫Service方法

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

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

    public void bindService(View view) {
        bindService(new Intent(MainActivity.this, MyService.class), connection, Context.BIND_AUTO_CREATE);
    }

    public void unBindService(View view) {
        unbindService(connection);
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    public void startMainActivity2(View view) {
        startActivity(new Intent(this, MainActivity2.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
    }

 

startService  stopService

第一次startService:

12-06 19:38:49.693 1958-1958/liudeli.service1 D/MyService: onCreate()...
12-06 19:38:49.693 1958-1958/liudeli.service1 D/MyService: onStartCommand()...
12-06 19:38:49.693 1958-1958/liudeli.service1 D/MyService: onStart()...

 

第二次startService:

12-06 19:38:55.433 1958-1958/liudeli.service1 D/MyService: onStartCommand()...
12-06 19:38:55.433 1958-1958/liudeli.service1 D/MyService: onStart()...

 

去點選stopService:

12-06 19:43:24.658 1958-1958/liudeli.service1 D/MyService: onDestroy()...

 

startService後:

 

 

bindService  unBindService

bindService,多次點選bindService,只會出現一次:

12-06 19:49:27.800 1958-1958/liudeli.service1 D/MyService: onCreate()...
12-06 19:49:27.801 1958-1958/liudeli.service1 D/MyService: onBind()...

 

unBindService,點選unBindService,會執行:

12-06 20:47:58.903 4112-4112/liudeli.service1 D/MyService: onUnbind()...
12-06 20:47:58.903 4112-4112/liudeli.service1 D/MyService: onDestroy()...

 

startService  stopService    bindService  unBindService

點選startService:  

12-06 20:49:03.637 4235-4235/liudeli.service1 D/MyService: onCreate()...
12-06 20:49:03.638 4235-4235/liudeli.service1 D/MyService: onStartCommand()...
12-06 20:49:03.638 4235-4235/liudeli.service1 D/MyService: onStart()...

 

點選bindService:

12-06 20:49:08.867 4235-4235/liudeli.service1 D/MyService: onBind()...

 

點選unBindService:

12-06 20:49:32.640 4235-4235/liudeli.service1 D/MyService: onUnbind()...

 

點選stopService:

12-06 20:49:35.351 4235-4235/liudeli.service1 D/MyService: onDestroy()...

 

注意:Service是單例方式,onCreate()  onDestroy() 只會建立一次

     Service中是沒有onStop()方法

     Service中的只能由別人呼叫啟動,Service自己不能啟動

     Service中停止可以由別人呼叫stopService,Service自己也可以呼叫stopSelf();

     bindService不會在手機系統中顯示執行服務

   startService會在手機系統中顯示執行服務

     bindService是與Activity同生共死的,在介面結束後需要呼叫:Activity-->onDestroy(unBindService),AActivity繫結服務後只能AActivity解綁服務

     startService是與Activity無關的,是在後臺服務長時間執行的,AActivity啟動的服務,可以用BActivity來停止服務

   服務的特點是:在後臺長時間的執行,不能直接的去執行耗時操作,需要通過非同步方式去執行耗時操作,可以把Service想象成沒有介面的Activity,區別是服務在後臺長時間執行