非UI线程更改UI线程3

Andriod提供了一个叫做AsyncTask的类,专门用于完成非UI线程,更新UI线程,她是个抽象类,需要首先子类化

AsyncTask定义了三种泛型类型 Params,Progress和Result。

        Params 启动任务执行的输入参数,比如HTTP请求的URL。

        Progress 后台任务执行的百分比。 

        Result 后台执行任务最终返回的结果,比如String。


她的几个主要方法

onPreExecute:开始执行前的准备工作

doInbackground()开始执行执行后的后台操作,在里面可以调用publishProgress()方法更新适时地任务进度

onProgressUpdate()在publishProgress()被调用后,UI线程将调用这个方法从而在页面上展示任务进度,

onPostExecute()执行完成后的操作,传送结果给UI线程

代码:

package com.example.change_ui3;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Main extends Activity {

	private TextView tv;
	private ProgressBar bar;
	private Button btn;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv=(TextView) findViewById(R.id.tv_id);
		bar=(ProgressBar) findViewById(R.id.progressBar1);
		btn=(Button) findViewById(R.id.button1);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MyAsyncTask asyncTask=new MyAsyncTask(tv, bar);
				asyncTask.execute(1000);
				
				
				
			}
		});
	}

	

}
子类化AsyncTask

package com.example.change_ui3;

import android.R.integer;
import android.os.AsyncTask;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MyAsyncTask extends AsyncTask<Integer, Integer, String> {
    private TextView tv;
    private ProgressBar bar;
    
    public MyAsyncTask(TextView tv,ProgressBar bar){
    	this.tv=tv;
    	this.bar =bar;
    }
	
	protected String doInBackground(Integer... params) {
		int i;
		for( i=0;i<100;i++){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			publishProgress(i);
			
		}
		
		return i+params[0].intValue()+"";
	}

	@Override
	protected void onPreExecute() {
		tv.setText("执行异步线程");
		
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(String result) {
		tv.setText(result);
		super.onPostExecute(result);
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		bar.setProgress(values[0].intValue());
	}
	

}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_id"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tv_id"
        android:layout_marginTop="149dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBar1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="106dp"
        android:layout_marginLeft="22dp"
        android:text="Button" />

</RelativeLayout>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值