Progressdialog增加连接超时接口

系统自带的Progressdialog是没有连接超时的,这个使用起来有点不方便,要增加这样的功能,我们就需要重写Progressdialog,下面代码介绍如何实现:



import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

/**
 * 自定义带连接超时接口的ProgressDialog
 * @author fanxl
 *
 */
public class MyProgressDialog extends ProgressDialog {

	private long mTimeOut = 0;// 默认timeOut为0, 即无限大
	private OnTimeOutListener mTimeOutListener = null;// timeOut后的处理器
	private Timer mTimer = null;// 定时器

	@SuppressLint("HandlerLeak") private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (mTimeOutListener != null) {
				mTimeOutListener.onTimeOut(MyProgressDialog.this);
				dismiss();
			}
		}
	};

	public MyProgressDialog(Context context) {
		super(context);
		// 设置进度条风格,风格为圆形,旋转的
		this.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		// 设置ProgressDialog 标题
		this.setTitle("提示");
		// 设置ProgressDialog 提示信息
		this.setMessage("正在加载数据中,请稍后");
		// 设置ProgressDialog 的进度条是否不明确
		this.setIndeterminate(false);
		// 设置ProgressDialog 是否可以按退回按键取消
		this.setCancelable(false);
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		if (mTimeOut != 0) {
			mTimer = new Timer();
			TimerTask timerTast = new TimerTask() {
				@Override
				public void run() {
					// dismiss();
					Message msg = mHandler.obtainMessage();
					mHandler.sendMessage(msg);
				}
			};
			mTimer.schedule(timerTast, mTimeOut);
		}

	}

	/**
	 * 设置timeOut长度,和处理器
	 * 
	 * @param t
	 *            timeout时间长度
	 * @param timeOutListener
	 *            超时后的处理器
	 */
	public void setTimeOut(long t, OnTimeOutListener timeOutListener) {
		mTimeOut = t;
		if (timeOutListener != null) {
			this.mTimeOutListener = timeOutListener;
		}
	}

	/**
	 * 
	 * 处理超时的的接口
	 * 
	 */
	public interface OnTimeOutListener {

		/**
		 * 当progressDialog超时时调用此方法
		 */
		abstract public void onTimeOut(MyProgressDialog dialog);
	}

	/**
	 * 通过静态Create的方式创建一个实例对象
	 * 
	 * @param context
	 * @param time
	 *            timeout时间长度
	 * @param listener
	 *            timeOutListener 超时后的处理器
	 * @return MyProgressDialog 对象
	 */
	public static MyProgressDialog createProgressDialog(Context context,
			long time, OnTimeOutListener listener) {
		MyProgressDialog progressDialog = new MyProgressDialog(context);
		if (time != 0) {
			progressDialog.setTimeOut(time, listener);
		}
		return progressDialog;
	}

}
上面自定义的MyProgressDialog增加了一些样式,一些属性,在构造方法里面设置,另外增加一个OnTimeOutListener listener的连接超时回调接口,使用静态方法即可获取MyProgressDialog,下面是使用:

<pre name="code" class="java">MyProgressDialog <span style="font-family: Arial, Helvetica, sans-serif;">progressDialog = MyProgressDialog.createProgressDialog(getActivity(),  10000, new OnTimeOutListener() {</span>
@Overridepublic void onTimeOut(MyProgressDialog dialog) {Utils.toast(getActivity(), "连接超时,请重试!");}});

 传入参数,上下文,10秒的连接超时时间,还有就是连接超时的处理接口,这里知识一个吐司提醒用户连接超时。 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值