平时的app网络操作一般有几种方式: new Thread() + handler ; new AsyncTask().......
我常用的方式是new AsyncTask(), 可能比较方便吧,弊端:就是一旦请求,没有焦点,只能等待黑圈转完;new Thread() 好处是好控制,但是用多了占内存;
前几天跟同事讨论了下,翻看了下源码,方案来了!
我们要实现的就是我不想请求了,按下back键,请求终止,然后焦点出现:
来段登录请求的源码:
package com.bt.property.asynctask;
/**
* 异步任务-登录
* @author 24K
* @created 2013年11月20日15:02:17
*/
public class LoginAsyncTask extends AsyncTask<String, Void, User> {
protected static final String TAG = LoginAsyncTask.class.getSimpleName();
private ProgressDialog dialog;
private int result;
private Context activity;
private LoginService loginService = new LoginService();
private User userInfo;
private SharedPreferencesUtil sp;
private boolean isRemember;//是否记住密码
public LoginAsyncTask(Context mContext, boolean isRemember) {