Android处理程序的FIND_POTENTIAL_LEAKS

时间:2017-08-05 10:15:20

标签: android android-handler

Handler.java的源代码中,我跨越代码段

public Handler(Callback callback, boolean async) {
    if (FIND_POTENTIAL_LEAKS) {
        final Class<? extends Handler> klass = getClass();
        if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                (klass.getModifiers() & Modifier.STATIC) == 0) {
            Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
                klass.getCanonicalName());
        }
    }

}

从代码中,我可以看到FIND_POTENTIAL_LEAKS用于查找潜在的泄漏。但是,该字段为private且始终为false

那么什么时候才能真正使用?

修改

来自Murat,反思似乎有效,但为什么Android设置默认值true

1 个答案:

答案 0 :(得分:0)

该字段的文档中有评论:

 public class Handler {
65     /*
66      * Set this flag to true to detect anonymous, local or member classes
67      * that extend this Handler class and that are not static. These kind
68      * of classes can potentially create leaks.
69      */
70     private static final boolean FIND_POTENTIAL_LEAKS = false;

我猜它已通过反射设置为true,例如这里描述Change private static final field using Java reflection