android封装工作线程跟Handler工具类
博客专区 > executor 的博客 > 博客详情
android封装工作线程跟Handler工具类
executor 发表于7个月前
android封装工作线程跟Handler工具类
  • 发表于 7个月前
  • 阅读 140
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

直接上代码,不解说 - -

基于MVP封装P的基类

AbsHandlerThreadHelper.java

import java.lang.ref.WeakReference;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

public abstract class AbsHandlerThreadHelper<T extends Context> {

	protected ExecutorService mExecutorService;

	private WeakReference<T> mContextWeakReference;

	protected HandlerInner<T> mHandler;

	protected static class HandlerInner<T  extends Context> extends Handler {

		private WeakReference<AbsHandlerThreadHelper<T>> mWeakReference;

		public HandlerInner(AbsHandlerThreadHelper<T> handlerThreadHelper) {
			super();
			this.mWeakReference = new WeakReference<AbsHandlerThreadHelper<T>>(handlerThreadHelper);
		}

		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			AbsHandlerThreadHelper<T> handlerThreadHelper = mWeakReference.get();
			if (handlerThreadHelper != null) {
				handlerThreadHelper.dispatchMessage(msg);
			}
		}
	}

	public AbsHandlerThreadHelper(T t) {
		super();
		this.mContextWeakReference = new WeakReference<T>(t);
		init();
	}

	private void init() {
		mExecutorService = Executors.newCachedThreadPool();
		mHandler = new HandlerInner<T>(this);
	}

	public T getContext() {
		return mContextWeakReference.get();
	}

	public abstract void dispatchMessage(Message msg);

	public void onPreDestroy() {
		onDestroy();
		if (mExecutorService != null) {
			mExecutorService.shutdown();
		}
		mExecutorService = null;
		if (mHandler != null) {
			mHandler.removeCallbacksAndMessages(null);
		}
		mHandler = null;
		if (mContextWeakReference != null) {
			mContextWeakReference.clear();
		}
		mContextWeakReference = null;
	}

	public abstract void onDestroy();
}

线程的基类

AbsThread.java

import java.lang.ref.WeakReference;

import android.content.Context;

public abstract class AbsThread<T extends Context> implements Runnable {

	protected WeakReference<AbsHandlerThreadHelper<T>> mReference;

	public AbsThread(AbsHandlerThreadHelper<T> handlerThreadHelper) {
		super();
		this.mReference = new WeakReference<AbsHandlerThreadHelper<T>>(handlerThreadHelper);
	}

	@Override
	public void run() {
		synchronized (this) {
			AbsHandlerThreadHelper<T> handlerThreadHelper = mReference.get();
			if (handlerThreadHelper != null) {
				runPro();
			}
		}
	}

	public  abstract  void runPro();
}

我用的是okhttp3的,所以封装多一个AbsOkHttpThread.java

import java.io.IOException;

import android.content.Context;
import android.os.Message;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public abstract class AbsOkHttpThread<T extends Context> extends AbsThread<T> implements Callback {

	private int success = 0;

	private int fail = -1;

	public AbsOkHttpThread(AbsHandlerThreadHelper<T> handlerThreadHelper) {
		super(handlerThreadHelper);
	}

	public AbsOkHttpThread(AbsHandlerThreadHelper<T> handlerThreadHelper, int success, int fail) {
		super(handlerThreadHelper);
		this.success = success;
		this.fail = fail;
	}

	@Override
	public void onFailure(Call call, IOException exception) {
		AbsHandlerThreadHelper<T> handlerThreadHelper = mReference.get();
		if (handlerThreadHelper != null) {
			handlerThreadHelper.mHandler.sendEmptyMessage(fail);
		}
	}

	@Override
	public void onResponse(Call call, Response response) throws IOException {
		try {
			AbsHandlerThreadHelper<T> handlerThreadHelper = mReference.get();
			if (handlerThreadHelper != null) {
				Message msg  = new Message();
				//TODO call
				final String result = response.body().string();
				msg.obj = result;
				msg.what = success;
				handlerThreadHelper.mHandler.sendMessage(msg);
			}
		} catch (Exception e) {
			onFailure(call, null);
		}
	}
}

例子DemoPresenter.java

import android.app.Service;
import android.os.Message;

public class DemoPresenter extends AbsHandlerThreadHelper<Service> {

	private TestRun testRun;

	public DemoPresenter(Service t) {
		super(t);
		testRun = new TestRun(this, 3, 2);
	}

	@Override
	public void dispatchMessage(Message msg) {
		LogDebug.e(getContext(), "DemoPresenter.dispatchMessage:" + msg.what);
	}

	@Override
	public void onDestroy() {

	}

	public void register() {
		mExecutorService.execute(testRun);
	}

	private static class TestRun extends AbsOkHttpThread<Service> {

		public TestRun(AbsHandlerThreadHelper<Service> handlerThreadHelper, int success, int fail) {
			super(handlerThreadHelper, success, fail);
		}

		@Override
		public void runPro() {
			AbsHandlerThreadHelper<Service> absHandlerThreadHelper = mReference.get();
			Service context = absHandlerThreadHelper.getContext();
			if (context != null) {
				OkHttp3Utils.getInstance(context).doPostPro("http://www.google.com", null, "{}", this);
			}
		}
	}
}

 

共有 人打赏支持
粉丝 52
博文 335
码字总数 116759
×
executor
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: