我有一个有按钮的活动。当用户单击该按钮时,它会调用一个构建 zip 文件的 Util 类。在构建 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;
}
}