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

处理类

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值