如何在doInBackground()时显示对话框。的AsyncTask

时间:2015-09-21 11:46:11

标签: android android-asynctask android-alertdialog progressdialog async-onprogressupdate

我正在尝试使用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) {

    }
}

我发布了这样的asyncktask:

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

我希望在消息中显示一个对话框&#34;从服务器&#34;获取名称的lis;而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只是一个管理对话框的类,getAlertDialog()mehots正在显示一个带有标题和消息的正常警告对话框。

但这不起作用,我无法想象如何显示一个自动打开的对话框,并在&#34; doInBackGround()&#34;已完成。

我该怎么做?

非常感谢!

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

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$3.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)

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

4 个答案:

答案 0 :(得分:1)

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();
}

答案 1 :(得分:0)

在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");
        }

答案 2 :(得分:0)

就这样做..

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();
}

答案 3 :(得分:0)

您无法向doInBackground显示对话框,因为doInBackground没有保留在主线程中(您无法获取上下文来显示它)。但方法onPreExecuteonPostExecute属于主线程,您可以在该方法中调用getAplicationContext()。我用这种方法来显示对话框。