我是 android 的菜鸟,我试图在启动画面之后的 OnCreate 方法期间显示一个进度对话框。目前,在我的启动画面之后,屏幕黑屏大约 5 秒钟,然后出现布局。我正在使用 Asynctask 进行后台操作,所以我不明白为什么在应用程序初始化时没有出现进度对话框而不是 5 秒的黑色。这仅适用于 Android 3.0 及更低版本。所有后续 sdk 版本都会正确显示进度对话框。任何帮助是极大的赞赏。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//wakelock
PowerManager pM = (PowerManager)getSystemService(Context.POWER_SERVICE);
wL = pM.newWakeLock(PowerManager.FULL_WAKE_LOCK, "Screen Dim");
wL.acquire();
LoadingData dataLoaded = new LoadingData();
dataLoaded.execute("");
---code for UI Thread---
}//--End onCreate
private class LoadingData extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
//TWITTER API CODE HERE
} catch (Exception e) {
Log.e("log_tag", "Error parsing data "+ e.toString());
}
publishProgress(33);
//DOWNLOADING IMAGES CODE HERE
try{
}catch(Exception e){
}
publishProgress(66);
//FOREX RATES CODE HERE
try {
}catch(JSONException e) {
Log.e("log_tag", "Error acquiring forex data "+e.toString());
}
publishProgress(100);
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog (GoldandSilverCoinCalculatorActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("Loading... please wait");
dialog.setIndeterminate(false);
dialog.setMax(100);
dialog.setProgress(0);
dialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
dialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.dismiss();
}
}
----EDIT---- 我的 OnResume 和 OnPause 方法
@Override
protected void onPause() {
// TODO Auto-generated method stub
compass.disableCompass();
super.onPause();
wL.release();
lm.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
compass.enableCompass();
super.onResume();
wL.acquire();
lm.requestLocationUpdates(towers, 500, 1, this);
}