I want to change the activity after the progress bar ends. Means progress Bar thread ends. I am adding the activity2 after the thread. But the activity2 starts as the application runs. Why is it so?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
}
});
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
Intent myIntent = new Intent(getApplicationContext(), activity2.class);
myIntent.putExtra("key", "......"); //Optional parameters
startActivity(myIntent);
You have created a new thread which is responsible for progress bar while on main thread your new activity code is executed. You need to place start activity code within the same thread.
What you can do is :
new Thread(new Runnable() {
public void run() {
while (progressStatus <= 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
}
});
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Intent myIntent = new Intent(getApplicationContext(), activity2.class);
myIntent.putExtra("key", "......"); //Optional parameters
startActivity(myIntent);
}
}).start();
This is because you start the progress bar in a new thread while the start of the intent is in another thread. The thread starting the intent does not wait for the progress bar to be finished because they are asynchronous. You could solve this by starting the intent in the runnable after the while loop is done.
Related
If i have something like example.com/time.php and the output is just 20000 so How can i link this to control time of refresh, Any Idea?
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(10000); // here is the number which should be linked to the webpage output
runOnUiThread(new Runnable() {
#Override
public void run() {
// update View here!
}
});
}
} catch (InterruptedException e) {
}
}
};
t.start();
You should use handler for easier implementation.
Handler handler = new Handler();
Runnable test = new Runnable() {
#Override
public void run() {
//do work
handler.post(test, 4000); //wait 4 sec and run again, you can change it programmatically
}
};
public void stopTest() {
handler.removeCallbacks(test);
}
public void startTest() {
handler.post(test,0); //wait 0 ms and run
}
I am new to android and I want to get a Toast message from the background every 5 seconds but my app becomes not responding. I spent all the day to solve this problem :( but I can't
Can anyone help me?
thanks,
here is my code:
public void onCreate() {
super.onCreate();
System.out.println("Application Created");
new Thread(new Runnable() {
#Override
public void run() {
service1();
while (true) {
try {
Thread.sleep(5000);
mHandler.post(new Runnable() {
#Override
public void run() {
// here to update the UI
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
}
private void service1() {
Toast.makeText(appApplication.this, "Service 1", Toast.LENGTH_LONG).show();
}
You need to use runOnUiThread() when you want to update your UI from a Non-UI Thread, something like this:
private void showMessage() {
new Thread() {
public void run() {
while (true) {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "Service 1", Toast.LENGTH_LONG).show();
}
});
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
Or you can use Timer with TimerTask and Handler:
Timer timer;
TimerTask timerTask;
//we are going to use a handler to be able to run in our TimerTask
final Handler handler = new Handler();
// Call this to start showing the message.
private void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
timerTask = new TimerTask() {
public void run() {
//use a handler to run process
handler.post(new Runnable() {
public void run() {
/**************************/
/** Do your process here **/
/**************************/
Toast.makeText(getApplicationContext(), "Service 1", Toast.LENGTH_LONG).show();
}
});
}
};
//schedule the timer, start run TimerTask then run every 5000ms i.e 5 seconds.
timer.schedule(timerTask, 0, 5000);
}
// Call this to stop the timer.
private void stopTimerTask() {
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
I have a activity in which on server response success I want to show a progress bar for few second/minutes and after few minutes dismiss progress bar and then change activity .How can I do that.
here is my code
public void conditions() throws JSONException {
if (m_oResponseobject.getString("resultdescription").equalsIgnoreCase("Transaction Successful")) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
CProgressBar.getInstance().showProgressBar(getApplicationContext(), "Please wait while rediecting to website");
}
}, 3000);
CProgressBar.getInstance().hideProgressBar();
Intent i = new Intent(CDealAppListingDetails.this, CMainActivity.class);
startActivity(i);
}
}
int DELAY = 3000;
CProgressBar.getInstance().showProgressBar(getApplicationContext(), "Please wait while rediecting to website");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
CProgressBar.getInstance().hideProgressBar();
Intent i = new Intent(CDealAppListingDetails.this, CMainActivity.class);
startActivity(i);
}
}, DELAY);
Try This:
CProgressBar.getInstance().showProgressBar(getApplicationContext(), "Please wait while rediecting to website");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
CProgressBar.getInstance().hideProgressBar();
Intent i = new Intent(CDealAppListingDetails.this, CMainActivity.class);
startActivity(i);
}
}, 3000);
Am new in android .. am trying to implement thread in a android. But am getting error .. I googled and getting answer "AsyncTask", but truly i dont know how to implement
Error message
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
my code
final Thread thread = new Thread(){
#Override
public void run() {
try {
DatabaseHandler dbh = new DatabaseHandler(test.this);
result=dbh.Verify(1);
if(result != ""){
getData();
progress.dismiss();
}
else{
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
Use following code to run in UI thread.
Runnable runnable = new Runnable() {
#Override
public void run() {
handler.post(new Runnable() { // This thread runs in the UI
#Override
public void run() {
DatabaseHandler dbh = new DatabaseHandler(test.this);
result=dbh.Verify(1);
if(result != ""){
getData();
progress.dismiss();
}
else{
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
new Thread(runnable).start();
Seems like you need to create your handler in MainActivity and then pass it further. Like so:
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
}
I am trying to display a dialog in a non-Activity class. Basically, I detect an object in my app, I would like to display a dialog and then switch activities. I'm getting a "java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()" in my logcat.
Here is some of my code:
public ImageTargetsRenderer(Context context) {
this.context = context;
mDialog = new ProgressDialog(context);
}
public void onDrawFrame(GL10 gl) {
testFlag = 0;
// DO NOT RENDER IF THERE IS NO TRACKABLE
if (!mIsActive)
return;
// Call our native function to render content
// RENDER IF THERE IS A TRACKABLE
testFlag = renderFrame();
System.err.println("ImageTargetsRenderer reports: " + testFlag);
if(testFlag > 0 && frameCount > 5)
{
frameCount = 0;
System.err.println("Starting to switch activities.");
mDialog.setTitle("Please wait");
mDialog.setMessage("Please wait");
mDialog.show();
new Thread() {
public void run() {
try{
sleep(5000);
} catch (Exception e) { }
// Dismiss the Dialog
mDialog.dismiss();
}
}.start();
Intent myIntent = new Intent(context, FlashActivity.class);
myIntent.putExtra("com.qualcomm.QCARSamples.ImageTargets.flagTest", testFlag);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(myIntent);
testFlag = 0;
return;
}
frameCount++;
}
Your Dialog should be called from the UIthread so try to use this,
context.this.runOnUiThread(new Runnable() {
#Override
public void run() {
mDialog.show();
}
});
Hope this works.
You do this
context.runOnUIThread( new Runnable() {
public void run() {
// show the Dialog
mDialog.setTitle("Please wait");
mDialog.setMessage("Please wait");
mDialog.show();
}
});
}
Thread.sleep(5000);
context.runOnUIThread( new Runnable() {
public void run() {
// Dismiss the Dialog
mDialog.dismiss();
}
});
This is the exception you will get if you are performing any UI operation on any thread or from any background task. Also context.runOnUiThread won't work.
Instead use:
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
mDialog.dismiss();
}
});
You can use the same for showing where activity is the object of activity.