ボタンのあるアクティビティがあります。ユーザーがそのボタンをクリックすると、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;
}
}