async的用法

package com.example.administrator.myapplication;

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

import java.io.IOException;


public class DownloadTask extends AsyncTask<Object,Integer,Boolean> {
    private TextView tv;

    @Override
    protected Boolean doInBackground(Object... params) {
        tv=(TextView) params[0];
        int percent=0;
        try{
            while (true){
                percent++;
                Thread.sleep(1000);
                publishProgress(percent);
                if(percent>=100){
                    break;
                }
            }
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    private static final String TAG = "DownloadTask";
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        tv.setText(values[0].toString());
        
        Log.d(TAG, "onProgressUpdate: "+values[0]);
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        Log.d(TAG, "onPostExecute: ");
    }
}

 

posted @ 2018-01-06 21:10  layfork  阅读(182)  评论(0编辑  收藏  举报