我有一个活动从数据库下载数据。虽然活动正在完成这项工作,但我想用ProgressDialog来显示进度。我使用ProgressDialog.STYLE_HORIZONTAL
,因为我想显示实际值。我用一个Handler
要启动的显示ProgressDialog活动:如何在Android中使用intent显示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();
}
}
当主要活动从数据库下载的新表的Handler
开始一个新的ProgressDialogActivity并出现一个新的活动。我想避免这一点。我的目标是只显示一个活动,其中显示具有正确值的ProgressDialog。我不能在主要活动中创建ProgressDialog,我必须找到另一种方式,这是某种功课,但我需要一些帮助)。 谢谢!
我不得不使用意图... – Alex 2012-07-12 10:31:22
瓦你是否必须使用意图?你真的应该使用AsyncTask和Progress对话框来做这样的事情。现在你每次想要更新一个简单的对话框时都试图开始一个新的活动。你还想在活动的onPause()方法中关闭对话框,否则你可能会泄漏一个窗口。 – Joel 2012-07-12 19:50:35