进度betweed活动进度、betweed

2023-09-12 05:27:27 作者:你的眼里有星星呐

我有两个活动。第一个执行第二个

I have two activities. The first one executes the second one.

Intent i = new Intent(MyOne.this, MyTwo.class);
                     startActivity(i);

的问题:我的第二个活动确实在推出一些繁重的工作,所以它推出的第二对,它是发射之前我看到一个黑色的屏幕。

The problem: My second activity does some heavy work on launching so it launches couple of second and before it is launched i see a black screen.

这是任何方式来设置这个黑色屏幕的进度或一些图像呢?因为我不认为用户会等待一些东西,他不知道。我试过后设置进度的的setContentView 的在我的第二个活动,但进度条显示出来,只有当活动全面启动。

Is it any way to set a progressbar or some image instead of this black screen? Because i don't think user will wait for something, that he doesn't know. I tried setting progressbar after setcontentview in my second activity, but progressbar shows up only when activity fully started.

推荐答案

我建议你使用异步的 AsyncTask的。

短的例子:

ProgressDialog dialog;
class YourTask extends AsyncTask<Void, Void, Void> {
        protected void onPreExecute() {
                   dialog = ProgressDialog.show(...);
        }

        protected Void doInBackground(Void... unused) {
            try {
               // doSomethingHeavy();
                   // publishProgress(...);
            } catch(Exception e) {
                //...
            } 

            return null;
        }

        protected void onProgressUpdate(Void... unused) {
        }

        protected void onPostExecute(Void unused) {
            dialog.dismiss();

        }
    }