Progress bar with threads

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Progress bar with threads

Postby kitty » Wed Dec 16, 2009 6:03 am

Hi all,

I'm writing an application for uploading a file to server, the uploading status should be shown with the Progress bar. I've used threads to display the completion status of the uploading process.

I'm not posting my code because its confidential, i'm posting a sample code which resembles my problem in which i'm trying to print some numbers in one thread and displaying the progress bar in another thread. But it is not working when i remove the while loop in the first run method, the Progress bar is not showing the status, but if i don't remove that loop the numbers are being printed repeatedly for 100 times.

Here is my code so please come up with your suggestions, its urgent.

Thanks in advance
Kitty

PBar.java

public class Pbar extends Activity {

int progress=0;
ProgressBar pbar;
int progressStatus=0;
Handler handler=new Handler();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

pbar=(ProgressBar)findViewById(R.id.progressbar);

new Thread(new Runnable()
{
public void run()
{
while(progressStatus < 100)
{
progressStatus=method();
System.out.println("PS---->"+progressStatus);
handler.post(new Runnable()
{
public void run()
{
pbar.incrementProgressBy(10);
}
});
}
handler.post(new Runnable()
{
public void run()
{
pbar.setVisibility(8);
}
});

}
private int method()
{
try
{
sample();
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
return ++progress;
}
}).start();
}


public void sample()
{
for(int i=0;i<100;i++)
{
System.out.println("i----->"+i);
}
}
}
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests