2011-08-17 53 views
0

我有函数与webservice进行通信并将数据插入数据库出现 在此功能执行期间....我无法预测执行该函数需要多少时间。需要实现进度条和线程

问题是,我需要显示一个progess栏,它在函数执行停止时结束。它需要显示进度。

还是有,展示了commuction任何正在进行的其他选项....

我的功能是作为下面给出....

public Boolean startdownload() {  


     downloadhelper = new download_helper(this); 

     downloadhelper.DownloadProblemAndReasonCode();  

     pkManifest=downloadhelper.DownloadNewManifest();   

     downloadhelper.DownloadNewMessages(); 


     return True; 
    } 

此外,我对的onclick执行线程开始按钮的事件.......

start_button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
           Status.setText("Download in Progress...."); 
       new Thread(new Runnable() { 
        public void run() { 
         try { 

           startdownload(); 
           onStop(); 
           isRunning=false; 
         } catch (Throwable t) { 
          t.toString(); 
         } 

        } 
        }).start();    

       if (isRunning=false) 
       { 
        Status.setText("Download Completed"); 
       } 

      } 
     }); 

回答

1

更简单的方法做,这是使用的Android和很容易Asyntask概念实现 参考this

0

除了Asynctask,您可以使用Handler及其handleMessage()。你的工作线程内,发送消息到Handler来显示进度,并通知它当工作线程结束

1
//Take one progress bar Display here 

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

          startdownload(); 
          onStop(); 
          isRunning=false; 
        } catch (Throwable t) { 
         t.toString(); 
        } 

       } 
       }).start();    

      if (isRunning=false) 
      { 
       Status.setText("Download Completed"); 
      } 
    handler.sendEmptyMessagetrue(0); 

     } 
//Now take handler class here to dismiss() progress bar when worked finish in thread 
private Handler handler()=new Handler(){ 
override onhandlerMsge();dissmiss dialog in this method 
} 
+0

是否能解决你的问题,那么请接受它作为一个答案 – Sameer

1

你可以把一个ProgressBar某处你的布局,当你准备做你打电话mProgressBar.setVisibility(View.VISIBLE);网络通信一旦完成呼叫mProgressBar.setVisibility(View.GONE);

另一种方法是使用Android系统栏中的进度条。在致电setContentView()之前,在您的活动电话requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)onCreate()中这样做,然后您可以在您的活动的任何地方拨打setProgressBarIndeterminateVisibility(boolean)

+0

使用系统栏是减少杂波,并防止恼人的闪烁,如果进展发生得很快,可能发生的好办法。 –

0
showLoading();// Calling showdialog before downloading.. 
     Thread t = new Thread(){ 
      public void run() { 

       // Code for download.... 

       // After download UI thread will call 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
        dialog.cancel(); 
        alertbox("Info","Download Completed"); 
        } 
       }); 
      }}; 
      t.start(); 

public ProgressDialog dialog; 
    public void showLoading() { 


      // prepare the dialog box 
      //ProgressDialog 
      dialog = new ProgressDialog(this); 

      // make the progress bar cancelable 
      dialog.setCancelable(true); 

      // set a message text 
      dialog.setMessage("Please wait while Downloading.."); 


      // show it 
      dialog.show(); 




     } 
protected void alertbox(String title, String mymessage) 
    { 
    new AlertDialog.Builder(this) 
     .setMessage(mymessage) 
     .setTitle(title) 
     .setCancelable(true) 
     .setNeutralButton("OK", 
     new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int whichButton){} 
     }) 
     .show(); 
    } 

编辑:2011年8月19日
可以取消进度使用下面的代码对话框。请尝试

dialog.setButton("Cancel",new DialogInterface.OnClickListener() { 


      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
       // TODO Auto-generated method stub 

      } 
     }); 
+0

感谢您的评论,其作品现在....但我已经给了这onclisk在一个开始按钮,因为前进的进度对话框没有能够点击停止按钮来停止行动..... –