日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android应用开发——service连接泄露异常:android.app.ServiceConnectionLeaked: that was originally bound here

發布時間:2023/12/19 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android应用开发——service连接泄露异常:android.app.ServiceConnectionLeaked: that was originally bound here 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在做service開發過程中,大部分可能會遇到以下異常,該異常僅通過log輸出,并不會導致app crash。

E/ActivityThread: Activity com.example.image.all_samples.Main2Activity has leaked ServiceConnection com.example.image.all_samples.Main2Activity$5@d124870 that was originally bound hereandroid.app.ServiceConnectionLeaked: Activity com.example.image.all_samples.Main2Activity has leaked ServiceConnection com.example.image.all_samples.Main2Activity$5@d124870 that was originally bound hereat android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1369)at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1264)at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1454)at android.app.ContextImpl.bindService(ContextImpl.java:1426)at android.content.ContextWrapper.bindService(ContextWrapper.java:636)at com.example.image.all_samples.Main2Activity$3.onClick(Main2Activity.java:53)at android.view.View.performClick(View.java:5724)at android.view.View$PerformClick.run(View.java:22572)at android.os.Handler.handleCallback(Handler.java:751)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6259)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:913)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)

異常原因:
比較簡單,一般為在activity退出時未調用unbindService導致,只需要添加unbindService接口即可。

進一步思考:android系統是如何檢測到給泄漏的呢。
很容想到bindService時,是通過當前activity的context進行bind的,所以service的生命周期應該是同改activity關聯,進一步驗證,bindService時通過application的context進行bind,退出當前activity時,不會報上面的異常錯誤,基本可以證明service的生命周期同bindService時依賴的context相關,所以如果需要一個不依賴于具體activity生命周期的service,需要用application的context進行bind。

總結

以上是生活随笔為你收集整理的Android应用开发——service连接泄露异常:android.app.ServiceConnectionLeaked: that was originally bound here的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。