2017-08-05 41 views
0

Handler.java的源代码,我过下面的代码段的Android Handler的FIND_POTENTIAL_LEAKS

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是用来寻找潜在leaks.However的申请是private始终false

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

编辑

从穆拉特,反射似乎工作,但为什么Android设置的值默认true

回答

0

有一个在文档该字段的评论:我想这是得到通过反射设置为true例如

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; 

这里描述的Change private static final field using Java reflection

+0

我想这样。但它似乎愚蠢,大声笑。 – JianxinLi

+0

并非所有你不明白的都是愚蠢的。 – Ridcully

+0

那么为什么不默认'真'? @Ridcully – JianxinLi