如何显示对话框以等待用户在 AsyncTask 中继续?
How to display a dialog to wait for user to continue in an AsyncTask?
我有一个 AsyncTask,它在 doInBackground() 部分做了很多事情,在这些事情之间,我需要等待用户实际做一些事情才能继续。我如何弹出一些对话框让用户在继续之前单击“确定”?
谢谢!
class LoadData extends AsyncTask<Object, Object, Object>
{
@Override
protected Object doInBackground(Object... p_params)
{
// Your background code
return null;
}
@Override
protected void onPreExecute()
{
// Display Progress dialog with cancelable false
super.onPreExecute();
}
@Override
protected void onPostExecute(Object p_result)
{
// Dismiss Progress dialog
super.onPostExecute(p_result);
}
}
in between that bunch of stuff, I need to wait for the user to
physically do something before I can continue.
你不应该在 doInBackground 方法中这样做,你需要在 onPostExecute() 中这样做;与用户的交互应该在 onPostExecute 中完成。
你可以在这里做什么?
将您的代码分成两部分,在 doInBackground 中执行必须完成的代码,直到用户在后台进行交互,让用户交互在 onPostExecute 中执行,之后剩余的代码您可以使用另一个 AsyncTask。
如果你想在 doInBackground 部分之间放置等待对话框,那么你可以尝试以下代码:
@Override
protected Void doInBackground(Void... params) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
final Dialog dialog = new Dialog(activity);
dialog.setTitle("Demo");
Button button = new Button(activity);
button.setText("Press For Process..");
dialog.setContentView(button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, "Perform Task",
Toast.LENGTH_LONG).show();
// You can perform task whatever want to do after
// on user press the button
dialog.dismiss();
}
});
dialog.show();
}
});
return null;
}
我有一个 AsyncTask,它在 doInBackground() 部分做了很多事情,在这些事情之间,我需要等待用户实际做一些事情才能继续。我如何弹出一些对话框让用户在继续之前单击“确定”?
谢谢!
class LoadData extends AsyncTask<Object, Object, Object>
{
@Override
protected Object doInBackground(Object... p_params)
{
// Your background code
return null;
}
@Override
protected void onPreExecute()
{
// Display Progress dialog with cancelable false
super.onPreExecute();
}
@Override
protected void onPostExecute(Object p_result)
{
// Dismiss Progress dialog
super.onPostExecute(p_result);
}
}
in between that bunch of stuff, I need to wait for the user to physically do something before I can continue.
你不应该在 doInBackground 方法中这样做,你需要在 onPostExecute() 中这样做;与用户的交互应该在 onPostExecute 中完成。
你可以在这里做什么?
将您的代码分成两部分,在 doInBackground 中执行必须完成的代码,直到用户在后台进行交互,让用户交互在 onPostExecute 中执行,之后剩余的代码您可以使用另一个 AsyncTask。
如果你想在 doInBackground 部分之间放置等待对话框,那么你可以尝试以下代码:
@Override
protected Void doInBackground(Void... params) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
final Dialog dialog = new Dialog(activity);
dialog.setTitle("Demo");
Button button = new Button(activity);
button.setText("Press For Process..");
dialog.setContentView(button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, "Perform Task",
Toast.LENGTH_LONG).show();
// You can perform task whatever want to do after
// on user press the button
dialog.dismiss();
}
});
dialog.show();
}
});
return null;
}