Android网络请求回调没正常走 处理方案

原文链接: https://my.oschina.net/moziqi/blog/1524476

处理类

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

/**
 * 
 * 考虑可能回调没正常执行
 * 
 * @author ziqi-mo
 *
 */
public class TimerOutNextHandle {

	private Handler mH;

	private CallBack mCallBack;

	private NextCallBack mNextCallBack;

	private volatile static TimerOutNextHandle instance;

	public static TimerOutNextHandle getInstance() {
		if (instance == null) {
			synchronized (TimerOutNextHandle.class) {
				if (instance == null) {
					instance = InnerTimerOutNextHandle.mTimerOutNextHandle;
				}
			}
		}
		return instance;
	}

	static class InnerTimerOutNextHandle {
		private static TimerOutNextHandle mTimerOutNextHandle = new TimerOutNextHandle();
	}

	public void init() {
		if (mH != null) {
			mH.removeCallbacksAndMessages(null);
			mH = null;
		}
		if (mH == null) {
			mH = new Handler(Looper.getMainLooper()) {

				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					handleMessagePro(msg);
				}
			};
		}
	}

	private void handleMessagePro(Message msg) {
		switch (msg.what) {
		case Status.CURRENT_CODE:
			extra(msg);
			break;
		case Status.NEXT_CODE:
			extra(msg);
			break;
		default:
			extra(msg);
			break;
		}
	}

	private void extra(Message msg) {
		if (msg.arg1 > 0) {
			if (mH != null) {
				mH.removeMessages(msg.arg1);
			}
			mNextCallBack = null;
			if (mCallBack != null) {
				mCallBack.go();
				mCallBack = null;
			}
		} else {
			mCallBack = null;
			if (mNextCallBack != null) {
				mNextCallBack.goNext();
				mNextCallBack = null;
			}
			if (mH != null) {
				mH.removeCallbacksAndMessages(null);
				mH = null;
			}
		}
	}

	public void sendWhat(int currentWhat, int nextPriorityNext, final CallBack callBack) {
		if (mH != null) {
			Message message = new Message();
			message.what = currentWhat;
			message.arg1 = nextPriorityNext;
			mH.sendMessage(message);
			if (callBack != null) {
				mCallBack = callBack;
			}
		}
	}

	public void sendNextWhatDelay(final int nextPriorityNext, long delay, final NextCallBack callBack) {
		if (mH != null) {
			Message message = new Message();
			message.what = nextPriorityNext;
			message.arg1 = -1;
			mH.sendMessageDelayed(message, delay);
			if (callBack != null) {
				mNextCallBack = callBack;
			}
		}
	}

	public interface CallBack {
		public void go();
	}

	public interface NextCallBack {
		public void goNext();
	}

	public static class Status {

		public final static int CURRENT_CODE = 10001;

		public final static int NEXT_CODE = 20001;

	}

}

使用方法

		// 放在 网络请求 前
		TimerOutNextHandle.getInstance().init();
		// TODO 网络
		//.......
		final int timer = (int) (1 + Math.random() * 10);
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				// 模拟在onSuccess /onFail 回调
				TimerOutNextHandle.getInstance().sendWhat(Status.CURRENT_CODE, Status.NEXT_CODE, new CallBack() {

					@Override
					public void go() {
						Toast.makeText(getApplicationContext(), "我不超时" + timer, Toast.LENGTH_SHORT).show();
					}
				});
			}
		}, timer * 100);
		// 放在网络请求后 超时的操作,可以设置时间
		TimerOutNextHandle.getInstance().sendNextWhatDelay(Status.NEXT_CODE, 500, new NextCallBack() {

			@Override
			public void goNext() {
				Toast.makeText(getApplicationContext(), "我超时了" + timer, Toast.LENGTH_SHORT).show();
			}
		});

 

转载于:https://my.oschina.net/moziqi/blog/1524476

你可能感兴趣的:(Android网络请求回调没正常走 处理方案)