1

当用户在我的应用程序中按下选项卡时 - 我希望启动一个显示进度条的后台任务。

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

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

目前代码的作用如下:

http://pastebin.com/KUsg3Mri但是,当我更改选项卡并调用 onCreate 时,异步任务被启动 - 无需更改选项卡的内容或显示进度条,当异步任务完成时 - 活动然后显示其所有 gui元素成功

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

4

2 回答 2

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 );
                }
    }    
}
于 2012-07-02T17:14:54.500 回答
0

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

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

所以我删除了 .get() 方法,并将相关的代码部分放入 onPostExecute - 一切正常!

于 2012-07-03T23:55:52.433 回答