最近刚刚用到 android 的 Handler ,有几点疑惑。

  1. 描述你的问题

    如下图,我在Activity中用到了Handler,然后在 Android Studio 中老是有一大块地黄色,主要是看不太明白。。。这是个warning吧。。。求大神翻译指导一下。。。
    
    warning的内容是:
    This Handler class should be static or leaks might occur ...
    
    Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected. if the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue. if the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows : Declare the Handler as a staic class; In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler; Make all references to members of the outer class using the WeakPreference object.
    
    打完这段warning之后,是不是,建议把Handler弄成static,以方便其他的外部类直接访问这个Handler,以便更新UI线程?
    
    
  2. 贴上相关代码

//UI线程的Handler
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x01:{
                    destoryWaitDialog();
                    okLand();
                    break;
                }
                case 0x02:{
                    destoryWaitDialog();
                    waringLand();
                    break;
                }
            }
        }
    };

贴上相关截图

查看回复