进度对话框开放活动 [英] Progress Dialog on open activity
问题描述
嘿,伙计们, 我已经与进度对话框的问题上打开一个活动(称为例如,在活动2)。 活动2有很多code本OnCreate事件来执行。
hey guys, i've a problem with progress dialog on opening an activity (called activity 2 in example). The activity 2 has a lot of code to execute in this OnCreate event.
final ProgressDialog myProgressDialog = ProgressDialog.show(MyApp.this,getString(R.string.lstAppWait), getString(R.string.lstAppLoading), true);
new Thread() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
showApps();
}
});
myProgressDialog.dismiss();
}
}.start();
在showApps功能推出的活动2。
The showApps function launch activity 2.
如果我执行这个code。关于我的按钮点击事件上的活动1,我看到了进步,但她不动,并afeter我有在2〜3秒的黑屏时间为Android的展现活动。
if i execute this code on my button click event on activity 1, i see the progress, but she doesn't move and afeter i have a black screen during 2 or 3 seconds the time for android to show the activity.
如果我在活性2的OnCreate执行此code,如果我在code上的OnCreate,更换showApps活动1冻结2秒,我看不到进度对话框,并再次冻结2秒在活动2之前看到的结果。
If i execute this code in the OnCreate of Activity2 and if i replace the showApps by the code on OnCreate, Activity1 freeze 2 seconds, i don't see the progress dialog, and freeze again 2 seconds on activity 2 before seeing the result.
这是想法?
推荐答案
我有同样的问题,使用的 AsyncTask的是为我工作。
I had the same issue and using an AsyncTask is working for me.
有在AsyncTask的覆盖三个重要的方法。
There are 3 important methods to override in AsyncTask.
- doInBackground :这就是你的背景肉 会发生处理。
- 在preExecute :在这里展示您的ProgressDialog(的ShowDialog)
- onPostExecute :这里隐藏ProgressDialog(removeDialog或dismissDialog )
- doInBackground : this is where the meat of your background processing will occur.
- onPreExecute : show your ProgressDialog here ( showDialog )
- onPostExecute : hide your ProgressDialog here ( removeDialog or dismissDialog )
如果你让你的AsyncTask子类作为一个内部类的活动,那么你可以从你的AsyncActivity中调用框架方法的ShowDialog,dismissDialog和removeDialog。
If you make your AsyncTask subclass as an inner class of your activity, then you can call the framework methods showDialog, dismissDialog, and removeDialog from within your AsyncActivity.
下面是AsyncTask的样本实现:
Here's a sample implementation of AsyncTask:
class LoginProgressTask extends AsyncTask<String, Integer, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
try {
Thread.sleep(4000); // Do your real work here
} catch (InterruptedException e) {
e.printStackTrace();
}
return Boolean.TRUE; // Return your real result here
}
@Override
protected void onPreExecute() {
showDialog(AUTHORIZING_DIALOG);
}
@Override
protected void onPostExecute(Boolean result) {
// result is the value returned from doInBackground
removeDialog(AUTHORIZING_DIALOG);
Intent i = new Intent(HelloAndroid.this, LandingActivity.class);
startActivity(i);
}
}
这篇关于进度对话框开放活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!