为什么当我从活动onResume调用AsyncTask时,我没有看到progressdialog?

时间:2012-07-02 16:39:10

标签: android android-asynctask android-activity progressdialog

当用户按下我的应用程序中的选项卡时 - 我想要启动后台任务,这会显示进度条。

我是通过从我的活动的onResume()启动AsyncTask来实现的。问题是执行此操作时未显示我的进度对话框 - 后台任务成功运行,并且在运行onPostExecute之后,焦点将返回到我的活动,应用程序将继续正常运行。

如何从onResume =启动AsyncTask,或者在启动活动时仍显示onPreExecute上创建的活动布局/进度条?

目前代码的行为如下:

http://pastebin.com/KUsg3Mri但是当我更改tab并调用onCreate时,asyntask将启动 - 在asynctask完成时,不会更改选项卡的内容或显示进度条 - 然后活动将显示其所有gui元素成功

好像我这样做:http://pastebin.com/KUsg3Mri并从onCreate或onResume启动findReplays - 这将显示进度对话框 - 但是publishProgress从不调用onProgressUpdate - 所以它更近,但没有雪茄!

2 个答案:

答案 0 :(得分:1)

试试这个......

在从UI线程执行AsyncTask<> 的execute()方法之前使用 ProgressDiaglog初始化,并在AsyncTask<>的doInBackground()方法中使用调用dismiss()。在退货声明之前......

更好地解释它的一个例子......

public class AsyncTaskExampleActivity extends Activity 
{
        protected TextView _percentField;
        protected Button _cancelButton;
        protected InitTask _initTask;
        ProgressDialog pd;

    @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();


         pd = ProgressDialog.show(AsyncTaskExampleActivity.this, "Loading", "Please Wait");


        _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() );
                                }
                        }
                        pd.dismiss(); 
                        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 );
                }
    }    
}

答案 1 :(得分:0)

OK!最终通过android-dev IRC频道找到了解决方案。

任务正在工作但progressPublish调用没有过滤回UI的原因是因为我正在调用AsyncTask执行,然后立即调用.get()方法 - 阻止 - 在asynctask中绑定UI线程我试图用它!

所以我删除了.get()方法,并将相关代码段放入onPostExecute中 - 所有这些都完美无缺!