AsyncTask
来源:互联网 发布:司马懿 新旧知乎 编辑:程序博客网 时间:2024/06/15 05:03
public class MainActivity extends Activity {private static final String TAG = "ASYNC_TASK";private Button execute;private Button cancel;private ProgressBar progressBar;private TextView textView;private MyTask mTask; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); execute = (Button) findViewById(R.id.execute); execute.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//注意每次需new一个实例,新建的任务只能执行一次,否则会出现异常mTask = new MyTask();mTask.execute("http://developers.douban.com");execute.setEnabled(false);cancel.setEnabled(true);}}); cancel = (Button) findViewById(R.id.cancel); cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//取消一个正在执行的任务,onCancelled方法将会被调用mTask.cancel(true);}}); progressBar = (ProgressBar) findViewById(R.id.progress_bar); textView = (TextView) findViewById(R.id.text_view); } private class MyTask extends AsyncTask<String, Integer, String> { //1 onPreExecute方法用于在执行后台任务前做一些UI操作 @Override protected void onPreExecute() { Log.i(TAG, "onPreExecute() called"); textView.setText("loading..."); } //doInBackground方法内部执行后台任务,不可在此方法内修改UI@Overrideprotected String doInBackground(String... params) {Log.i(TAG, "doInBackground(Params... params) called");try {HttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(params[0]);HttpResponse response = client.execute(get);if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {HttpEntity entity = response.getEntity();InputStream is = entity.getContent();long total = entity.getContentLength();ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buf = new byte[1024];int count = 0;int length = -1;while ((length = is.read(buf)) != -1) {baos.write(buf, 0, length);count += length;//调用publishProgress公布进度,最后onProgressUpdate方法将被执行publishProgress((int) ((count / (float) total) * 100));//为了演示进度,休眠500毫秒Thread.sleep(500);}return new String(baos.toByteArray(), "utf-8");}} catch (Exception e) {Log.e(TAG, e.getMessage());}return null;}//onProgressUpdate方法用于更新进度信息@Override protected void onProgressUpdate(Integer... progresses) {Log.i(TAG, "onProgressUpdate(Progress... progresses) called");progressBar.setProgress(progresses[0]);textView.setText("loading..." + progresses[0] + "%"); } //onPostExecute方法用于在执行完后台任务后更新UI,显示结果@Overrideprotected void onPostExecute(String result) {Log.i(TAG, "onPostExecute(Result result) called");textView.setText(result);execute.setEnabled(true);cancel.setEnabled(false);}//onCancelled方法用于在取消执行中的任务时更改UI@Overrideprotected void onCancelled() {Log.i(TAG, "onCancelled() called");textView.setText("cancelled");progressBar.setProgress(0);execute.setEnabled(true);cancel.setEnabled(false);} }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/execute" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="execute" /> <Button android:id="@+id/cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:enabled="false" android:text="cancel" /> <ProgressBar android:id="@+id/progress_bar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="0" /> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/text_view" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </ScrollView></LinearLayout>
0 0
- AsyncTask
- ASyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- AsyncTask
- sort
- jquery bind('click')传参问题(例如:列表中每行绑定一个事件)
- Longest Palindromic Substring
- 音视频即时通讯解决方案
- Ogre 3DMax导出插件的制作
- AsyncTask
- 【windows】boost 1.4.9安装方法
- 再说注解
- 在一个UILabelView中实现不同的字体和颜色
- Zedboard & Zynq 图像采集 视频开发 (一) 硬件平台搭建
- SQL Server 存储过程之嵌套游标
- Oracle 11.2.0.3.0 RAC升级到11.2.0.4.0
- VS2010编译luabind
- hive rcfile存储格式