2013-01-17 91 views
0

我正在製作一個非常簡單的程序。我只想在第一和第二項活動之間顯示進度條。2個活動之間的進度條

活動1只包含一個按鈕,當我點擊它時,它應該在同一個活動中顯示一個進度條並持續顯示,直到出現僅包含正在顯示的消息的第二個活動。

+2

在進展過程中應該發生什麼? – fiddler

回答

0

調用第二個活動並在執行重要任務之前顯示進度條的效率更高。 看看類AsyncTask是如何工作的。

0

我假設你想在移動到第二個活動時實現長時間運行的後臺任務。如果是這種情況,則執行AsyncTask

還有的AsyncTask的4種方法,你可以實現,但你可以做如下:

  1. doInBackground() - 實現在此您的長期運行的任務
  2. onPostExecute() - 移至第二活動
1

ProgressBar用於任何需要更多時間的進程,那麼我們應該使用異步任務並顯示進度對話框,直到完成任務

,如果你有那麼這樣的任何需求請按照下列

private class LongOperation extends AsyncTask<String, Void, String> 
{ 
    protected void onPreExecute() 
    { 
     progressDialog = new ProgressDialog(activity.this); 
     progressDialog.setTitle("Processing..."); 
     progressDialog.setMessage("Please wait..."); 
     progressDialog.setCancelable(true); 
     progressDialog.show(); 
    } 

    protected String doInBackground(String... params) 
    { 
     try 
     { 
      //BackGround process(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    protected void onPostExecute(String result) 
    { 

     progressDialog.dismiss(); 
     Intent n = new Intent(firstactivity.this, secondactivity.class); 
     startActivity(n); 
    } 
    } 

如何調用該

ProgressDialog progressDialog; 
LongOperation mytask = null; 
mytask = new LongOperation(); 
mytask.execute(); 
0

看一看這個例子中這個wiil清除所有概念相關的進度條和的AsyncTask

import java.io.BufferedInputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import android.app.Activity; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class download extends Activity { 

public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
private Button startBtn; 
private ProgressDialog mProgressDialog; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    startBtn = (Button)findViewById(R.id.startBtn); 
    startBtn.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v) { 
      startDownload(); 
     } 
    }); 
} 

private void startDownload() { 
    String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg"; 
    new DownloadFileAsync().execute(url); 
} 
@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DIALOG_DOWNLOAD_PROGRESS: 
     mProgressDialog = new ProgressDialog(this); 
     mProgressDialog.setMessage("Downloading file.."); 
     mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     mProgressDialog.setCancelable(false); 
     mProgressDialog.show(); 
     return mProgressDialog; 
    default: 
     return null; 
    } 
} 

class DownloadFileAsync extends AsyncTask<String, String, String> { 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    showDialog(DIALOG_DOWNLOAD_PROGRESS); 
} 

@Override 
protected String doInBackground(String... aurl) { 
    int count; 

try { 

URL url = new URL(aurl[0]); 
URLConnection conexion = url.openConnection(); 
conexion.connect(); 

int lenghtOfFile = conexion.getContentLength(); 
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile); 

InputStream input = new BufferedInputStream(url.openStream()); 
OutputStream output = new FileOutputStream("/sdcard /some_photo_from_gdansk_poland.jpg"); 

byte data[] = new byte[1024]; 

long total = 0; 

    while ((count = input.read(data)) != -1) { 
     total += count; 
     publishProgress(""+(int)((total*100)/lenghtOfFile)); 
     output.write(data, 0, count); 
    } 

    output.flush(); 
    output.close(); 
    input.close(); 
} catch (Exception e) {} 
return null; 

} 
protected void onProgressUpdate(String... progress) { 
    Log.d("ANDRO_ASYNC",progress[0]); 
    mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
} 

@Override 
protected void onPostExecute(String unused) { 
    dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
} 
} 
}