2017-05-26 64 views
1

我正試圖在Android上製作一個簡單的應用程序。ProgressDialog未啓動 - Android(java)

編輯

新代碼:

package com.sirseni.simpleandroidwebviewexample; 

/** 
* Created by otsma on 26.05.2017. 
*/ 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 



public class Hack extends Activity { 
    Button b1; 
    private ProgressDialog progress; 
    // Splash screen timer 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.hack); 
     b1 = (Button) findViewById(R.id.button); 

    } 
    private int jumpTime = 0; 

    public void download() { 
     //initializing progress dialog 
     progress = new ProgressDialog(this); 
     progress.setMessage("Changing Files"); 
     progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     progress.setProgress(0); 
     progress.show(); 

     final int totalProgressTime = 100; 
     //creating a thread 
     final Thread t = new Thread() { 
      @Override 
      public void run() { 
       while(jumpTime < totalProgressTime) { 
        try { 
         jumpTime += 5; 
         // if we have to update the view, we have to execute the code in UI thread. runOnUiThread() gives us that functionality 
         runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           // updating view in UI thread 
           progress.setProgress(jumpTime); 
           progress.setMessage("Hello" + jumpTime); 
          } 
         }); 
         sleep(200); 
        } catch (InterruptedException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 
     }; 
     t.start(); 
    }} 

現在,它的那樣工作,按鈕用戶敲擊一下,progressdialog正在出現,但每次它爲0%和0/100。如何解決它?例如,20秒後100%以及如何製作,當它達到100%時,新的活動開始?

謝謝你們的幫助!

回答

0

請通過下面的代碼更新的下載方法:

private int jumpTime = 0; 

public void download() { 
    //initializing progress dialog 
    progress = new ProgressDialog(this); 
    progress.setMessage("Changing Files"); 
    progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    progress.setProgress(0); 
    progress.show(); 

    final int totalProgressTime = 100; 
    //creating a thread 
    final Thread t = new Thread() { 
     @Override 
     public void run() { 
      while(jumpTime < totalProgressTime) { 
       try { 
        jumpTime += 5; 
        // if we have to update the view, we have to execute the code in UI thread. runOnUiThread() gives us that functionality 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          // updating view in UI thread 
          progress.setProgress(jumpTime); 
          progress.setMessage("Hello" + jumpTime); 
         } 
        }); 
        sleep(200); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
    }; 
    t.start(); 
} 
+0

@AvjitKarmakar它不工作。 jumpTime在紅色下劃線。在哪裏我可以給新的意圖運行? –

+0

@MarkusMaleńki請告訴我錯誤。 –

+0

@AvjitKarmakar這是說 - 「變量jumpTime是從內部類訪問,需要宣佈最終 –

0

試試這個。

progress = new ProgressDialog(this); 
progress.setMessage("Changing Files"); 
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
progress.setMax(100); 
progress.setProgress(0); 
progress.show(); 

int jumpTime = 0; 
Handler ha = new Handler(); 
ha.post(new Runnable() { 

    @Override 
    public void run() { 
     //call function 
     jumpTime += 5; 
     progress.setProgress(jumpTime); 
     progress.setMessage("Hello" + jumpTime); 

     if(jumpTime < 100){ 
      ha.postDelayed(this, 200); 
     } 
     else{ 
      //do code 
     } 
    } 
});