[英]Start an activity from the thread
我有一个生成pdf文件的类。 该类由Activity扩展,意味着具有onCreate,onPause(等等)方法。
在生成pdf文件时,它需要一些时间才能生成,因此我需要显示一个进度条以通知用户该文件正在生成。
一切正常,希望当我单击“生成”按钮时进度条冻结几秒钟(根据Logcat:文件生成时进度条冻结)。
所以我的问题是,我不知道如何在生成文件时使进度条不可阻挡。
下面是我的代码:
Log.i(TAG, "PDF generation: " + position);
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setIndeterminate(true);
dialog.setMessage("Generating...");
dialog.show();
new Thread(new Runnable() {
@Override
public void run() {
startActivity(new Intent(getApplicationContext(),
PdfGenerator.class));
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.hide();
}
});
}
}).start();
使用runOnUiThread将代码更改为从线程启动活动:
///... your code here..
new Thread(new Runnable() {
@Override
public void run() {
Your_Current_Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.cancel();
// START ACTIVITY HERE
Your_Current_Activity.this.startActivity(new
Intent(Your_Current_Activity.this,
PdfGenerator.class));
}
});
}
}).start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.