当前位置: 动力学知识库 > 问答 > 编程问答 >

multithreading - My ProgressBar does not appear on Android

问题描述:

I have this code for the xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="15px"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Loading..."

/>

<ProgressBar android:id="@+id/progressBar"

android:paddingTop="10px"

style="?android:attr/progressBarStyleHorizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:max="100" />

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/txt"

/>

</LinearLayout>

Then for the class :

public class Loading extends Activity {

ProgressBar bar;

TextView txt;

int total=0;

boolean isRunning=false;

// handler for the background updating

Handler handler=new Handler() {

@Override

public void handleMessage(Message msg) {

total=total+5;

String perc=String.valueOf(total).toString();

txt.setText(perc+"% completed");

bar.incrementProgressBy(5);

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.loading_bar);

bar=(ProgressBar)findViewById(R.id.progressBar);

txt=(TextView)findViewById(R.id.txt);

}

public void onStart() {

super.onStart();

// reset the bar to the default value of 0

bar.setProgress(0);

// create a thread for updating the progress bar

Thread background=new Thread(new Runnable() {

public void run() {

try {

for (int i=0;i<20 && isRunning;i++) {

// wait 1000ms between each update

Thread.sleep(1000);

handler.sendMessage(handler.obtainMessage());

}

}

catch (Throwable t) {

}

}

});

isRunning=true;

// start the background thread

background.start();

}

public void onStop() {

super.onStop();

isRunning=false;

}

}

And with this I call it on another activity:

public void onClick (View v)

{

switch (v.getId())

{

case R.id.buttonNext3:

Intent Loading = new Intent(this, Loading.class);

startActivity(Loading);

getContent();

Call();

db.generation();

Intent Next3 = new Intent(this, FoodPlanning.class);

startActivity(Next3);

break;

}

}

But it is not work on mine, it just show blackscreen but it still on progress .... I want the loading bar appear when I click the button and the process still on progress. any idea? Thx u

网友答案:

As suggested by Adil, AsyncTask is the preferred method to run background tasks with easy options to run certain commands on the UI thread, while pushing the rest to the background. See this example from the SDK (http://developer.android.com/reference/android/os/AsyncTask.html):

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
 protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
         totalSize += Downloader.downloadFile(urls[i]);
         publishProgress((int) ((i / (float) count) * 100));
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
}

Note the "onProgressUpdate" method as well as the doInBackground.

分享给朋友:
您可能感兴趣的文章:
随机阅读: