怎样的android与asynctasks progressdialog工作 [英] android how to work with asynctasks progressdialog

查看:98
本文介绍了怎样的android与asynctasks progressdialog工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AsyncTask的有4个替代方法在preExecute() doInBackground() onProgressUpdate() onPostExecute() 除了 onProgressUpdate 所有工作。 我应该怎么做,这样onProgressUpdate()应该工作。 任何人都可以请简单介绍一下我有什么用 onProgressUpdate(),我应该在此写?

Asynctask have 4 override methods onPreExecute(), doInBackground(), onProgressUpdate(), onPostExecute() except onProgressUpdate all are working. What should I do so that onProgressUpdate() should work. Can anybody please briefly explain me what's the use of onProgressUpdate(), what should write within this?

推荐答案

onProgressUpdate()用于通过这种方法来操作异步操作的进度。注意:由于数据类型的参数整数。这对应于类定义第二个参数。这个回调可以从 doInBackground()方法体内调用触发 publishProgress()

onProgressUpdate() is used to operate progress of asynchronous operations via this method. Note the param with datatype Integer. This corresponds to the second parameter in the class definition. This callback can be triggered from within the body of the doInBackground() method by calling publishProgress().

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

public class AsyncTaskExample extends Activity {

    protected TextView _percentField;

    protected Button _cancelButton;

    protected InitTask _initTask;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        _percentField = (TextView) findViewById(R.id.percent_field);
        _cancelButton = (Button) findViewById(R.id.cancel_button);
        _cancelButton.setOnClickListener(new CancelButtonListener());
        _initTask = new InitTask();
        _initTask.execute(this);
    }

    protected class CancelButtonListener implements View.OnClickListener {

        public void onClick(View v) {
            _initTask.cancel(true);
        }
    }

    /**
     * sub-class of AsyncTask
     */
    protected class InitTask extends AsyncTask<Context, Integer, String> {

        // -- run intensive processes here
        // -- notice that the datatype of the first param in the class definition matches the param passed to this
        // method
        // -- and that the datatype of the last param in the class definition matches the return type of this method
        @Override
        protected String doInBackground(Context... params) {
            // -- on every iteration
            // -- runs a while loop that causes the thread to sleep for 50 milliseconds
            // -- publishes the progress - calls the onProgressUpdate handler defined below
            // -- and increments the counter variable i by one
            int i = 0;
            while (i <= 50) {
                try {
                    Thread.sleep(50);
                    publishProgress(i);
                    i++;
                }
                catch (Exception e) {
                    Log.i("makemachine", e.getMessage());
                }
            }
            return "COMPLETE!";
        }

        // -- gets called just before thread begins
        @Override
        protected void onPreExecute() {
            Log.i("makemachine", "onPreExecute()");
            super.onPreExecute();
        }

        // -- called from the publish progress
        // -- notice that the datatype of the second param gets passed to this method
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            Log.i("makemachine", "onProgressUpdate(): " + String.valueOf(values[0]));
            _percentField.setText((values[0] * 2) + "%");
            _percentField.setTextSize(values[0]);
        }

        // -- called if the cancel button is pressed
        @Override
        protected void onCancelled() {
            super.onCancelled();
            Log.i("makemachine", "onCancelled()");
            _percentField.setText("Cancelled!");
            _percentField.setTextColor(0xFFFF0000);
        }

        // -- called as soon as doInBackground method completes
        // -- notice that the third param gets passed to this method
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.i("makemachine", "onPostExecute(): " + result);
            _percentField.setText(result);
            _percentField.setTextColor(0xFF69adea);
            _cancelButton.setVisibility(View.INVISIBLE);
        }
    }
}

这篇关于怎样的android与asynctasks progressdialog工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆