java - 尝试将 Android ProgressBar 添加到初始化实用程序类构建 zip 的 Activity

标签 java android android-progressbar

我有一个带有按钮的 Activity 。当用户单击该按钮时,它会调用构建 zip 文件的 Util 类。在构建 zip 后,它将其返回到 Activity 并创建电子邮件 Intent 。

我的下面的代码部分工作。进度条部分按其应有的方式工作。但电子邮件已生成多次。我知道它这样做是因为该代码是循环的,但我不知道如何修复。

我只想在处理新的 ActivityDumpUtil 时使用进度栏。当它回调 zip 文件时,我想发起发送电子邮件的 Intent ..

public class ActivityDump extends Activity {

private Button button;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarbHandler = new Handler();

private long fileSize = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    Util.onActivityCreateSetTheme(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dump);
    addButtonListener();

}

public void addButtonListener() {
    button = (Button) findViewById(R.id.activity_dump);
    button.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {

            // create and display a new ProgressBarDialog
            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(true);
            progressBar.setMessage("Gathering app info off your device");
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();

            progressBarStatus = 0;

            fileSize = 0;

            new Thread(new Runnable() {

                public void run() {
                    while (progressBarStatus < 100) {


                        final File outfile = new File(Environment.getExternalStorageDirectory(), "BigDX");
                        new ActivityDumpUtil(ActivityDump.this, outfile, new ActivityDumpUtil.ActivityDumpCallback() {
                            @Override
                                public void onComplete (File outfile) {

                                    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                                    intent.setType("text/plain");
                                    Uri uri = Uri.fromFile(outfile);
                                    intent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
                                    intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "bignadad@gmail.com" });
                                    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Icon Support");
                                    intent.putExtra(android.content.Intent.EXTRA_TEXT, "I am using " + Build.DEVICE + " with Android version " + Build.VERSION.RELEASE + " on " + getResources().getString(R.string.app_name));
                                    startActivity(Intent.createChooser(intent, "Send eMail.."));
                                    finish();
                                }
                            })
                                .dumpActivityInformation();

                        progressBarStatus = downloadFile();

                        // sleep 1 second (simulating a time consuming task...)
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        // Update the progress bar
                        progressBarbHandler.post(new Runnable() {
                            public void run() {
                                progressBar.setProgress(progressBarStatus);
                            }
                        });
                    }

                    // if the file is downloaded,
                    if (progressBarStatus >= 100) {

                        // sleep 2 seconds, so that you can see the 100%
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        // and then close the progressbar dialog
                        progressBar.dismiss();
                    }
                }
            }).start();

        }

    });

}

public int downloadFile() {

    while (fileSize <= 1000000) {

        fileSize++;

        if (fileSize == 100000) {
            return 10;

        } else if (fileSize == 200000) {
            return 20;

        } else if (fileSize == 300000) {
            return 30;

        } else if (fileSize == 400000) {
            return 40;

        } else if (fileSize == 500000) {

            return 50;
        } else if (fileSize == 700000) {

            return 70;
        } else if (fileSize == 800000) {

            return 80;
        }
        //...

    }

    return 100;

}

}

最佳答案

假设在 UI 线程中调用 onComplete 回调(如果您不确定,您可以使用 Handler 强制执行此操作),我建议您启动一个 AsyncTask,它只会更新您的 ProgressBar 的进度然后启动您的 dumpActivityInformation() (我假设它是在后台线程上执行的,否则会更容易)。接下来,当您的 dumpActivityInformation() 结束时,您必须升起一个表示提取已完成的标志,并检查 AsyncTask 是否结束,然后您将有 2 个选项

  1. AsyncTask 已结束 - 只是隐藏进度条
  2. AsyncTask 尚未结束 - 不执行任何操作

然后在 AsyncTask onPostExecute 中,您必须升起一个已结束的标志并检查 2 个案例

  1. dumpActivityInformation() 已完成 - 只需隐藏进度条
  2. dumpActivityInformation() - 不执行任何操作

如果dumpActivityInformation()是一个阻塞方法,那就更容易了,那么你不需要这个标志,只需在AsyncTask中的progressBar之后执行它即可。

关于java - 尝试将 Android ProgressBar 添加到初始化实用程序类构建 zip 的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17910966/

相关文章:

java - 设置TextView的宽度

Android,对话框中的SeekBar

java - 如何使用 HttpURLConnection 在 Java 中等待 Expect 100-Continue 响应

java - java中如何读取base64 xml格式数据以及如何获取值

android - 添加大量 Google 联系人

android - 如何将渐变定义为圆形progressBar

android - 创建一个从右到左的进度条

用于下载图像的 Android 加载指示器

java - 单行(DOTALL)正则表达式匹配比我想要的更多

java - 为什么使用 1<<4 而不是 16?