升级数据库时显示进度对话框

发布于 2024-11-14 16:58:23 字数 371 浏览 5 评论 0原文

我的应用程序的下一个版本需要升级数据库,这需要相当多的时间。我想显示一个进度对话框来向用户更新进度。问题是,我不太清楚如何以及在哪里创建对话框。

我的基本设置是我有一个基本上是闪屏的活动。我想在这个屏幕上显示进度。我有一个单独的 DbAdapter.java 文件,其中 DatabaseHelper 类扩展了 SQLiteOpenHelper,我在其中重写了 onUpgrade (升级部分工作正常)。

我尝试了几个不同的地方来实现进度对话框,但我似乎没有找到正确的位置。我尝试将上下文从闪屏活动传递到 onUpgrade,但是当 onUpgrade 运行时,它似乎是从我的 ContentProvider 获取上下文。

有谁有一个关于如何在升级数据库时显示进度对话框的好例子?

The next version of my app needs to upgrade the database and this takes quite a bit of time. I'd like to show a progressDialog to update the user on the progress. Problem is, I can't quite figure out how and where to create the dialog.

My basic setup is that I have an activity which is essentially a splashscreen. It's on this screen I would like to show the progress. I have a separate DbAdapter.java file where a DatabaseHelper class extends SQLiteOpenHelper, where I override onUpgrade (the upgrade part is working fine).

I've tried a few different places to implement the progress dialog, but I don't seem to find the right spot. I tried passing context from my splashscreen activity to onUpgrade, but when onUpgrade runs it seems to be getting the context from my ContentProvider instead.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猛虎独行 2024-11-21 16:58:23

您需要实现一个AsyncTask。示例:

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

然后您只需调用

new YourAsyncTask().execute();

您可以在此处阅读有关 AsyncTask 的更多信息:http://developer.android.com/reference/android/os/AsyncTask.html

You need to implement an 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 have to call

new YourAsyncTask().execute();

You can read more about AsyncTask here: http://developer.android.com/reference/android/os/AsyncTask.html

云胡 2024-11-21 16:58:23
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();
    }
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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文