【问题标题】:ProgressBar is not rotating - AndroidProgressBar 不旋转 - Android
【发布时间】:2017-07-14 07:55:56
【问题描述】:

我正在开发一个 Android 应用程序,这里我正在使用 ProgressBar ,为此我使用 runOnUiThread()doInBackground() 制作 VISIBLE/GONE AsyncTask 的方法,除了doInBackground() 没有找到其他地方适合做VISIBLE/GONE ProgressBar来满足UI需求。

在这个编码场景中,ProgressBar 可见但不旋转,当我通过 Activity 的onCreate() 方法使 ProgressBar 可见时,它正在旋转。

我不明白为什么会这样,我访问了 Stackoveflow 上的一些帖子,他们说 UI 线程无法旋转 ProgressBar,因为它可用于处理其他任务,因此我无法找到其他的修复让它工作的方法。

所以请帮我解决这个问题。 这是我用来制作 VISIBLE/GONE ProgressBar 的代码。

@Override
protected String doInBackground(String... f_url) {
    int count;
    try {

        zipfileName = f_url[1];
        fUrl3 = f_url[2];

        if (fUrl3.equals("DontDownloadZip")) {
            return "DownloadSuccess";
        } else {
            Log.d("Downloading ", zipfileName);

            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();

            // Hide ProgressBar after server connection .
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tipLayout.setVisibility(GONE);
                }
            });

            // Code to download a file .
            int lenghtOfFile = conection.getContentLength();

            InputStream input = new BufferedInputStream(url.openStream(),
                    8192);
            OutputStream output = new FileOutputStream(MyBooks.screen
                    .getFilesDir().getAbsolutePath()
                    + "/BookZips/"
                    + f_url[1]);
            byte data[] = new byte[1024];

            long total = 0;
            int counter = 0;

            while ((count = input.read(data)) != -1) {
                counter++;
                total += count;

                // Display ProgressBar when percentage is 99 .
                if ((int) ((total * 100) / lenghtOfFile) == 99) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (!identifier1.equals("BookZips9Download"))
                                tipLayout.setVisibility(VISIBLE);
                        }
                    });
                }
                publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
            android.os.FileUtils.setPermissions(MyBooks.screen
                    .getFilesDir().getAbsolutePath()
                    + "/BookZips/"
                    + f_url[1], FileUtils.S_IRWXU | FileUtils.S_IRWXG
                    | FileUtils.S_IROTH | FileUtils.S_IWOTH
                    | FileUtils.S_IXOTH, -1, -1);

        }

    } catch (Exception e) {
        e.printStackTrace();
        return "DownloadFailed";
    }

    return "DownloadSuccess";
}

【问题讨论】:

  • 可以在 AsyncTask 的 onPreExecute() 方法中看到。

标签: android android-layout android-progressbar


【解决方案1】:

尝试在清单文件中添加以下行:

<activity android:name=".MainActivity"
    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    >

在这里,将“.MainActivity”替换为您使用异步任务代码的那个活动。

【讨论】:

  • 它将保存您屏幕的当前信息。当前正在运行的进程将不会再次创建。试试看,或许能解决你的问题。
【解决方案2】:

onPreExecute 方法中使进度条可见,并在 onPostExecute 方法中隐藏进度条。这应该可以解决您的问题,因为您不需要使用 runOnUIThread 方法

【讨论】:

  • 你说的场景是真实场景,理想情况下应该是,但不符合UI要求,在此之前我尝试过你提到的相同,我需要解决编码场景中的问题我附上。
【解决方案3】:

不要将您的 UI 代码放在 doInBackground() 上(doInBackground() 在后台线程上执行)。这将崩溃,因为您试图在另一个线程中访问 UI 线程的对象(在本例中为 View 对象)。

如果你想更新你的进度条:

  1. 在doInBackground()中,触发该方法

publishProgress(整数...进度)

    @Override
    protected String doInBackground(String... f_url) {
            // Do your background tasks
            publishProgress(progress);
    }
  1. 重写 onProgressUpdate() 方法(您可以在这里安全地访问 View 对象)

    protected void onProgressUpdate(Integer... progress) {
        // Updates your progress bar here
    }
    

【讨论】:

  • 试过你的方式,但我得到了同样的行为。
猜你喜欢
  • 2012-10-31
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-17
相关资源
最近更新 更多