In which Thread is an Activity called from inside a Runnable? - java

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();

Related

starting android activity from a thread's inside thread.

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.

Android-Starting new Activity in Handler/runnable is really slow

I am making an android app that requires a runnable. I am starting a new activity from the runnable. The new activity comes up and works fine. The issue is that when the call is made to start the activity, it is incredibly slow. It takes a full 5 seconds to start the activity when I want it to be instantaneous.
Boolean handlerrun=true;
Intent intent= new Intent(this,newactivity.class);
int somevalue=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameactivity);
handler=new Handler();
final Runnable r = new Runnable()
{
public void run()
{
if(handlerrun){somevalue++;}
if(somevalue>500){
handlerrun=false;
startActivity(intent);
finish();
}
handler.postDelayed(this, 1);}
}
};
handler.postDelayed(r, 1);
}
The activity starts when somevalue is greater than 500. To stop the handler from increasing the value of somevalue, I use a boolean handlerrun, which only runs the handler when it is true. When somevalue is greater than 500, handlerrun= false so the handler doesn't increase the value. I tried using the handler.removeCallbacksandMessages() method but it didn't work. Logcat doesn't give me any errors.Any help would be appreciated.
You could try something like this:
#Override
protected void onResume() {
super.onResume();
if(done){
return;
}
done = true;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(), YourActivity.class));
finish();
overridePendingTransition(0, 0);
}
}, 5000);
}
That will start YourActivity after 5 seconds approximately.
Hope it helps.

Inform service about finished job so he could be stopped

I have Service, that in onStart I'm calling to existing AsyncTask <> from new Thread.
public void onStart(Intent intent, int startId) {
Thread thread = new Thread() {
public void run() {
try {
//code that fetch data
}
myTask taskA = new myTask ();
taskA.execute();
}
};
thread.start();
}
After the work in taskA is finished, how he can inform Service so he can be stopped.
Firstly you should override both onStart and onStartCommand in a service to accommodate for all range of Android devices.
Regarding stopping a service from within you can you stopSelf() to do that.

why i cannot send android intent from within new thread?

private void Foo() {
// progressDialog = ProgressDialog.show(this, "", "Loading...");
Thread t = new Thread(new Runnable() {
#Override
public void run() {
Intent i = new Intent(this, TodoDetailActivity.class);
i.putExtra("driveUri", file.getAlternateLink());
startActivityForResult(i, SAVE);
}
});
t.start();
}
I get this error:
The constructor Intent(new Runnable(){}, Class<TodoDetailActivity>) is undefined
I have tried to change to explicit declaration, but it didn't help
android.content.Intent i = new android.content.Intent.Intent(this, TodoDetailActivity.class);
though I know there is such a signature from past use.
This line
Intent i = new Intent(this, TodoDetailActivity.class);
Is inside the Anonymous Class Runnable.
Change it to
Intent i = new Intent(getBaseContext(), TodoDetailActivity.class);
Try to change
Intent i = new Intent(this, TodoDetailActivity.class); in
Intent i = new Intent(getActivity(), TodoDetailActivity.class); if you are in a Fragment, or
by
Intent i = new Intent(yourActualActivity.this, TodoDetailActivity.class); if you are in an Activity
Intent need context (of some activity of application), but in thread this (in new Intent(this, TodoDetailActivity.class);) passes the context of you thread, which is not an activity.
so just getApplicationContext() or context of activity form which thread is started like MainActivity.this
You are using Itennt() inside the Thread, so this means thread's object, which is wrong for Intent's syntax.
You should modify your code as follows,
private void Foo()
{
// progressDialog = ProgressDialog.show(this, "", "Loading...");
Thread t = new Thread(new Runnable() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), TodoDetailActivity.class);
i.putExtra("driveUri", file.getAlternateLink());
startActivityForResult(i, SAVE);
}
});
t.start();
}

Wait for a thread before continue on Android

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

Categories

Resources