我有一个AsyncTask在doInBackground()部分做了很多东西,在那堆东西之间,我需要等待用户在继续之前做一些事情。如何在继续之前弹出一些对话框以供用户单击“确定”?
谢谢!
答案 0 :(得分:1)
在那堆东西之间,我需要等待用户 在我能继续之前做一些事情。
你不应该在doInBackground方法中这样做,你需要在onPostExecute()中做到这一点。与用户的交互应该在onPostExecute中完成。
你能在这做什么?
将代码分为两部分,执行必须完成的代码,直到用户在doInBackground的后台进行交互,让用户Interact在onPostExecute中执行此操作,之后剩下的其余代码可以使用另一个AsyncTask。
答案 1 :(得分:0)
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);
}
}
答案 2 :(得分:0)
如果要在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;
}