[英]Start an activity from the thread
I have a class which generates a pdf file. 我有一个生成pdf文件的类。 This class is extended by Activity that means is have onCreate, onPause (and so on) methods.
该类由Activity扩展,意味着具有onCreate,onPause(等等)方法。
While a pdf file is generating it takes some time to be generated so I need to show a progress bar to notify the user that the file is generating at the moment. 在生成pdf文件时,它需要一些时间才能生成,因此我需要显示一个进度条以通知用户该文件正在生成。
Everything works fine expect that when I click on a generate button the progress bar freezes for a few seconds (according to Logcat: progress bar freezing while file is generating). 一切正常,希望当我单击“生成”按钮时进度条冻结几秒钟(根据Logcat:文件生成时进度条冻结)。
So my problem is that, that I don't know how to make the progress bar unstoppable while the file is generating. 所以我的问题是,我不知道如何在生成文件时使进度条不可阻挡。
Below is my code: 下面是我的代码:
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();
Change your code as for starting an Activity from Thread using runOnUiThread: 使用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.