I've one thread, but i'd like to wait for it to finish before continue with the next actions. How could i do it?
new Thread(
new Runnable() {
#Override
public void run() {
mensaje = getFilesFromUrl(value);
}
}).start();
here i'd like to put something (but loops) that knows when thread finishes and evaluate the result (mensaje)
btnOk.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
lblEstado.setText(mensaje);
if(mensaje.equals("Importado"))
startActivity(new Intent(ScanUrl.this, MainActivity.class));
Thread t = new Thread(new Runnable() {
#Override
public void run() {
mensaje = getFilesFromUrl(value);
}});
t.start(); // spawn thread
t.join(); // wait for thread to finish
Related
This code is inside MainActivity class.
Thread thread1 = new Thread(new Runnable() {
#Override
public void run() {
Thread thread2 = new Thread(new Runnable() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), NextActivity.class);
startActivity(i);
}
});
thread2.start();
}
});
thread1.start();
When i run this code nothing happend.
startActivity should be run in the main thread, so if you need to run it from your thread2, I'd post it to run in the main thread.
Thread thread2 = new Thread(new Runnable() {
#Override
public void run() {
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
#Override
public void run() {
Intent i = new Intent(context, NextActivity.class);
startActivity(i);
}
mainHandler.post(myRunnable);
};
You cannot use other threads to work on UI or calling other activities,
You can do it from main UI thread only.
Intent i = new Intent(getApplicationContext(), NextActivity.class);
startActivity(i);
Running other threads means, you are doing some extra tasks simultaneously along with the main thread.
If I start a new thread by calling the start() method, and then start a new Activity from its run method, is my next activity and all the followups activities considered to be in my new Thread?
Thread test = new Thread(new Runnable() {
#Override
public void run() {
Intent n = new Intent(this, SecActivity.class);
startActivity(n);
}
});
test.start();
Or any other mechanism to delay code execution without hanging the main thread?
Use a Handler's postDelayed(). Documentation
Java example
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
//logic
}
}, 2000);
or
Thread({
try{
Thread.sleep(2000)
} catch (e: Exception) {
//
}
//logic
}).start()
I am trying to make my application launcha splash screen for 5 seconds while initializing various web services in the background. Here is my code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Splash screen view
setContentView(R.layout.splashscreen);
final SplashScreen sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread()
{
#Override
public void run()
{
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex)
{
}
finally
{
finish();
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen, Splash_testActivity.class);
startActivity(intent);
stop();
}
}
};
mSplashThread.start();
for (int i=0;i<100;i++)
Log.d("splash test", "initialize web methods");
}
Now what I think should happen is that while the splash screen is displayed, the application should log "initialize web methods."
But what actually happens is that the log is added only after the slash screen disappears.
What am I doing wrong??
Try to do it this way. This tutorial is simple and flexible. This is what you need:
// You initialize _splashTime to any value
// thread for displaying the SplashScreen
Thread splashTread = new Thread() {
#Override
public void run() {
try {
int waited = 0;
while(waited < _splashTime)) {
sleep(100);
waited += 100;
}
}
} catch(InterruptedException e) {
// do nothing
} finally {
finish();
startActivity(new Intent("com.droidnova.android.splashscreen.MyApp"));
stop();
}
}
};
splashTread.start();
Note: This code is adopted from the above url.
Run your Thread Using Handler or AsyncTask.
here I have a function of checkupdate() in which my application check updates available for users, so i need to display two progressdialog one when server is under request(Checking process) and other when synchronization process is ongoing and both the process is done on the load of application.
Now problem is I'm unable to display these two progressdialog boxes, here only the first thread checking updates is running and application is terminated.
Waiting for your valuable answers.
public void CheckUpdate()
{
//----------------Process of checking the updates--------------------------
try
{
progressDialog = ProgressDialog.show(this, "Wait", "Checking update...", false, true);
Thread thr1 = new Thread()
{
public void run()
{
int Flag = call.CheckUserUpdate(UsrId);
Update = Flag;
progressDialog.dismiss();
//stop();
interrupt();
}
};
thr1.start();
}
catch(final Exception e)
{
}
//---------------Process of Synchronization----------------------------------------------
try
{
progressDialog1 = ProgressDialog.show(this, "Wait", "Synchronizing...", false, true);
Thread thr2 = new Thread()
{
public void run()
{
if(Update == 1)
{
SyncData();
final int UpdateFlag = 1;
call.UpdateUserUpdate(UsrId, UpdateFlag);
progressDialog1.dismiss();
}
else
{
progressDialog1.dismiss();
}
progressDialog1.dismiss();
}
};
thr2.start();
}
catch(final Exception e)
{
}
}
Starting the new threads causes them to start executing elsewhere. Your program here starts two threads, gets to the end of main() and exits, causing the JVM/OS to kill your newly started threads that never really got a chance to run very far.
You need to add an idle loop (or similar) that yields control on the main thread while the worker threads to their thing, assuming you don't have any work happening on the main thread.
Do I understand correctly what you want?
Your UI activitiy has two progressbars which should be updated based on either
update check process
synchronization process
then you should create a handler in your UI activity and use two separate threads one for update check and the other for synchronization check
Both send their progress info to the main thread like:
mHandler.obtainMessage(Main_screen.MESSAGE_PGROGRESS_CHANGE_UPDATE, state, -1)
.sendToTarget();
in the other you have
mHandler.obtainMessage(Main_screen.MESSAGE_PGROGRESS_CHANGE_SYNCHRINIZATION, state, -1)
.sendToTarget();
In your UI activity you have the handler looking like this...
private final Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (DEBUG)
Log.i(this.getClass().getSimpleName(),
"-> "
+ Thread.currentThread().getStackTrace()[2]
.getMethodName());
switch (msg.what) {
case MESSAGE_PGROGRESS_CHANGE_UPDATE:
if (DEBUG)
Log.i(this.getClass().getSimpleName(),
" MESSAGE_PGROGRESS_CHANGE_UPDATE: " + msg.arg1);
// do your update of progressbar or whatever here
break;
case MESSAGE_PGROGRESS_CHANGE_SYNCHRINIZATION:
if (DEBUG)
Log.i(this.getClass().getSimpleName(),
" MESSAGE_PGROGRESS_CHANGE_SYNCHRINIZATION: " + msg.arg1);
// do your update of progressbar or whatever here
break;