我有一个从数据库下载数据的活动。当 Activity 进行这项工作时,我想用ProgressDialog显示进度。我使用它ProgressDialog.STYLE_HORIZONTAL
是因为我想显示实际值。我使用 aHandler
启动显示ProgressDialog的活动:
Intent intent = new Intent(this, ProgressDialogActivity.class);
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == SET_PROGRESS){
intent.putExtra("action", "show");
intent.putExtra("progress", msg.arg1);
intent.putExtra("max", msg.arg2);
intent.putExtra("message", syncMessage);
intent.putExtra("title", R.string.please_wait);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else if(msg.what == SHOW_PROGRESS){
intent.putExtra("action", "show");
intent.putExtra("title", syncMessage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else if(msg.what == HIDE_PROGRESS){
intent.putExtra("action", "hide");
intent.putExtra("message", "");
intent.putExtra("title", "");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
};
这是ProgressDialogActivity:
public class ScreenProgressDialog extends Activity {
ProgressDialog pd;
Bundle extras;
String action;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
extras = getIntent().getExtras();
pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(false);
pd.setProgress(extras.getInt("progress"));
pd.setMax(extras.getInt("max"));
pd.setMessage(extras.getCharSequence("message"));
pd.setTitle(extras.getString("title"));
action = extras.getString("action");
if (action.equals("show")){
pd.show();
}
else{
pd.dismiss();
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
当 Main Activity 从数据库下载一个新表时,会Handler
启动一个新的ProgressDialogActivity并出现一个新的 Activity。我想避免这种情况。我的目标是只显示一个显示具有正确值的ProgressDialog的活动。(我无法在主活动中创建ProgressDialog,我必须找到另一种方法。这是某种家庭作业,但我需要一些帮助)。谢谢!