android - 为什么当我从 onResume Activity 调用 AsyncTask 时,我看不到进度对话框?

标签 android android-asynctask android-activity progressdialog

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

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

我如何从 onResume = 启动 AsyncTask 或者当 Activity 开始时仍然显示在 onPreExecute 上创建的 Activity 布局/进度条?

目前代码的作用如下:

http://pastebin.com/KUsg3Mri然而,当我更改选项卡并调用 onCreate 时,asyntask 被启动 - 从未更改选项卡的内容,或显示进度条,当 asynctask 完成时 - 然后 Activity 成功显示其所有 gui 元素

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

最佳答案

试试这个....

在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 );
                }
    }    
}

关于android - 为什么当我从 onResume Activity 调用 AsyncTask 时,我看不到进度对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11297734/

相关文章:

android - 将私有(private)存储的图像分享到外部应用程序 - Android

java - 如何在测试 android 应用程序时隐藏 LeanFT 中的键盘?

java - 当类扩展了LinearLayout时如何将类作为 Activity 调用

android - 在 Android 中检索 AsyncTask 方法内的值

android - 将 Activity Context 存储在 DialogFragment 中会导致内存泄漏吗?

java - 在布局中使用表面 View 隐藏/显示软键盘时的黑色区域

android - 从多个表的 SQLite DB 初始化应用程序?

Android:来自错误线程的 Realm 访问。 Realm 对象只能在创建它们的线程上访问

java - 如何将当前位置从一项 Activity 发送到另一项 Activity ?

android - 当我从一项 Activity 转到另一项 Activity 时如何显示警报对话框?