在查看 Android 服務示例時,我有一個問題。
https://developer.android.com/guide/components/services
Android 開發者服務示例圖片
為什么不呼叫 super.onDestroy,即使他們在此示例中覆寫了 onDestroy?Android Service onDestroy 的作用是什么?
我試著在 Android 上閱讀 git 源代碼。
我洗掉了 super.onDestroy 并在分析器中進行了捕獲。(但是,無論我添加與否,服務都沒有被釋放)
uj5u.com熱心網友回復:
Q1:
為什么不呼叫 super.onDestroy,即使他們在此示例中覆寫了 onDestroy?
Ans: super 方法在這里什么都不做。如果您覆寫本機 Android 類,則包含 super 呼叫只是一個好習慣。
此外,這對于私有方法可能并非如此,因為我總是在不呼叫超級方法的情況下覆寫第 3 方庫中的方法。這取決于。
參考文獻:AOSP - frameworks/base/core/java/android/app/Service.java
...
public abstract class Service extends ContextWrapper implements ComponentCallbacks2,
ContentCaptureManager.ContentCaptureClient {
...
/**
* Called by the system to notify a Service that it is no longer used and is being removed. The
* service should clean up any resources it holds (threads, registered
* receivers, etc) at this point. Upon return, there will be no more calls
* in to this Service object and it is effectively dead. Do not call this method directly.
*/
public void onDestroy() {
}
...
}
Q2:
Android Service onDestroy的作用是什么?
Ans:這是一條由低級 API 呼叫發出的訊息。它只是一個通知,告訴你你的服務將被銷毀,請在這個塊結束之前清理資源(或者如果你沒有什么重要的事情要做就忽略它)。即使你在那里什么都不做,服務也會被破壞,因為它只是一個通知。在正常情況下,您無法控制那里的生命周期。
更新
所以我忘了回答你的標題問題。該行為與我在 Ans 1 中所說的有點矛盾。
IMO,我認為該網站示例只是想讓您知道,通過在那里創建祝酒詞,服務將在那里被破壞。他們是否在那里添加沒有區別super.onDestroy
。
但同樣,在網站示例中呼叫onDestroy
似乎違反了 .com 評論中的建議Service#onDestroy
。
歡迎大家指正
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/535899.html
標籤:安卓