问:用实际结果更新 ProgressBar(包括代码)

Q: Updating a ProgressBar with real results (includes code)

我正在设计一个加载我的应用程序的功能性启动画面,上面有一个 ProgressBar

根据官方 Android 开发者文档,我使用 Thread+Runnable 来完成我的加载工作,并设置了 ProgressBar.

下面我的代码中的所有内容都直接来自 Android 文档。

我的问题在我的代码之后..

这是我当前的代码:

public class SplashActivity extends AppCompatActivity {

private static final int PROGRESS = 0x1;

private ProgressBar mProgress;

private int mProgressStatus = 0;

private Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_splash);

    mProgress = (ProgressBar) findViewById(R.id.progressBar);

    new Thread(new Runnable() {
        public void run() {
            while (mProgressStatus < 100) {

                mProgressStatus = doWork();

                // Update the progress bar 
                mHandler.post(new Runnable() {
                    public void run() {
                        mProgress.setProgress(mProgressStatus);
                    }
                });
            }
        }
    }).start();

}
// Set up a method here to do my loading work called doWork?
}

我的问题是:

为此我推荐AsyncTask,请参考下面的代码

public class SplashActivity extends AppCompatActivity {

private ProgressBar mProgress;

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_splash);
    mProgress = (ProgressBar) findViewById(R.id.progressBar);
    new DoWork().execute(this);
}
   protected class DoWork extends AsyncTask<Context, Integer, Void> {
        @Override
        protected String doInBackground(Context... params) {
             //do some task say 20% of complete task
              publishProgress(20);
            //do some task say 50% of complete task
        publishProgress(50);
        //do some task say 100% of complete task
        publishProgress(100);
            return null;
        }

        //Called just before execution begins
        @Override
        protected void onPreExecute() {
            Log.i("AsyncTask", "onPreExecute()");
        mProgress.show();
            super.onPreExecute();
        }

        //called from the publish progress
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values); 
        mProgress.setProgress(values[0]);           
        }

    // when execution is completed
        @Override
        protected void onPostExecute(Void param) {
            super.onPostExecute(result);
            mProgress.dismiss();
        }
    }
}

编辑

按照您的方式很简单,doWork() 在技术上不是一种方法。 它是您执行内联的后台任务,如

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_splash);

    mProgress = (ProgressBar) findViewById(R.id.progressBar);

    new Thread(new Runnable() {
        public void run() {
            while (mProgressStatus < 100) { // Show progress till its 100
                //for example your downloading something, everytime you hit the while loop ,you'll update the mProgressStatus to current progress.
                //I need to download file of size 10mb
                //every time while loops execute , you calcluate size downloaded say 1mb i.e.. 1mb of 10mb means 10% progressed so set mProgressStatus to 10
                int percentageOfFileDownloaded = (downloadedfileSize/TotalFileSize) * 100; //Just for example
                mProgressStatus = percentageOfFileDownloaded;
                  //At one moment your entire file will be downloaded (10mb of 10mb) so update mProgressStatus to 100; it will break while loop.
                // Update the progress bar 
                mHandler.post(new Runnable() {
                    public void run() {
                        mProgress.setProgress(mProgressStatus);
                    }
                });
            }
        }
    }).start();

}
}