2012-12-08 150 views
0

我有一个生成PDF文件的类。 这个类由Activity扩展,意味着它有onCreate,onPause(等等)方法。从线程开始活动

当生成PDF文件需要一些时间才能生成,所以我需要显示一个进度条来通知用户该文件正在生成。

一切工作正常,期望当我点击一个生成按钮进度条冻结几秒钟(根据Logcat:进度栏冻结,而文件正在生成)。

所以我的问题是,我不知道如何使进度条在文件生成时不可停止。

下面是我的代码:

Log.i(TAG, "PDF generation: " + position); 

final ProgressDialog dialog = new ProgressDialog(this); 
dialog.setCancelable(true); 
dialog.setIndeterminate(true); 
dialog.setMessage("Generating..."); 
dialog.show(); 

new Thread(new Runnable() { 

    @Override 
    public void run() { 

     startActivity(new Intent(getApplicationContext(), 
      PdfGenerator.class)); 

     runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       dialog.hide(); 
      } 
     }); 

    } 

}).start(); 

回答

1

更改您的代码开始使用runOnUiThread从主题活动:

///... your code here.. 

    new Thread(new Runnable() { 

     @Override 
     public void run() { 

      Your_Current_Activity.this.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 

       dialog.cancel(); 

       // START ACTIVITY HERE 
       Your_Current_Activity.this.startActivity(new 
         Intent(Your_Current_Activity.this, 
             PdfGenerator.class)); 

       } 
      }); 

     } 

    }).start(); 
+0

现在看起来不错,但我做了一些改变。我已将dialog.hide()更改为dialog.cancel()并在开始新活动后移动。 还是谢谢! – burjulius

+0

@juliuks:但'runOnUiThread'是Activity类的方法,然后不使用Activity上下文,它不可能访问它。最受欢迎的firend :) –

+0

我觉得你不理解我做了什么改变。 顺便说一句,代码现在看起来像这样:http://pastebin.com/fxB1Zz9i – burjulius