日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android 解決InputMethodManager 內存泄露問題

作者:匆忙擁擠repeat 更新時間: 2022-07-18 編程語言

在android 11的 InputMethodManager的源碼中,查看 windowDismissed(),如下

/**
 * An empty method only to avoid crashes of apps that call this method via reflection and do not
 * handle {@link NoSuchMethodException} in a graceful manner.
 *
 * @deprecated This is an empty method.  No framework method must call this method.
 * @hide
 */
public void windowDismissed(IBinder appWindowToken) {
    // Intentionally empty.
    //
    // It seems that some applications call this method via reflection to null clear the
    // following fields that used to exist in InputMethodManager:
    //  * InputMethodManager#mCurRootView
    //  * InputMethodManager#mServedView
    //  * InputMethodManager#mNextServedView
    // so that these objects can be garbage-collected when an Activity gets dismissed.
    //
    // It is indeed true that older versions of InputMethodManager had issues that prevented
    // these fields from being null-cleared when it should have been, but the understanding of
    // the engineering team is that all known issues have already been fixed as of Android 10.
    //
    // For older devices, developers can work around the object leaks by using
    // androidx.activity.ComponentActivity.
    // See https://issuetracker.google.com/u/1/issues/37122102 for details.
    //
    // If you believe InputMethodManager is leaking objects in API 24 or any later version,
    // please file a bug at https://issuetracker.google.com/issues/new?component=192705.
}

/*
* 以下三個字段,在低于 android 10 的版本中是都存在的;之后,有變更。
* 低于 android 10,當Activity dismissed 時,可以通過反射,將它們置null,使它們不再持有該Activity中View的引用,防止內存泄露。
* InputMethodManager#mCurRootView
* InputMethodManager#mServedView
* InputMethodManager#mNextServedView
*
* android 10之后修復了所有已知問題。
* 對于較老的版本,建議使用 androidx.activity.ComponentActivity 及其子類。
* issues/37122102,說明 android N/7.0 (api 24),就開始了修復。
*
* android 11 source code:http://aospxref.com/android-11.0.0_r21/xref/frameworks/base/core/java/android/view/inputmethod/InputMethodManager.java
* android 10 source code:http://aospxref.com/android-10.0.0_r47/xref/frameworks/base/core/java/android/view/inputmethod/InputMethodManager.java
*/

解決方案:對于低于android10,且非 ComponentActivity的context,反射獲取InputMethodManager對象中的 mCurRootViewmServedViewmNextServedView這三個屬性,轉換為View類型后,判斷view的context等于要釋放的Activity的context時,將這個屬性置為null。

fun fixMemoryLeak(context: Context?) {
    try {
        context ?: return
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) return // android 10 已修復
        if (context is ComponentActivity) return // androidx.activity.ComponentActivity 已修復
        val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager ?: return
        imm.javaClass.declaredFields.filter {
            it.name == "mCurRootView" || it.name == "mServedView" || it.name == "mNextServedView"
        }.forEach { filed ->
            val origin = filed.isAccessible
            if (!origin) {
                filed.isAccessible = true
            }
            (filed.get(imm) as? View)?.takeIf { it.context == context }?.also {
                filed.set(imm, null)
            }
            filed.isAccessible = origin
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

原文鏈接:https://blog.csdn.net/jjwwmlp456/article/details/124580048

欄目分類
最近更新