2017-08-05 74 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