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
Related
this is a piece of code from my project, i need this thread to be over untill the end and only then go to the last Log.i() and finish the function.
public void delay3Seconds(final String txt1, final String txt2, final String s, final Intent i)
{
//keepMoving= false;
counter= 3;
secondsBool= true;
if(!errorMonitor)
{
Log.i("Main.delay3Seconds()", s+" in 3 seconds");
new Thread()
{
public void run()
{
while(secondsBool)
{
try {
Thread.sleep(1500);
}
catch (InterruptedException e){e.printStackTrace();}
if(!errorMonitor)
{
handler.post(new Runnable()
{
public void run()
{
final DialogFragment loadDF= new RecDialog(MainActivity.this, txt1, txt2, s+(counter--)+" שניות", null, false, true, ll.getWidth(), ll.getHeight());
loadDF.show(getSupportFragmentManager(), "RecDialog");
dialog.dismiss();
dialog= loadDF;
if(counter == 0)
secondsBool= false;
}
});
}
else
secondsBool= false;
}
if(!errorMonitor)
{
handler.post(new Runnable()
{
public void run()
{
dialog.dismiss();
if (i.resolveActivity(getPackageManager()) != null)
{
Log.i("Main.delay3Seconds()", "resolveActivity != null");
setResolveNotFail(true);
Log.i("Main.delay3Seconds()", "resolveNotFail = "+resolveNotFail);
startActivity(i);
}
else
{
Log.i("Main.delay3Seconds()", "resolveActivity == null");
setResolveNotFail(false);
Log.i("Main.delay3Seconds()", "resolveNotFail = "+resolveNotFail);
}
}
});
}
}
}.start();
}
Log.i("Main.delay3Seconds()", "(end) resolveNotFail = "+resolveNotFail);
}
i can't figure out how to do that. i tried using synchronized(), but i probably use it wrong because the function finishes itself first and only then the thread works, simultaneously to the activity.
i would appreciate any tips on how to do that..
That's absolutely not how or why you use a Thread. First off, if this is the UI thread you should never pause it for 3 seconds. Second, the entire point of a Thread is to work in parallel. You never want one thread to pause and wait for another. If you need something on Thread A to occur only when Thread B is done, you send a message via a handler, semaphore or other method to Thread A when Thread B is done.
Looking at your code, it seems like you should throw it out and reimplement it with a timer.
https://docs.oracle.com/javase/tutorial/essential/concurrency/join.html
Just call the .join() method after you start your thread so the calling thread will wait until your new thread executes.
Something like this:
Thread t = new Thread(...);
t.start();
t.join();
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
}
});
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();
}
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.
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.