简体   繁体   English

如何制作进度对话框以进行数据库复制?

[英]How to make progress dialog for database copying?

I have a game with a sqlite database, 10MB large. 我有一个带有sqlite数据库的游戏,大小为10MB。 So, after installing the game, and upon first game start the database is being copied to internal memory and that takes a while, maybe 30 seconds, with black screen. 因此,在安装游戏之后,并且在第一次游戏开始时,数据库将被复制到内部存储器,这需要一段时间,可能需要30秒钟,并且出现黑屏。 This is pretty bad cause a user can think that the game froze. 这非常糟糕,因为用户可以认为游戏冻结了。 I was thinking, is there a way to show something like on the image below, saying "Copying DB in progress" or something, once the main menu opens? 我当时在想,一旦打开主菜单,是否有办法在下图显示类似“正在复制数据库”之类的内容? And how can I do that? 我该怎么办?

I'm copying my db from Assets folder using this code: 我正在使用以下代码从Assets文件夹复制数据库:

TestAdapter mDbHelper = new TestAdapter(this);
        DataBaseHelper myDbHelper = new DataBaseHelper(this);

        if(!myDbHelper.checkDataBase()){
        mDbHelper.createDatabase();
        }

在此处输入图片说明

Progress bar can be used with asyncTask. 进度栏可以与asyncTask一起使用。 As you know the UI thread should be spared from heavy operation that can affect the responsiveness of the UI. 如您所知, UI线程应该避免可能会影响UI响应能力的繁重操作。 Now this is when you use worker threads. 现在是使用工作线程的时候。

It can be very easy to use progressBar with async task... But since u didnt want asyncTask here is one example. 在异步任务中使用progressBar可能非常容易。但是由于您不希望asyncTask,因此这里是一个示例。

  btnStartProgress = (Button) findViewById(R.id.btnStartProgress);//the button that starts your function
    btnStartProgress.setOnClickListener(
             new OnClickListener() {

       @Override
       public void onClick(View v) {

        // prepare for a progress bar dialog
        progressBar = new ProgressDialog(v.getContext());
        progressBar.setCancelable(true);
        progressBar.setMessage("File downloading ...");
        progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressBar.setProgress(0);
        progressBar.setMax(100);
        progressBar.show();

        //reset progress bar status
        progressBarStatus = 0;

        //reset filesize
        fileSize = 0;

        new Thread(new Runnable() {
          public void run() {
            while (progressBarStatus < 100) {

              // process some tasks
              progressBarStatus = doSomeTasks();

              // your computer is too fast, sleep 1 second
              try {
                Thread.sleep(1000);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }

              // Update the progress bar
              progressBarHandler.post(new Runnable() {
                public void run() {
                  progressBar.setProgress(progressBarStatus);
                }
              });
            }

            // ok, file is downloaded,
            if (progressBarStatus >= 100) {

                // sleep 2 seconds, so that you can see the 100%
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // close the progress bar dialog
                progressBar.dismiss();
            }
          }
           }).start();

           }

            });

    }

// file download simulator... a really simple
public int doSomeTasks() {

    while (fileSize <= 1000000) {

        fileSize++;

        if (fileSize == 100000) {
            return 10;
        } else if (fileSize == 200000) {
            return 20;
        } else if (fileSize == 300000) {
            return 30;
        }
        // ...add your own

    }

    return 100;

}

Click here for the sourcecode. 单击此处获取源代码。

EDIT: 编辑:

Check this tutorial . 检查本教程。 Click here. 点击这里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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