Handler内存泄露与解决

直接创建Handler  

warning: In Android, Handler classes should be static or leaks might occur.`。         

`Android`程序第一次创建的时候,默认会创建一个`Looper`对象,`Looper`去处理`Message Queue`中的每个`Message`,主线程的`Looper`存在整个应用程序的生命周期.
- `Hanlder`在主线程创建时会关联到`Looper`的`Message Queue`,`Message`添加到消息队列中的时候`Message(排队的Message)`会持有当前`Handler`引用,当`Looper`处理到当前消息的时候,会调用`Handler#handleMessage(Message)`.就是说在`Looper`处理这个`Message`之前,会有一条链`MessageQueue -> Message -> Handler -> Activity`,由于它的引用导致你的`Activity`被持有引用而无法被回收`
- **在java中,no-static的内部类会隐式的持有当前类的一个引用。static的内部类则没有。**

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.audiocn.karaoke.utils;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Handler.Callback;

public class UIHandler {
    private static Handler handler = new Handler(Looper.getMainLooper(), new Callback() {
        public boolean handleMessage(Message msg) {
            return UIHandler.handleMessage(msg);
}
    });

    public UIHandler() {
    }

    private static boolean handleMessage(Message msg) {
        Object[] objs = (Object[])((Object[])msg.obj);
Message inner = (Message)objs[0];
Callback callback = (Callback)objs[1];
        return callback != null?callback.handleMessage(inner):false;
}

    private static Message getShellMessage(Message msg, Callback callback) {
        Message shell = new Message();
shell.what = 1;
shell.obj = new Object[]{msg, callback};
        return shell;
}

    private static Message getShellMessage(int what, Callback callback) {
        Message msg = new Message();
msg.what = what;
        return getShellMessage(msg, callback);
}

    public static boolean sendMessage(Message msg, Callback callback) {
        return handler.sendMessage(getShellMessage(msg, callback));
}

    public static boolean sendMessageDelayed(Message msg, long delayMillis, Callback callback) {
        return handler.sendMessageDelayed(getShellMessage(msg, callback), delayMillis);
}

    public static boolean sendMessageAtTime(Message msg, long uptimeMillis, Callback callback) {
        return handler.sendMessageAtTime(getShellMessage(msg, callback), uptimeMillis);
}

    public static boolean sendMessageAtFrontOfQueue(Message msg, Callback callback) {
        return handler.sendMessageAtFrontOfQueue(getShellMessage(msg, callback));
}

    public static boolean sendEmptyMessage(int what, Callback callback) {
        return handler.sendMessage(getShellMessage(what, callback));
}

    public static boolean sendEmptyMessageAtTime(int what, long uptimeMillis, Callback callback) {
        return handler.sendMessageAtTime(getShellMessage(what, callback), uptimeMillis);
}

    public static boolean sendEmptyMessageDelayed(int what, long delayMillis, Callback callback) {
        return handler.sendMessageDelayed(getShellMessage(what, callback), delayMillis);
}

    public static boolean post(Runnable r) {
        return handler.post(r);
}

    public static boolean postAtFrontOfQueue(Runnable r) {
        return handler.postAtFrontOfQueue(r);
}

    public static boolean postDelayed(Runnable r, long delayMillis) {
        return handler.postDelayed(r, delayMillis);
}

    public static boolean postAtTime(Runnable r, long uptimeMillis) {
        return handler.postAtTime(r, uptimeMillis);
}

    public static boolean postAtTime(Runnable r, Object token, long uptimeMillis) {
        return handler.postAtTime(r, token, uptimeMillis);
}

    public static void removeCallbacks(Runnable r) {
        handler.removeCallbacks(r);
}

    public static void removeAllMessages() {
        handler.removeMessages(1);
}

    public static void removeMessages(int what) {
        handler.removeMessages(what);
}
}

你可能感兴趣的:(Handler内存泄露与解决)