handler本身没有什么特别的事,但是因为AS升级3.0之后使用handler经常被警告标黄所以看了一下。
This Handler class should be static or leaks might occur (anonymous android.os.Handler) less... (Ctrl+F1)
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 static 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 WeakReference object.
大概是因为handler没有返回导致可能资源被一直占用可能导致内存泄漏。
private class GetSearchList extends Thread {
@Override
public void run() {
super.run();
Message msg = new Message();
msg.obj = Str;
handler.sendMessage(msg);
}
}
这是一段很普通的线程
目前的解决办法是换一个handler方法来处理
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
String str = (String) msg.obj;
return true;
}
});
使用一个有返回的handleMessage来处理线程返回的消息,就不会有黄色的警告了