All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mobi.cangol.mobile.Task Maven / Gradle / Ivy

There is a newer version: 1.2.7
Show newest version
package mobi.cangol.mobile;


import android.os.Handler;
import android.os.Message;

import java.lang.ref.WeakReference;

/**
 * Created by xuewu.wei on 2018/5/3.
 */
public abstract class Task implements Runnable {
    private InnerHandler handler;

    public Task() {
        this.handler = new InnerHandler(this);
    }

    @Override
    public void run() {
        final R r = call();
        Message.obtain(handler, 1, r).sendToTarget();
    }

    public abstract R call();

    public abstract void result(R r);

    public static class InnerHandler extends Handler {
        private final WeakReference taskWeakReference;

        public InnerHandler(Task task) {
            taskWeakReference = new WeakReference<>(task);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1 && taskWeakReference.get() != null) {
                taskWeakReference.get().result(msg.obj);
                taskWeakReference.clear();
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy