简体   繁体   中英

Android execute code while showing progress dialog

protected void showSpinner() {
    dialogSpin = ProgressDialog.show(activity, "", 
            "Loading. Please wait...", true);

    Thread t = new Thread(new Runnable(){

        @Override
        public void run() {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {


                    EverydayNotesAndroid3Activity.importAllCalendar();
                }
            });
        }

    });
    t.start();
}

I want to show the spinner while the thread is running. This way, I can show how my import of calendar is going to the user while I'm importing it.

Problem : my Spinner dialog will only show when the import is done. I don't know what I'm doing wrong, because according to my code it should show the dialog and then run the import (which takes 30 seconds), but once more, the dialog wait the end of the import to show up.

Thanks for paying attention, ask for more info.

考虑研究Asynctasks

You are importing calender in UI thread it self. Then your progress dialog wait's until import has been done. Then again it will show.

If you want to display progress dialog, then import calender in another thread not in UI thread.

I would use an AsyncTask to import the calendar. AsyncTask has three methods that can be overridden. You overwrite doInBackground and do all the stuff that is done in the background thread there. You can call publishProgress from your background task every time something should change in the UI. The AsyncTask will then call onProgressUpdate in the UI-Thread to allow you to show the progress to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM