我试图通过创建一系列可在Android中完成的对话框菜单来模拟Android中的USSD交互。现在,(感谢帮助我on here的用户!)我已经拥有它,以便在对话框之间有一个进度对话框,说明" USSD代码运行..."在每个对话框之间运行2秒。但是,现在我的问题是我无法在运行后取消进度对话框。现在,当我完成对话时,有几个进度对话框仍然打开并运行。我知道我需要在某处的代码中包含progress.cancel()命令,但我尝试的每个地方似乎都阻止了进度对话框的运行。
Runnable progressRunnable;
public void FirstScreen() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Inflate and set the layout for the dialog; pass null as the parent view because its going in the dialog layout
final View dialogView = inflater.inflate(R.layout.number_response_dialog, null);
builder.setView(dialogView)
.setMessage(R.string.MainMenuText)
// Add action buttons
.setPositiveButton(R.string.send, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Send number to next dialog
String choice = getMenuChoice(dialogView);
if (choice.equals("5")) {
//Go to FirstTimeUse menu
progressRunnable = new Runnable() {
@Override
public void run() {
FirstTimeUse();
}
};
USSDprogressDialog(progressRunnable);
} else if (choice.equals("6")) {
//Something else
} else {
//Do nothing
dialog.dismiss();
}
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// End session
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
实际进度对话框辅助方法:
public void USSDprogressDialog(Runnable runnable) {
final ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("USSD code running...");
progress.show();
Handler handler = new Handler();
handler.postDelayed(runnable, 2000);
}
我尝试在USSDProgressDialog方法结束时以及在运行USSDProgressDialog时在所有对话框中包含。还有什么地方可以包含它,以便在计时器用完后每次取消?谢谢!
答案 0 :(得分:0)
final ProgressDialog progress = new ProgressDialog(this);
您需要将进度对话框的声明放在USSDprogressDialog
函数之外,以便可以从活动的所有其他函数访问它。全局初始化进度对话。
现在,从正面按钮onClickListener
尝试使用此示例代码。
if (choice.equals("5")) {
//Go to FirstTimeUse menu
progressRunnable = new Runnable() {
@Override
public void run() {
FirstTimeUse();
// Now cancel the progress dialog here.
if(progress != null) progress.cancel();
}
};
USSDprogressDialog(progressRunnable);
}