androidandroid-asynctaskandroid-alertdialogprogressdialogasync-onprogressupdate

how to show dialog while doInBackground(). Asynctask


I'm triying to do a task with an Asynctask in android.

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) {

    }
}

I'm launching this asyncktask like this:

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

I want to show a dialog wiht the message "obtaining lis of names from server" while doInBackGround() is running. I tried to do in this way:

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 is just a class that manage dialogs, getAlertDialog() mehots is showing an normal alert dialog with a title and a message.

But this is nor working, i can't figure how to show a dialog that opens automaticlly and close when "doInBackGround()" has finished.

How can i do this?

Thanks a lot!

EDIT: after try the suggestion i have this exception:

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)

The application is closed by force after this. Any suggestion?


Solution

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