在Android中进行大量计算时如何显示加载屏幕? [英] How to display a loading screen while doing heavy computing in Android?
问题描述
我正在研究一个程序,该程序会在用户电话中搜索某个日期,该日期大约需要2-3秒.在计算时,我想显示一个加载屏幕,以便用户知道确实正在发生某些事情.但是,当我尝试在计算之前显示加载屏幕时,屏幕上什么都没有显示.
I am working on a program that searches the users phone for some date, which takes about 2-3 seconds. While it's computing I want to display a loading screen, so the user knows something indeed is happening. However, when I try to display a loading screen before the computations, nothing is displayed on the screen.
这就是我所拥有的:
ProgressDialog loading= new ProgressDialog(this);
loading.setTitle("Loading");
loading.setMessage("Please wait...");
loading.show();
//search stuff
loading.dismiss();
除此之外,我还尝试将ProgressDialog放入如下所示的线程中,
In addition to this, I have tried putting the ProgressDialog in a thread like the following,
new Thread(new Runnable(){
public void run(){
ProgressDialog loading= new ProgressDialog(this);//error here for "this"
loading.setTitle("Loading");
loading.setMessage("Please wait...");
loading.show();
}
});
//search stuff
但是由于"this"关键字而失败,我相信是因为它指的是Activity而不是常规类,但是我可能错了...
but it fails due to the "this" keyword, I believe because its referring to an Activity and not a regular class, but I could be wrong...
如何使ProgressDialog正确显示?
How can I get the ProgressDialog to display properly?
推荐答案
尝试以这种方式处理
mProgressDialog = ProgressDialog.show(this, "Please wait","Long operation starts...", true);
new Thread() {
@Override
public void run() {
//Do long operation stuff here search stuff
try {
// code runs in a thread
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressDialog.dismiss();
}
});
} catch (final Exception ex) {
}
}
}.start();
这篇关于在Android中进行大量计算时如何显示加载屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!