Displaying the progress dialog box when updating the database

The next version of my application should update the database, and it takes quite a while. I would like to show progressDialog to update the user about the progress. The problem is that I cannot understand how and where to create a dialogue.

My main setup is that I have an activity, which is essentially a splash screen. On this screen, I would like to show progress. I have a separate DbAdapter.java file, where the DatabaseHelper class extends SQLiteOpenHelper, where I redefine onUpgrade (part of the update works fine).

I tried several different places to implement the dialogue of progress, but it seems I did not find a suitable place. I tried passing the context from my splashscreen activity to onUpgrade, but when onUpgrade starts, it seems to get the context from my ContentProvider.

Does anyone have a good example of how to display a progress dialog when updating a database?

+3
source share
2 answers

You need to implement AsyncTask. Example:

class YourAsyncTask extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        //show your dialog here
        progressDialog = ProgressDialog.show(this, "title", "message", true, false)
    }

    @Override
    protected Void doInBackground(Void... params) {        
        //update your DB - it will run in a different thread
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //hide your dialog here
        progressDialog.dismiss();
    }
}

Then you just need to call

new YourAsyncTask().execute();

Read more about AsyncTask here: http://developer.android.com/reference/android/os/AsyncTask.html

+3
source
ProgressDialog myProgressDialog = null;
public void DownloadFiles() {
        myProgressDialog = ProgressDialog.show(this, "Please wait !",
                "Updating...", true);
        new Thread() {
            public void run() {
                try {
                       //Your upgrade method !
                       YourUpdateFunction();
                } catch (Exception e) {
                    Log.v(TAG, "Error");
                }
                myProgressDialog.dismiss();
            }
        }.start();
    }
0
source

All Articles