Android加载对话冻结

时间:2010-05-19 13:39:10

标签: android

我在android中有这个代码。但是当螺纹乞求加载冻结时。如何防止这种冻结发生。以下是与此问题相关的代码:

private void reload(final String criteria) {
        try {
myProgressDialog = ProgressDialog.show(myfiles.this,
     "Please wait...", "Loading Your Photos... Please Wait",
     false);

   new Thread() {
    public void run() {
     try {

      Thread.sleep(2000);
     } catch (Exception e) {
     }

     runOnUiThread(new Runnable() {
      @Override
      public void run() {
       // Fetch the fact

       try {
        /* here my code  */

       } catch (Exception e) {
        myProgressDialog.dismiss();
        // TODO Auto-generated catch block
        e.printStackTrace();
       }

      }
     });
     // Dismiss the Dialog

     myProgressDialog.dismiss();
    }
   }.start();
} catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

如果有人可以检查

会很棒

1 个答案:

答案 0 :(得分:0)

通过使用runOnUiThread,您正在UI线程上执行密集加载任务,这将冻结UI。您希望在后台线程中运行任务,然后在任务完成时更新ui。

Thread task = new Thread(new Runnable() {
    void run() {
       //load some data.
       // notify UI thread using Handler
    }
});
task.start();