Progressdialog增加连接超时接口
来源:互联网 发布:安徽11选五任3遗漏数据 编辑:程序博客网 时间:2024/05/17 08:24
系统自带的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() {@Overridepublic 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);}@Overrideprotected void onStop() {super.onStop();if (mTimer != null) {mTimer.cancel();mTimer = null;}}@Overridepublic void onStart() {super.onStart();if (mTimeOut != 0) {mTimer = new Timer();TimerTask timerTast = new TimerTask() {@Overridepublic 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秒的连接超时时间,还有就是连接超时的处理接口,这里知识一个吐司提醒用户连接超时。
0 0
- Progressdialog增加连接超时接口
- progressdialog超时接口编写
- 设置超时的progressdialog
- 自定义ProgressDialog,实现超时功能
- 如何修改TIME_WAIT超时时间以增加连接数
- android关于手机连接http接口超时,虚拟机连接正常的原因
- ProgressDialog
- ProgressDialog
- ProgressDialog
- progressDialog
- ProgressDialog
- ProgressDialog
- progressDialog
- ProgressDialog
- ProgressDialog
- ProgressDialog
- ProgressDialog
- ProgressDialog
- count(*)有时会影响结果,当然这是我自己的不小心!粘贴代码忘改了!最后发现问题
- Ubuntu64位下使用Eclipse闪退的解决
- spring中有三中实例化bean的方式
- 图片格式与算法
- Away3D之-----------------DefaultRenderer VS DeferredRender
- Progressdialog增加连接超时接口
- iPhone6分辨率与适配
- Xcode6 之后 PCH 文件创建
- iphone 获取手机运营商信息
- 谈谈C/C++中的排序函数的比较函数的写法。切忌不要走火入魔。
- iOS开发UI篇—CAlayer(自定义layer)
- java的String类中的其中一个subString方法的源码
- mysql索引需要了解的几个注意
- 神秘的40毫秒延迟与 TCP_NODELAY