package com.example.testasynctask;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.widget.ProgressBar;import android.app.Activity;public class MainActivity extends Activity {// 要请求的urlprivate final String[] urlStr = new String[];private ProgressBar pBar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pBar = (ProgressBar) findViewById(R.id.progress);new MyAsyncTask().execute(urlStr);}//定义异步任务private class MyAsyncTask extends AsyncTask<String, Integer, Long> {//doInBackground方法前执行,可启动progressBar等@Overrideprotected void onPreExecute() {// TODO Auto-generated method stubsuper.onPreExecute();System.out.println("onPreExecute");}//doInBackground方法后执行,可在此关闭ProgressBar等等@Overrideprotected void onPostExecute(Long result) {// TODO Auto-generated method stubsuper.onPostExecute(result);System.out.println("onPostExecute :"result);pBar.setProgress();pBar.setVisibility(View.GONE);}//可在此更新UI@Overrideprotected void onProgressUpdate(Integer... values) {// TODO Auto-generated method stubsuper.onProgressUpdate(values);System.out.println("onProgressUpdate: " values[0]);pBar.setProgress(values[0]);}@Overrideprotected void onCancelled() {// TODO Auto-generated method stubsuper.onCancelled();System.out.println("onCancelled");}//执行耗时任务@Overrideprotected Long doInBackground(String... params) {// TODO Auto-generated method stubSystem.out.println("doInBackground");for (int i = 0; i < ; i) {urlStr[i] = " {for (int i = 0; i < params.length; i) {if(isCancelled()) break;String urlStr = params[i];URL url = new URL(urlStr);URLConnection conn = url.openConnection();InputStream is = conn.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String line;while ((line = br.readLine()) != null) {System.out.println(line);}is.close();//调用之后会执行onProgressUpdate方法publishProgress(i);}} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//这里返回的作为onPostExecute方法的参数return (long) params.length;}}}
推荐整理分享异步任务(异步任务管理器),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:异步任务有哪些,异步任务队列,异步任务框架,异步任务有哪些,异步任务处理机制理解,异步任务管理器,异步任务处理机制理解,异步任务处理机制理解,内容如对您有帮助,希望把文章链接给更多的朋友!
版权声明:本文为博主原创文章,未经博主允许不得转载。
学习笔记-注解+反射读取Bean中数据 我们经常有从数据源(即javabean中拿数据)的需要,但不同的人对bean中内容的命名五花八门,但利用注解反射可以写出通用的提取数据的代码。假设需
Android--Tomcat各种启动错误的解决办法,如:Exception in thread "Thread-6" NoClassDefFoundError,Document base E: 上午遇到一个棘手的事儿,导入一个工程,结果把原有的Tomcat服务器给导坏了,各种红的、黑的、蓝的错误满天飞啊,刚弄完一个项目,怕被毁了,我
Android 多个ImageView图片层叠透明区域点击事件穿透 经常用到多个透明图片层叠,但又需要获取不同图片的点击事件,本文实现图片透明区域穿透点击事件。实现人体各个部位点击内部类privateclassMyOnTouchLi