如何在 doInBackground() 时显示对话框。异步任务

how to show dialog while doInBackground(). Asynctask

我正在尝试使用 android 中的 Asynctask 执行任务。

class GetItemNames extends AsyncTask<CosmeConnection, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(CosmeConnection... cosme) {
        ContentValues cv;
        cosme[0].orderListOfNames();
        while(!(cosme[0].getState() == CosmeStates.RECEIVED_LIST_OF_NAMES)){}
        DBHandler dbHandler = new DBHandler(getApplicationContext());
        SQLiteDatabase db = dbHandler.getReadableDatabase();
        if(db != null){
            db.delete(DBHandler.TABLE_SERVER_ITEM, null, null);

            String[] nombres = cosme[0].getExistingNames().getListOfNames().toString().replaceAll("[\[\]]", "").split(",");
            for (String str : nombres){
                cv = new ContentValues();
                cv.put(DBHandler.SERVER_ITEM_NAME, str);
                cv.put(DBHandler.SERVER_ITEM_NAME, "N");
                try{
                    db.insert(DBHandler.TABLE_SERVER_ITEM, null, cv);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        db.close();
        dbHandler.close();
        return null;
    }

    @Override
    protected void onProgressUpdate(String... progress) {

    }

    @Override
    protected void onPostExecute(String result) {

    }
}

我正在像这样启动这个异步任务:

new GetItemNames().execute(cosme, null, null);  

我想显示一个带有消息 "obtaining lis of names from server" 的对话框,而 doInBackGround() 是 运行。我试过这样做:

protected void onProgressUpdate(String... progress) {
        String title = getString(R.string.received_list_of_names_title);
        String msg = getString(R.string.received_list_of_names_msg);
        DialogHelper.getAlertDialog(getApplicationContext(), title, msg);
}

DialogHelper 只是一个管理对话框的 class,getAlertDialog() mehots 显示带有标题和消息的普通警报对话框。

但这也行不通,我想不出如何显示一个自动打开并在 "doInBackGround()" 完成后关闭的对话框。

我该怎么做?

非常感谢!

编辑:尝试建议后我有这个例外:

09-21 14:07:21.966: E/AndroidRuntime(22016): FATAL EXCEPTION: main
09-21 14:07:21.966: E/AndroidRuntime(22016): Process: tfc.unizar.blasmobile, PID: 22016
09-21 14:07:21.966: E/AndroidRuntime(22016): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:643)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.app.Dialog.show(Dialog.java:286)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at tfc.unizar.blasmobile.BagContent$GetItemNames.onPreExecute(BagContent.java:263)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.AsyncTask.execute(AsyncTask.java:535)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at tfc.unizar.blasmobile.BagContent.onOptionsItemSelected(BagContent.java:133)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.app.Activity.onMenuItemSelected(Activity.java:2633)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1040)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:184)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.widget.AbsListView.performItemClick(AbsListView.java:1152)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.widget.AbsListView.run(AbsListView.java:3865)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.Handler.handleCallback(Handler.java:808)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.Handler.dispatchMessage(Handler.java:103)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.os.Looper.loop(Looper.java:193)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at android.app.ActivityThread.main(ActivityThread.java:5296)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at java.lang.reflect.Method.invokeNative(Native Method)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at java.lang.reflect.Method.invoke(Method.java:515)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
09-21 14:07:21.966: E/AndroidRuntime(22016):    at dalvik.system.NativeStart.main(Native Method)

此后应用程序被强制关闭。有什么建议吗?

private ProgressDialog mProgress;
@Override
protected void onPreExecute(){ 
   super.onPreExecute();
   mProgress = new ProgressDialog(yourContext);
   mProgress.setMessage("Obtaining list of names from server...");
   mProgress.show();    
}

@Override
protected void onPostExecute(String result){
   super.onPostExecute(result);
   mProgress.dismiss();
}

将此代码添加到您的 Asynctask 中。

初始化 progressDialoge

ProgressDialog progressDialog;

并添加此代码是添加在 onPreExecute

 progressDialog = ProgressDialog.show(Form.this, "", "Loading...", true,false);

最后 onPostExecute 添加此代码

   if(progressDialog1!=null && progressDialog1.isShowing()){
            progressDialog1.dismiss();
            Log.e("DISMISS", "DISMISS");
        }

就这样做..

private ProgressDialog mDialog;

@Override
protected void onPreExecute(){    
   mDialog = new ProgressDialog(context);
   mDialog.setMessage("loading...");
   mDialog.setIndeterminate(true);
   mDialog.setCancelable(false);
   mDialog.show();    
}

@Override
protected void onPostExecute(String result){
   mDialog.dismiss();
}

您无法在 doInBackground 中显示对话框,因为 doInBackground 不保留在主线程中(您无法获取显示它的上下文)。但是方法 onPreExecuteonPostExecute 属于主线程,您可以在该方法中调用 getAplicationContext()。我建议在该方法中显示对话框。