前端之家收集整理的这篇文章主要介绍了
Android AsyncTack 异步任务实例详解,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Android AsyncTack 异步任务
这里写一个小实例,来学习巩固Android AsyncTack 异步任务的知识,以便在项目中使用。
介绍一下如何使用
1,继承AsyncTask
我们来说一下这三个泛型的作用:
Params: 调用异步任务时传入的类型 ;
Progress : 字面意思上说是进度条,实际上就是动态的由子线程向主线程publish数据的类型
Result : 返回结果的类型
2,重写这个类的抽象方法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 "已完成";
}
下面三个方法根据具体情况选择使用
调用
@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,执行异步任务
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);
注意: 如果我们直接去execute我们的任务,它(任务) 只会在同一个子线程中运行,所以上述第一种方式启动时,四个任务顺次执行(就是一个任务执行完了再执行另一个); 而第二种方式,给它创建了线程池,这样会自动给它创建新的子线程,所有的任务不是顺序执行,而是几个线程”同时执行”
获取网络数据呈现在Webview和下载图片和其共存的案例
1,首先我们要来一个布局,具体需求是这样的,在WebView之上有个ImageView,并且,ImageView可以随WebView滚动,所以这个时候我们想到了用ScrollView,但是大家一定不要忘记,ScrollView只能包含一个控件,所以我们可以用LinearLayout包裹一下即可
301_73@,Void,Object> {
private NetWorkTask.Callback@H_30173@ callback;
private Class@H301_73@ t;
private String url;
public NetWorkTask(String url,Class@H_30173@ t) {
this.url = url;
this.t = t;
}
@Override
protected Object doInBackground(Callback@H301_73@... 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,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);
}
}