1

ボタンのあるアクティビティがあります。ユーザーがそのボタンをクリックすると、zip ファイルを作成する Util クラスが呼び出されます。if が zip をビルドした後、それをアクティビティに戻し、電子メール インテントを作成します。

以下のコードが部分的に機能しています。プログレスバーの部分は正常に機能します。しかし、電子メールは複数回生成されています。そのコードがループしているため、それを行っていることは理解していますが、修正方法がわかりません。

NEW ActivityDumpUtil の処理中に進行状況バーを使用したいだけです。zipファイルが呼び出されたときに、メールを送信する意図を開始したい..

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;

}

}

4

1 に答える 1

0

コールバックが UI スレッドで呼び出されると仮定するとonComplete(わからない場合は、Handler を使用してこれを強制できます)、ProgressBar の進行状況を更新するだけの AsyncTask を開始することをお勧めしますdumpActivityInformation()(私はそれが上で実行されると仮定します)。バックグラウンド スレッド、それ以外の場合はさらに簡単です)。次に、終了したらdumpActivityInformation()、フェッチが完了したことを示すフラグを立て、AsyncTask が終了したかどうかを確認する必要があります。2 つのオプションがあります。

  1. AsyncTask は終了しました - プログレス バーを非表示にするだけです
  2. AsyncTask は終了していません - 何もしません

次に、AsyncTask で、終了onPostExecuteしたというフラグを立てて 2 つのケースをチェックする必要があります

  1. dumpActivityInformation()完了しました - プログレスバーを非表示にするだけです
  2. dumpActivityInformation()- 何もしない

ブロッキング メソッドであれば、これはさらに簡単ですdumpActivityInformation()。このフラグは必要ありません。progressBar の後に AsyncTask で実行するだけです。

于 2013-07-28T18:23:31.640 に答える