Service生命周期浅析

一、startService方式启动

    第一次调用startService:onCreate——onStartCommand——onStart;

    之后调用startService:onStartCommand——onStart;

    调用stopService:onDestroy

二、bindService方式启动

    第一次调用bindService:onCreate——onBind

    多次调用bindService:无效,只能调用一次

    调用unbindService:onUnbind——onDestroy

    再次调用unbindService:报错退出,如下:

           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime: FATAL EXCEPTION: main
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime: Process: com.winter.activitylife, PID: 16293
           03-03 13:50:51.017 16293-         16293/com.winter.activitylife E/AndroidRuntime: java.lang.IllegalArgumentException: Service not registered: com.winter.activitylife.MainActivity$1@13f07343
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1031)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at android.app.ContextImpl.unbindService(ContextImpl.java:1817)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at android.content.ContextWrapper.unbindService(ContextWrapper.java:551)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at com.winter.activitylife.MainActivity.onClick(
MainActivity.java:102 )
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at android.view.View.performClick(View.java:4783)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19887)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5283)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
           03-03 13:50:51.017 16293-16293/com.winter.activitylife E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

三、两种启动方式一起用

    1.startService——onCreate——onStartCommand——onStart——bindService——onBind——stopService——null——

unbindService——onUnbind——onDestroy

    2.startService——onCreate——onStartCommand——onStart——bindService——onBind——unbindService——onUnbind——

stopService——onDestroy

    3.bindService——onCreate——onBind——startService——onStartCommand——onStart——unbindService——onUnbind——

stopService——onDestroy

    4.bindService——onCreate——onBind——startService——onStartCommand——onStart——stopService——null——

unbindService——onUnbind——onDestroy

    (null代表没有经历任何生命周期方法)

四、与Activity生命周期的关联

    1.startService方式启动

    如果一个Activity用startService的方式启动Service,而且在Activity生命结束(onDestroy)之前没有调用stopService方法,Service不会结束,当Activity再次被打开,并重新调用starttService方法时,Service的onCreate方法不会被执行,只会执行nStartCommand——onStart方法。

    2.bindService方式启动

    如果一个Activity用bindService的方式启动Service,而且在Activity生命结束(onDestroy)之前没有调用unbindService方法,Activity的onDestroy方法执行后会报错,表明有连接泄漏并且会继续执行Service的onUnbind——onDestroy方法,现象如下:

           03-03 14:16:49.427 17091-17091/com.winter.activitylife E/MainActivity: onCreate
           03-03 14:16:49.427 17091-17091/com.winter.activitylife E/MainActivity: onStart
           03-03 14:16:49.437 17091-17091/com.winter.activitylife E/MainActivity: onResume
           03-03 14:16:55.327 17091-17091/com.winter.activitylife E/MyService: onCreate
           03-03 14:16:55.327 17091-17091/com.winter.activitylife E/MyService: onBind
           03-03 14:17:00.247 17091-17091/com.winter.activitylife E/MainActivity: onPause
           03-03 14:17:00.507 17091-17091/com.winter.activitylife E/MainActivity: onStop
           03-03 14:17:00.507 17091-17091/com.winter.activitylife E/MainActivity: onDestroy
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:
                                                                  Activity com.winter.activitylife.MainActivity has leaked ServiceConnection
                                                                  com.winter.activitylife.MainActivity$1@172b2663 that was originally bound here
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:
                                                                  android.app.ServiceConnectionLeaked: Activity com.winter.activitylife.MainActivity has leaked ServiceConnection 
                                                                  com.winter.activitylife.MainActivity$1@172b2663 that was originally bound here
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1079)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:973)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1783)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.app.ContextImpl.bindService(ContextImpl.java:1766)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.content.ContextWrapper.bindService(ContextWrapper.java:539)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at com.winter.activitylife.MainActivity.onClick(
MainActivity.java:99 )
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.view.View.performClick(View.java:4783)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.view.View$PerformClick.run(View.java:19887)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.os.Handler.handleCallback(Handler.java:739)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.os.Handler.dispatchMessage(Handler.java:95)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.os.Looper.loop(Looper.java:135)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at android.app.ActivityThread.main(ActivityThread.java:5283)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at java.lang.reflect.Method.invoke(Native Method)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at java.lang.reflect.Method.invoke(Method.java:372)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/ActivityThread:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/MyService: onUnbind
           03-03 14:17:00.537 17091-17091/com.winter.activitylife E/MyService: onDestroy
    打印调用堆栈显示,onUnbind方法是被ActivityThread调用的:
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err: java.lang.Exception: this is a log
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at com.winter.activitylife.MyService.onUnbind(MyService.java:51)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at android.app.ActivityThread.handleUnbindService(ActivityThread.java:2834)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at android.app.ActivityThread.access$2000(ActivityThread.java:154)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1408)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at android.os.Looper.loop(Looper.java:135)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5283)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
           03-03 14:27:05.857 13006-13006/com.winter.activitylife W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
    当Activity再次被打开,并调用 bindService方法时 ,Service会被重新创建并绑定:bindService——onCreate——onBind

五、Service的onRebind方法何时被调用?

    打开Service.java类,查看onRebind方法的注释:

    /**
     * Called when new clients have connected to the service, after it had

     * previously been notified that all had disconnected in its
     * {@link #onUnbind}. This will only be called if the implementation

     * of {@link #onUnbind} was overridden to return true.
     * 
     * @param intent The Intent that was used to bind to this service,
     * as given to {@link android.content.Context#bindService
     * Context.bindService}. Note that any extras that were included with
     * the Intent at that point will <em>not</em> be seen here.
     */
    public void onRebind(Intent intent) {

    }

    两点:第一,Service被连接(connected),并且已经调用onUnbind解开了连接;第二onUnbind方法被重写,并且返回true。


你可能感兴趣的:(android,service,生命周期)