Android中怎么利用AsyncTack 实现异步任务
创始人
2024-12-09 15:05:07
0次

1, 继承AsyncTask

public class MyTask extends AsyncTask

我们来说一下这三个泛型的作用:

Params: 调用异步任务时传入的类型 ;

Progress : 字面意思上说是进度条, 实际上就是动态的由子线程向主线程publish数据的类型

Result : 返回结果的类型

2, 重写这个类的抽象方法doInBackground, 当然它也有几个方法需要重写, 我们一一看来

doInBackground(抽象方法, 必须实现)

/* 唯一执行在子线程中的方法
 *  所以不可以进行UI的更新
 * @param params
 * @return
 */
@Override//返回值: Result    参数: Param
protected String doInBackground(TextView... params) {
  text = params[0];
  Random random = new Random();
  for (int i = 0; i < 50; i++) {
    //要进行进度的更新
    publishProgress(i);
    //不能直接调用onProgressUpdate方法,
    //这样会使得onProgressUpdate在子线程中运行
    try {
      Thread.sleep(random.nextInt(10) * 10);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  return "已完成";
}

下面三个方法根据具体情况选择使用

 //执行doInBackground之前调用
  @Override
  protected void onPreExecute() {
    super.onPreExecute();
  }
  @Override//与publishProgress(i)对应
  protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    text.setText(String.valueOf(values[0]));
  }
 //在doInBackground之后执行
  @Override // 参数s为 Result
  protected void onPostExecute(String s) {
    super.onPostExecute(s);
    text.setText(s);
  }

3, 执行异步任务

有两种方式, 我已经把区别写在了注释中
/*
 直接execute异步任务, 都是同一线程去执行
*/

text = (TextView) findViewById(R.id.main_text1);
new MyTask().execute(text);
text = (TextView) findViewById(R.id.main_text2);
new MyTask().execute(text);
text = (TextView) findViewById(R.id.main_text3);
new MyTask().execute(text);
text = (TextView) findViewById(R.id.main_text4);
new MyTask().execute(text);
/*
  启动多条线程来执行异步任务
  API11以上可以使用
*/
 ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(4);
 text = (TextView) findViewById(R.id.main_text1);
 new MyTask().executeOnExecutor(executor, text);
 text = (TextView) findViewById(R.id.main_text2);
 new MyTask().executeOnExecutor(executor, text);
 text = (TextView) findViewById(R.id.main_text3);
 new MyTask().executeOnExecutor(executor, text);
 text = (TextView) findViewById(R.id.main_text4);
 new MyTask().executeOnExecutor(executor, text);

注意: 如果我们直接去execute我们的任务, 它(任务) 只会在同一个子线程中运行, 所以上述第一种方式启动时, 四个任务顺次执行(就是一个任务执行完了再执行另一个); 而第二种方式, 给它创建了线程池, 这样会自动给它创建新的子线程, 所有的任务不是顺序执行, 而是几个线程”同时执行”

获取网络数据呈现在Webview和下载图片和其共存的案例

1, 首先我们要来一个布局, 具体需求是这样的, 在WebView之上有个ImageView, 并且, ImageView可以随WebView滚动, 所以这个时候我们想到了用ScrollView, 但是大家一定不要忘记, ScrollView只能包含一个控件, 所以我们可以用LinearLayout包裹一下即可


  
    
    
  

2, 接下来我们要有一个实体类, 用来存放从网页上下载的内容(这里加注解原因在于我们要使用GSON解析来自网页的内容)

public class Entry {
  @SerializedName("title")
  private String title;
  @SerializedName("message")
  private String message;
  @SerializedName("img")
  private String image;

  public String getTitle() {
    return title;
  }
  ...//省略其余getter和setter方法
  public void setImage(String image) {
    this.image = image;
  }
}

3, 那我们接下解决的问题就是 如何下载图片? 如何下载web内容? , 那我们写两个通用的工具类

下载工具类(通用型)

/**
 * Created by Lulu on 2016/8/31.
 * 

 * 通用下载工具类  */ public class NetWorkTask extends AsyncTask, Void, Object> {   private NetWorkTask.Callback callback;   private Class t;   private String url;   public NetWorkTask(String url, Class t) {     this.url = url;     this.t = t;   }   @Override   protected Object doInBackground(Callback... params) {     callback = params[0];     try {       HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();       connection.setRequestMethod("GET");       connection.setDoInput(true);       int code = connection.getResponseCode();       if (code == 200) {         InputStream is = connection.getInputStream();         ByteArrayOutputStream bos = new ByteArrayOutputStream();         byte[] buffer = new byte[102400];         int length;         while ((length = is.read(buffer)) != -1) {           bos.write(buffer, 0, length);         }         return bos.toString("UTF-8");       } else {         return new RuntimeException("服务器异常");       }     } catch (Exception e) {       e.printStackTrace();       return e;     }   }   @Override   protected void onPostExecute(Object o) {     super.onPostExecute(o);     if(o instanceof String) {       String str = (String) o;       Gson gson = new Gson();       T t = gson.fromJson(str, this.t);       callback.onSuccess(t);     }     if( o instanceof Exception) {       Exception e = (Exception) o;       callback.onFailed(e);     }   }   public interface Callback {     void onSuccess(S t);     void onFailed(Exception e);   } }

图片加载器(通用型)

/**
 * Created by Lulu on 2016/8/31.
 * 图片网络加载器
 * 下载成功返回Bitmap
 * 否则返回null
 */
public class ImageLoader extends AsyncTask{

  private ImageView image;

  public ImageLoader(ImageView image) {
    this.image = image;
    image.setImageResource(R.mipmap.ic_launcher);
  }

  @Override
  protected void onPreExecute() {
    super.onPreExecute();

  }

  @Override
  protected Bitmap doInBackground(String... params) {
    String url = params[0];
    try {
      HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
      connection.setRequestMethod("GET");
      connection.setDoInput(true);
      int code = connection.getResponseCode();
      if (code == 200) {
        InputStream is = connection.getInputStream();
        return BitmapFactory.decodeStream(is);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }


  @Override
  protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    if (bitmap != null) {
      image.setImageBitmap(bitmap);
    } else {
      image.setImageResource(R.mipmap.failed);
    }
  }
}

4, 测试Activity

注意: 看如何解决大图在webView中不左右滑动的问题!

public class Main2Activity extends AppCompatActivity implements NetWorkTask.Callback{
  private WebView web;
  private ImageView image;
  //解决大图在webView中不左右滑动的问题
  private static final String CSS = "";
  private String title;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    web = (WebView) findViewById(R.id.main2_web);
    image = (ImageView) findViewById(R.id.main2_image);
    new NetWorkTask<>("http://www.tngou.net/api/top/show?id=13122", Entry.class).execute(this);
  }
  @Override
  public void onSuccess(Entry t) {
    web.loadDataWithBaseURL("", t.getMessage(), "text/html; charset=utf-8", "UTF-8", null);
    new ImageLoader(image).execute("http://img.blog.csdn.net/20160829134937003");
  }
  @Override
  public void onFailed(Exception e) {
    web.loadDataWithBaseURL("", "加载失败", "text/html; charset=utf-8", "UTF-8", null);
  }
}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...