記,Android服務與綁定服務生命週期

  1. 啓動普通服務,然後Activity退出的日誌
I/System.out: Activity 開啓服務
I/System.out: Service onCreate
I/System.out: Service onStartCommand
I/System.out: Activity onDestroy

普通服務與啓動他的Activity生命週期無關

  1. 綁定服務,然後Activity退出的日誌
I/System.out: Activity 綁定服務
I/System.out: Service onCreate
I/System.out: Service onBind
I/System.out: Activity onServiceConnected
I/System.out: Activity onDestroy
I/System.out: Service onUnbind
I/System.out: Service onDestroy

綁定服務與綁定他的Activity生命週期同生共死

  1. 啓動普通服務,在綁定服務,然後Activity退出的日誌
I/System.out: Activity 開啓服務
I/System.out: Service onCreate
I/System.out: Service onStartCommand
I/System.out: Activity 綁定服務
I/System.out: Service onBind
I/System.out: Activity onServiceConnected
I/System.out: Activity onDestroy
I/System.out: Service onUnbind

綁定服務退出,普通服務還在

  1. 多次綁定服務的日誌
2019-09-28 00:35:13.240 17186-17186/com.example.bindservice I/System.out: Activity 綁定服務
2019-09-28 00:35:13.289 17186-17186/com.example.bindservice I/System.out: Service onCreate
2019-09-28 00:35:13.295 17186-17186/com.example.bindservice I/System.out: Service onBind
2019-09-28 00:35:13.308 17186-17186/com.example.bindservice I/System.out: Activity onServiceConnected
2019-09-28 00:35:13.742 17186-17186/com.example.bindservice I/System.out: Activity 綁定服務
2019-09-28 00:35:13.772 17186-17186/com.example.bindservice I/System.out: Activity onServiceConnected
2019-09-28 00:35:15.916 17186-17186/com.example.bindservice I/System.out: Activity onDestroy
2019-09-28 00:35:15.961 17186-17186/com.example.bindservice E/ActivityThread: Activity com.example.bindservice.MainActivity has leaked ServiceConnection com.example.bindservice.MainActivity$MyConn@660059c that was originally bound here
    android.app.ServiceConnectionLeaked: Activity com.example.bindservice.MainActivity has leaked ServiceConnection com.example.bindservice.MainActivity$MyConn@660059c that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1620)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1512)
        at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1659)
        at android.app.ContextImpl.bindService(ContextImpl.java:1612)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:698)
        at com.example.bindservice.MainActivity.bind(MainActivity.java:51)
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
        at android.view.View.performClick(View.java:6617)
        at android.view.View.performClickInternal(View.java:6594)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25926)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6672)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
2019-09-28 00:35:15.969 17186-17186/com.example.bindservice I/System.out: Service onUnbind
2019-09-28 00:35:15.971 17186-17186/com.example.bindservice I/System.out: Service onDestroy

多次綁定服務,然後綁定他的Activity退出此時會有ServiceConnectionLeaked,綁定的服務退出

  1. 啓動普通服務,多次綁定服務的日誌
2019-09-28 00:37:44.237 17186-17186/com.example.bindservice I/System.out: Activity 開啓服務
2019-09-28 00:37:44.268 17186-17186/com.example.bindservice I/System.out: Service onCreate
2019-09-28 00:37:44.277 17186-17186/com.example.bindservice I/System.out: Service onStartCommand
2019-09-28 00:37:45.172 17186-17186/com.example.bindservice I/System.out: Activity 綁定服務
2019-09-28 00:37:45.208 17186-17186/com.example.bindservice I/System.out: Service onBind
2019-09-28 00:37:45.213 17186-17186/com.example.bindservice I/System.out: Activity onServiceConnected
2019-09-28 00:37:45.696 17186-17186/com.example.bindservice I/System.out: Activity 綁定服務
2019-09-28 00:37:45.716 17186-17186/com.example.bindservice I/System.out: Activity onServiceConnected
2019-09-28 00:37:47.288 17186-17186/com.example.bindservice I/System.out: Activity onDestroy
2019-09-28 00:37:47.345 17186-17186/com.example.bindservice E/ActivityThread: Activity com.example.bindservice.MainActivity has leaked ServiceConnection com.example.bindservice.MainActivity$MyConn@58068fa that was originally bound here
    android.app.ServiceConnectionLeaked: Activity com.example.bindservice.MainActivity has leaked ServiceConnection com.example.bindservice.MainActivity$MyConn@58068fa that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1620)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1512)
        at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1659)
        at android.app.ContextImpl.bindService(ContextImpl.java:1612)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:698)
        at com.example.bindservice.MainActivity.bind(MainActivity.java:51)
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
        at android.view.View.performClick(View.java:6617)
        at android.view.View.performClickInternal(View.java:6594)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25926)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6672)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
2019-09-28 00:37:47.347 17186-17186/com.example.bindservice I/System.out: Service onUnbind

啓動普通服務,多次綁定服務,然後綁定他的Activity退出此時會有ServiceConnectionLeaked,解綁服務,服務還在

  1. 多次未解綁服務的多次綁定服務, 程序error
2019-09-28 00:40:59.639 17307-17307/com.example.bindservice I/System.out: Activity 綁定服務
2019-09-28 00:40:59.688 17307-17307/com.example.bindservice I/System.out: Service onCreate
2019-09-28 00:40:59.689 17307-17307/com.example.bindservice I/System.out: Service onBind
2019-09-28 00:40:59.707 17307-17307/com.example.bindservice I/System.out: Activity onServiceConnected
2019-09-28 00:41:00.894 17307-17307/com.example.bindservice I/System.out: Activity 綁定服務
2019-09-28 00:41:00.921 17307-17307/com.example.bindservice I/System.out: Activity onServiceConnected
2019-09-28 00:41:04.846 17307-17307/com.example.bindservice I/System.out: Activity 解綁服務
2019-09-28 00:41:04.847 17307-17307/com.example.bindservice D/AndroidRuntime: Shutting down VM
    
    
    --------- beginning of crash
2019-09-28 00:41:04.856 17307-17307/com.example.bindservice E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bindservice, PID: 17307
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
        at android.view.View.performClick(View.java:6617)
        at android.view.View.performClickInternal(View.java:6594)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25926)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6672)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
        at android.view.View.performClick(View.java:6617) 
        at android.view.View.performClickInternal(View.java:6594) 
        at android.view.View.access$3100(View.java:778) 
        at android.view.View$PerformClick.run(View.java:25926) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6672) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.unbindService(android.content.ServiceConnection)' on a null object reference
        at android.content.ContextWrapper.unbindService(ContextWrapper.java:717)
        at com.example.bindservice.MainActivity.unbind(MainActivity.java:58)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
        at android.view.View.performClick(View.java:6617) 
        at android.view.View.performClickInternal(View.java:6594) 
        at android.view.View.access$3100(View.java:778) 
        at android.view.View$PerformClick.run(View.java:25926) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6672) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860) 
2019-09-28 00:41:04.889 17307-17307/com.example.bindservice I/Process: Sending signal. PID: 17307 SIG: 9

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章