Android Asyntask异步线程操作

上一篇 / 下一篇  2013-01-31 10:30:04 / 个人分类:android开发

<?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" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:progress="10"
        android:max="500" />
    <Button android:id="@+id/btn_click"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="toStart"></Button>
</LinearLayout>

package com.src.hero.org;

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

public class AndroidAsynActivity extends Activity {
    /** Called when the activity is first created. */
TextView tv_content;
ProgressBar prpbar;
LinearLayout layout1;
Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv_content=(TextView)findViewById(R.id.tv);
        prpbar=(ProgressBar)findViewById(R.id.progressBar1);
        btn=(Button)findViewById(R.id.btn_click);
        btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Myasyn(tv_content, prpbar).execute(1000);
}
});
    }
}


package com.src.hero.org;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Myasyn extends AsyncTask<Integer, Integer,Integer>{
private static final String TAG="Myasyn";
    TextView tv;
    ProgressBar pb;
public Myasyn(TextView tv,ProgressBar pb){
this.tv=tv;
this.pb=pb;
}
@Override
protected Integer doInBackground(Integer... params) {
// TODO Auto-generated method stub
int time=params[0];
for(int i=0;i<=pb.getMax();i+=10){
pb.setProgress(i);
publishProgress(i);//调用后面的progressUpdata
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

return 100;
}
    /**
     * 开始调用此方法
     */
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
tv.setText("异步开始");
super.onPreExecute();
}


     /**
      * 结束时调用
      */
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
Log.v(TAG, "----post:"+result);
super.onPostExecute(result);
}

@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
int i=values[0];
Log.v(TAG, "to:"+i);
int b=i/5;
tv.setText(b+"%");
super.onProgressUpdate(values);
}

}

TAG:

 

评分:0

我来说两句

Open Toolbar