I read about this class:
Do not instantiate this class directly, instead, call
createSpeechRecognizer(Context). This class's methods must be invoked
only from the main application thread.
I suppose that the main application thread is the main Activity of an Android application...
Why should this class's methods be invoked only from the main application thread?
The main application thread is also known as the UI thread.
This is done to ensure thread safety. (No two processes can modify the same value at the same time).
Please see: Why can only the UI thread in Android update the UI?
Related
I have the following Kotlin code, executed from the UI thread of Android:
Runnable {
doSomeSuff() // Which thread will it run?
}.run()
On which thread will it run? The UI thread?
Your Runnable will be executed on the Thread it was created. In your case - UI thread. The question is - what do you want to achieve? There are bunch of built-in capabilities to perform background related work. I'll provide wider answer - if you explain your requirements.
From Android Documentation:
The Runnable interface should be implemented by any class whose
instances are intended to be executed by a thread. The class must
define a method of no arguments called run.
This interface is designed to provide a common protocol for objects
that wish to execute code while they are active. For example, Runnable
is implemented by class Thread. Being active simply means that a
thread has been started and has not yet been stopped.
Why it is different from Thread:
When an object implementing interface Runnable is used to create a
thread, starting the thread causes the object's run method to be
called in that separately executing thread.
I Have several activities handling diferent screens with information that is loaded asynchrounously via AsyncTask.
All data fetching are done in doInBackground()
All UI operations are done in onPostExecute()
In some instances I get the error: "Can't create handler inside thread that has not called Looper.prepare()"
If I do the Looper.prepare(), it crashes if a new activity is instanced for this class.
It's a weird behaviour that I'm able to find why it happens. The other screens with similar behaviour work as expected. There are no differences between them that I can find.
Has anyone had this behaviour?
"Can't create handler inside thread that has not called Looper.prepare()" means that you are trying to call AsyncTask.execute() outside of the UI thread.
There are few rules you should follow using AsyncTask:
The task instance must be created on the UI thread.
execute(Params...) must be invoked on the UI thread.
Also it can mean than you are creating Handler object inside doInBackground method.
All:
I am studying code that has a handler that is declared and instantiated along with other instance variables:
public class SomeActivity extends Activity {
Handler handler = new Handler(); // What thread is this taking place on?
// rest of class omitted
}
so is it being instantiated on the UI thread? I hope so, because it is used to post a Runnable to a ProgressBar, and my understanding is that the ProgressBar should only be manipulated on the UI Thread.
The Android docs say that something created in onCreate() is:
An application's activities run on the application's UI thread. Once
the UI is instantiated, for example in the activity's onCreate()
method, then all interactions with the UI must run in the UI thread."
but this is happening before onCreate().
Thanks for any info,
Michael
so is it being instantiated on the UI thread?
Yes. A Handler will exist in the Thread where it is created. You are creating yours on the main / UI Thread, so it can access UI elements.
but this is happening before onCreate().
This won't effect which Thread the Handler runs on.
Everything happens on the main thread (aka UI thread) unless you specifically run it on a background thread. So, yes, your Handler is created in the UI thread.
in my project I have several activities and 1 application defined. The application object contains some methods some of the activities are using.
Within the activity I am calling the methods of the application object out of an inner class that extends AsyncTask. Is this correct resp. is the method of the application is also executed in the AsyncTask process?
Thanks a lot!
Neo74
It depends on where you are calling that method in the Asynctask.
on "doInBackground" everything you are doing will be run on a separate thread.
and on "onPreExecute/onPostExecute" will be run on the main thread(or UI thread).
The application process along with all the threads will run as long as the system is not going to kill it.
You can read more about the process life cycle here:
http://developer.android.com/guide/components/processes-and-threads.html#Lifecycle
If I want to implement a Handler object from within my Activity's main thread, do I need to call Looper.prepare() beforehand, or do activities already have their own internal loopers? Does the same hold true if I want to instantiate a Handler instance from within a Service?
The main thread already has its own looper. See Context.getMainLooper(). The main thread encompasses all Activities and Services in the application.