本文介绍了如何显示进度对话框(在单独的线程?),而在处理主线程一些code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我需要做到以下几点:当应用程序启动时运行的活动(splashActivity),它试图创建一个在创建时检查数据库的存在,并创建一个如果没有一个DBHelper(SQLiteDatabase)实例结果
在这个数据库的创建过程中,我想显示ProgressDialog和 prevent主线程来处理,而DB是没有准备好

在splashActivity:

the splashActivity:

public class SplashActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        showSplash();
        DBHelper dbHelper = new DBHelper(this);
        dbHelper.close();
  }
}

在DBHelper类:

the DBHelper class:

public class DBHelper extends SQLiteOpenHelper { 
...
    private ProgressDialog mProgressDialog;
...
  public DBHelper(Context context) { 
    super(context, DB_NAME, null, 1);
    appContext=context;
    dbReadable = getReadableDatabase();
    if (createDB)
        initDB();
  } 
  @Override 
  public void onCreate(SQLiteDatabase db) {
      createDB=true;
  } 
  public void initDB() {
      ...
      if (createDB) 
          restoreDB();
      ...
  }

  private void restoreDB(){
   // place for db creation/restoring method call (restoreDBFromFile() etc)
   // and displaying the progress
  }

我试图使作为AsyncTask的DBHelper的内部类:

I tried to make AsyncTask as DBHelper's inner class:

  private class RestoreDBTask extends AsyncTask <Void, Void, String>
    {
        private ProgressDialog dialog;

        @Override
        protected void onPreExecute()
        {
            this.dialog = ProgressDialog.show(DBHelper.this.appContext, DBHelper.this.appContext.getResources().getString(R.string.progress_wait), 
                    DBHelper.this.appContext.getResources().getString(R.string.progress_db_installing), true);
        }

        @Override
        protected String doInBackground(Void... params)
        {
            restoreDBFromFile();
            return "";
        }

        @Override
        protected void onPostExecute(String result)
        {
            this.dialog.dismiss();
        }
    }

所以restoreDB()是:

so the restoreDB() was:

restoreDB() {
      RestoreDBTask task = new RestoreDBTask();
      task.execute();
}

启动任务显示PD但同时主线程继续,因此它崩溃的原因DB还没有准备好。这意味着,虽然DB是没有准备好,所以我想睡觉/等待/没有成功加入主线程应停止使用。好。这不是很好的反正。结果
下一次,我提出呼吁从restoreDB()restoreDBFromFile(),因此只被用于PD显示异步任务:

it starts task showing PD but meanwhile the main thread continues and so it crashes cause DB wasn't ready. That means main thread should be suspended while DB is not ready so i tried to sleep/wait/join with no success. Ok. it's not good anyway.
Next time i made call to restoreDBFromFile() from restoreDB() so that async task was used only for PD displaying:

restoreDB() {
  RestoreDBTask task = new RestoreDBTask();
  task.execute();
  restoreDBFromFile();    
}

它不会显示任何PD。所以,我试图创建PD就在restoreDB():

It doesn't display any PD. So I tried to create PD right in the restoreDB():

restoreDB() {
   mProgressDialog = ProgressDialog.show(appContext, 
          appContext.getResources().getString(R.string.progress_wait), 
          appContext.getResources().getString(R.string.progress_db_installing), 
          true);
  restoreDBFromFile();    
}

但没有任何PD!下一次,我尝试了以下内容:

but no PD either! Next time i tried the following:

    restoreDB() {
      Thread thread =  new Thread(null, new Runnable(){
              @Override
              public void run() {
                  mProgressDialog = ProgressDialog.show(appContext, 
                          appContext.getResources().getString(R.string.progress_wait), 
                          appContext.getResources().getString(R.string.progress_db_installing), 
                          true);
              }
          }, "ProgressDialog");
      thread.start();
      restoreDBFromFile();
}

再一次得到任何PD,并得到即时崩溃:

once again got no PD and got immediate crash:

04-12 08:40:38.894:
  ERROR / AndroidRuntime(2448):错误:
  线程附加失败04-12
  08:40:38.913:DEBUG / dalvikvm(2448):
  LinearAlloc为0x0使用639500的5242880
  (12%)04-12 08:40:39.094:
  DEBUG / DDM堆(2454):GOT功能列表
  要求8月四日至12日:40:39.492:
  WARN / dalvikvm(2454):主题ID = 15:
  螺纹未捕获的异常退出
  (组= 0x4001b188)八月四日至12日:40:39.492:
  ERROR / AndroidRuntime(2454):未捕获
  处理:螺纹ProgressDialog退出
  由于未捕获的异常04-12
  08:40:39.504:
  ERROR / AndroidRuntime(2454):
  了java.lang.RuntimeException:不能
  创建里面有螺纹的处理程序
  不叫活套。prepare()04-12
  08:40:39.504:
  ERROR / AndroidRuntime(2454):在
  android.os.Handler(Handler.java:121)
  04-12 08:40:39.504:
  ERROR / AndroidRuntime(2454):在
  android.app.Dialog(Dialog.java:105)
  04-12 08:40:39.504:
  ERROR / AndroidRuntime(2454):在
  android.app.AlertDialog(AlertDialog.java:63)
  04-12 08:40:39.504:
  ERROR / AndroidRuntime(2454):在
  android.app.ProgressDialog(ProgressDialog.java:80)
  04-12 08:40:39.504:
  ERROR / AndroidRuntime(2454):在
  android.app.ProgressDialog(ProgressDialog.java:76)
  04-12 08:40:39.504:
  ERROR / AndroidRuntime(2454):在
  android.app.ProgressDialog.show(ProgressDialog.java:101)
  04-12 08:40:39.504:
  ERROR / AndroidRuntime(2454):在
  android.app.ProgressDialog.show(ProgressDialog.java:90)
  04-12 08:40:39.504:
  ERROR / AndroidRuntime(2454):在
  com.taxiorder.DBHelper $ 1.run(DBHelper.java:157)
  04-12 08:40:39.504:
  ERROR / AndroidRuntime(2454):在
  java.lang.Thread.run(Thread.java:1096)

。结果
尝试了一些事情没有结果......所以,我只是无法弄清楚什么是错的。结果

.
Tried a few more things with no result... So I just can't figure out what's wrong.

再一次 - 我只是需要而restoreDB()执行,显示PD。因为我需要它完成之前的应用程序可以继续,我不能在单独的线程中运行restoreDB()。我所看到的PD应单独的线程只能运行,但我不能管理它。搜索结果
现在,我有2个其他问题:

Once again - I just need to show PD while restoreDB() executing. I can't run restoreDB() in separate thread because I need it finished before app can proceed. As I can see PD should be run in separate thread only but i can't manage it.

Now I have 2 additional questions:

1)如果PD应单独的线程创建它可以创建并显示,如果它的线程没有做任何事情。我的意思是这样的:

1) If a PD should be created in separate thread could it be created and showed if it's thread doesn't do anything at all. I mean something like this:

    new Thread(){
          public void run(){
              mProgressDialog = ProgressDialog.show(appContext, 
                      appContext.getResources().getString(R.string.progress_wait), 
                      appContext.getResources().getString(R.string.progress_db_installing), 
                      true);

      }.start();

据我了解线程执行的运行(后死亡)原因无关,因此PD不显示。结果
2)PD本身并不会暂停任何线程同时显示,不是吗?

As I understand the thread dies immediately after executing run() cause there is nothing to do and so PD doesn't show.
2) PD by itself doesn't suspend any thread while displaying, does it?

推荐答案

试图声明的AsyncTask 作为活动的一个内部类,实例化并执行它。我认为这个问题是当你创造它给 ProgressDialog 的背景下,这种做法的逻辑是太复杂了。在活动的声明的AsyncTask 是比较普遍的做法。

Try to declare AsyncTask as an inner class for the activity, instantiate and execute it there. I think the problem is in context given to ProgressDialog when you are creating it, your logic for doing this is too complicated. Declaring AsyncTask in activity is more common practice.

然后,AsyncTask的必须做所有你需要使用DB doInBackground

Then, AsyncTask must do all you need with DB in doInBackground method

public class SplashActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        new RestoreDBTask().execute();
    }

}

private class RestoreDBTask extends AsyncTask <Void, Void, String>
{
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute()
    {
        dialog = ProgressDialog.show(
            SplashActivity.this,
            getString(R.string.progress_wait),
            getString(R.string.progress_db_installing), 
            true);
    }

    @Override
    protected String doInBackground(Void... params)
    {
        // do all the things with DB
        DBHelper dbHelper = new DBHelper(SplashActivity.this);
        dbHelper.close();
        return "";
    }

    @Override
    protected void onPostExecute(String result)
    {
        dialog.dismiss();
    }
}

这篇关于如何显示进度对话框(在单独的线程?),而在处理主线程一些code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:20