Enabling and update progressbar from a thread - android

Progressbar will not show when I try to access it from a thread nor update the progress. I would prefer to just have a progress spinner be displayed until the network activity is complete, and this network activity is just to post some text data to the server.This is the code I have so far, any help is appreciated thanks,
Thread th = new Thread(){
public void run() {
try {
sleep(5000);
while(cnt < 5000){
activty.this.runOnUiThread(new Runnable(){
public void run()
{
ProgressBar pd = (ProgressBar)findViewById(R.id.progressBar1);
pd.setVisibility(0);
pd.setMax(100);
pd.setProgress(cnt);
cnt += 100;
}
});
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int cnt = 100;
};
th.start();
`

This is exactly the case where you would want to use AsyncTask, which processes data in a separate thread and contains methods publishProgress and onProgressUpdate to update your UI on the UI thread.
The Processes and Threads Guide has an example of how to implement an AsyncTask as well.

Related

how to force the AsyncTask to wait

i want the AsyncTask to wait till it finishes. so i wrote the below code and i used .get() method as follows and as shown below in the code
mATDisableBT = new ATDisableBT();
but at run time the .get() doesnt force ATDisableBT to wait, becuase in the logcat i receive mixed order of messages issued from ATDisableBT and ATEnableBT
which means .get() on ATDisableBT did not force it to wait
how to force the AsyncTask to wait
code:
//preparatory step 1
if (this.mBTAdapter.isEnabled()) {
mATDisableBT = new ATDisableBT();
try {
mATDisableBT.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
//enable BT.
this.mATEnableBT = new ATEnableBT();
this.mATEnableBT.execute();
You can do this way:
doInBackground of AsyncTask
#Override
protected Void doInBackground(String... params) {
Log.i("doInBackground", "1");
synchronized (this) {
try {
mAsyncTask.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.i("doInBackground", "2");
return null;
}
Outside this function from where you have to nstrong textotify AsyncTask to release from wait state:
new CountDownTimer(2000, 2000) {
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
synchronized (mAsyncTask) {
mAsyncTask.notify();
}
}
}.start();
Here I have notified AsyncTask by CountDownTimer after 2 seconds.
Hope this will help you.
You should execute AsyncTask on UI thread, so using get() - which will block it makes no sense - it might get you ANR error.
If you are on HONEYCOMB and up, then AsyncTasks are executed on single executor thread, serially - so your mATEnableBT should get executed after mATDisableBT. For more see here:
http://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...)
You might also switch from AsyncTask to Executors. AsyncTask is implemented using executors. By creating single threaded executor you make sure tasks will get executed serially:
ExecutorService executor = Executors.newSingleThreadExecutor();
//...
executor.submit(new Runnable() {
#Override
public void run() {
// your mATDisableBT code
}
});
executor.submit(new Runnable() {
#Override
public void run() {
// your mATEnableBT code
}
});

While loop not working in Android

I'm trying to make a while loop that will make a number count up. However, when the app runs, it just crashes. Here's the code I used:
Thread Timer = new Thread(){
public void run(){
try{
int logoTimer = 0;
while(logoTimer != 5000){
sleep(100);
logoTimer = logoTimer + 1;
button.setText(logoTimer);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
Timer.start();
Am I doing something wrong? Do I need to add something in the .xml file? Thanks in advance.
it does crash for two reasons
you are touching the UI from a thread that is not the UI Thread
you are calling setText(int), which looks up for a string inside string.xml. If it does not exits, the ResourceNotFoundException will be thrown.
Edit: as G.T. pointed out you can use button.setText(logoTimer+""); to avoid the exception at point 2
You need to run the setText for the button on the UI thread!
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
button.setText(logoTimer);
}
});

UI blocks during a function call

Suppose that I have 10 text views in my layout. I want to change their background one by one with a small delay between each operation. Here's a sample code:
public void test(){
for (int i=0 ; i < 10 ; i++){
myTextViews[i].setBackgroundResource(R.color.red);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
}
}
The problem is that during the time of running this function main thread blocks and backgrounds don't change. They change all together when the program finishes running the function. What should I do if I want the user to see each background is changed at the correct time and then next one...?
When you call Thread.sleep(), the thread you are actually blocking is the UI thread, which is why you're seeing a hang-up. What you need to do is start up another Thread to handle the sleeps, or delays, that you want, and then utilize the runOnUiThread method
Try this:
public void test() {
new Thread(new Runnable() {
public void run() {
for(int i = 0; i < 10; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//anything related to the UI must be done
//on the UI thread, not this thread we just created
runOnUiThread(new Runnable() {
public void run() {
myTextViews[i].setBackgroundResource(R.color.red);
}
});
}
}
}).start();
}

Android Progress DialogBox is not displaying?

I am trying to show a ProgressDialog for five seconds on user interface to block the UI so that my locationClient can get connected and after five seconds.
I am dismissing the dialog box.But the dialog box is not displaying at all and mobile screen goes black for 3-4 seconds.Is it happening because I am running this code On UiThread?. If yes is their any other approach i can follow to stop for 5 seconds and then execute the pending code.
My code for the thread is following:
public void startConnecting(final boolean isSearchWarnings) {
mProgressDialog = ProgressDialog.show(this, "Please wait","Long operation starts...", true);
MainActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
for (int i = 0; i <= 5; i++) {
try {
if (mLocationClient.isConnected()) {
break;
} else {
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
mProgressDialog.dismiss();
if (isSearchWarnings) {
new getWarnings().execute(false);
} else {
new getWarnings().execute(true);
}
}
});
}
You have this
MainActivity.this.runOnUiThread(new Runnable()
And then
Thread.sleep(1000);
You are calling sleep on the ui thread. This blocks the ui thread which you should not do. You will get ANR.
http://developer.android.com/training/articles/perf-anr.html
If you want to cause a delay use handler
give a delay of few seconds without using threads

Several setMessage updates (ProgressBar)

new Thread() {
public void run() {
try {
Progress.setMessage("Scanning Apps ...");
int CurrentNumber = 0;
while (CurrentNumber <= 99) {
Progress.setProgress(CurrentNumber);
Progress.setMessage(CurrentNumber + "");
sleep(100);
CurrentNumber ++;
} catch (Exception e) {
}
}
}.start();
So.. this code is not working :(
What am I doing wrong?
Without Progress.setMessage(CurrentNumber + ""); it is working perfectly...
write handler
e.g.
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
//set message here
}
};
Try looking at AsyncTask which handles progress updates, giving you a chance to set your progress message on the UI thread.
I'm assuming that Progress is an instance of ProgressDialog. You can only update the UI on the UI (main) thread. Read about threading and the UI on the dev site.

Categories

Resources