Android:使用runOnUIThread

时间:2016-06-09 20:12:04

标签: java android android-asynctask

有一个应用程序处理AsyncTask中有runOnUIThread个调用的许多操作。 该应用程序的代码在后台进行的过程中显示不确定的ProgressBar,但进度条始终未显示,有的情况是肯定的,在其他情况下没有。

使用这种(坏结构)代码模型,我怎么能确定始终显示进度条

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

public class AsyncTestActivity extends ActionBarActivity {
    private ProgressDialog pd = null;

    private void loadingBar(String titulo){
        pd = ProgressDialog.show(this, titulo, "Please wait ...", true, false);
    }

    private void closeLoading(){
        pd.dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_test);

        TextView textView = (TextView) findViewById(R.id.textView);

        Task async = new Task(textView);
        async.execute("");
    }

    private class Task extends AsyncTask<String, Void, String> {

        private TextView tv;

        public Task(TextView tv) {
            this.tv = tv;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loadingBar("Warning");//I want to show this always
        }

        @Override
        protected String doInBackground(String... params) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for(int i = 0; i <= 10; i++) {
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            return "End";
        }

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

我不能改变代码的结构(AsyncTask/runOnUIThread),因为它很大,我没有时间做这件事。目标是进度条总是如此。我一直在读这是一个不好的做法,但代码是由另一个人制作的。

2 个答案:

答案 0 :(得分:0)

loadingBar("Warning");之前致电async.execute("");

如果没有显示,则ProgressDialog存在问题。你能尝试编辑像这样的loadingBar()方法吗? :

ProgressDialog pd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_async_test);

    //init progress
    pd = new ProgressDialog(this);

    //
    TextView textView = (TextView) findViewById(R.id.textView);

    Task async = new Task(textView);
    async.execute("");
}

private void loadingBar(String titulo){
    pd.show();
    pd.setTitle(titulo);
}

答案 1 :(得分:0)

您是否尝试过调用bringToFront()中的onPreExecute()

@Override
    protected void onPreExecute() {
        super.onPreExecute();
        loadingBar.bringToFront();
        loadingBar("Warning");//I want to show this always
    }

(未经测试的代码)

或者如果您想确定,可以将progressBar包含为Task参数,然后直接执行bringToFront();

public Task(TextView tv, ProgressBar pB) {
            this.tv = tv;
            this.pb = pB.bringToFront();
        }

希望有所帮助