«

android对handler及asynctask的一个例子

时间:2024-3-2 19:28     作者:韩俊     分类: Android


Android子线程更新UI主线程方法之Handler
Android之多线程工作-AsyncTask与handler
详解Android中AsyncTask的使用
Android线程和Handler基础入门
Android中Handler的机制

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多线程demo。"/>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="async Task实时更新进度条" android:id="@+id/btn_async_task"/>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消任务" android:id="@+id/btn_cancel_async_task"/>
    </LinearLayout>

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="执行handler任务2" android:id="@+id/btn_handler"/>
    </LinearLayout>

<ProgressBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:progress="0" android:max="100" android:id="@+id/progress_bar_1" style="?android:attr/progressBarStyleHorizontal"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tips_1"/>
</LinearLayout>
package com.example.MyStudyAndroid;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ThreadDemo extends Activity {

    private static final String TAG="thread demo";

    private Button btn_async_task;
    private ProgressBar progressBar_1;
    private TextView tips_1;
    private Button btn_cancel_async_task;

    private Task1 myTask;

    private Button btn_handler;

    private Handler handler=new Handler();
    //--耗时的操作。
    Runnable longOperation=new Runnable() {
        @Override
        public void run() {

            handler.post(new Runnable() {
                @Override
                public void run() {
                    tips_1.setText("开始执行任务2");
                }
            });
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    tips_1.setText("这里已经任务2完成了。");
                }
            },5000);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.thread_demo);
        initUI();
        initEvents();
    }

    private void initUI(){
        btn_async_task=(Button)findViewById(R.id.btn_async_task);
        progressBar_1=(ProgressBar)findViewById(R.id.progress_bar_1);
        tips_1=(TextView)findViewById(R.id.tips_1);
        btn_cancel_async_task=(Button)findViewById(R.id.btn_cancel_async_task);
        btn_handler=(Button)findViewById(R.id.btn_handler);
    }
    private void initEvents(){
        btn_async_task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myTask=new Task1();
                myTask.execute("");
            }
        });
        btn_cancel_async_task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(myTask!=null){
                    myTask.cancel(true);
                }
            }
        });
        btn_handler.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(longOperation).start();
            }
        });
    }

    class Task1 extends AsyncTask<String,Integer,String>{

        //--onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute(){
            Log.e(TAG,"task1的onPreExecute开始执行:");
            tips_1.setText("开始执行 async task 任务。。。");

        }
        //--doInBackground方法内部执行后台任务,不可在此方法内修改UI
        @Override
        protected String doInBackground(String... strings) {
            try {
                Thread.sleep(500);
                publishProgress(10);
                Thread.sleep(600);
                publishProgress(22);
                Thread.sleep(1000);
                publishProgress(42);
                Thread.sleep(50);
                publishProgress(43);;
                Thread.sleep(250);
                publishProgress(48);
                Thread.sleep(1000);
                publishProgress(68);
                Thread.sleep(500);
                publishProgress(78);
                Thread.sleep(100);
                publishProgress(80);
                Thread.sleep(250);
                publishProgress(85);
                Thread.sleep(500);
                publishProgress(95);
                Thread.sleep(150);
                publishProgress(98);
                Thread.sleep(100);
                publishProgress(100);
            }
            catch (Exception ed){
                ed.printStackTrace();
            }
            return "执行完毕了吧?啊啊啊。。";
        }

        ////onProgressUpdate方法用于更新进度信息
        @Override
        protected void onProgressUpdate(Integer... values){
            progressBar_1.setProgress(values[0]);
            tips_1.setText(String.valueOf("加载。。。"+values[0]+"%"));
        }

        ////onPostExecute方法用于在执行完后台任务后更新UI,显示结果
        @Override
        protected void onPostExecute(String result){
            Log.e(TAG,result);
            tips_1.setText(result);
        }
        //onCancelled方法用于在取消执行中的任务时更改UI
        @Override
        protected void onCancelled(){
            tips_1.setText("取消任务");
            progressBar_1.setProgress(0);
        }
    }

}
        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

标签: android

热门推荐