I checked and the code inside the callback onBufferAvailable of Allocation.OnBufferAvailableListener is called in a different thread than the UI thread. Is it true? And if so why is that?
Thanks.
Related
Can I use runOnUiThread() inside a thread to update the user interface while running the thread, or handler is more efficient ? .
Yes you can use runOnUiThread() from a non UI thread to update the UI. That method uses a handler internally if you are not currently on the UI thread so using your own handler will not be more efficient. If you are already on the UI thread then the runnable will be executed immediately.
Efficiency isn't a big deal here. Handler might be useful if you need to call another class (for instance, if your thread runs in a class and you need to update the fragment). If you're in a fragment/activity and have access to the elements you need to update, then it may be easier to do runOnUIThread. It's all about code access really.
I personally use callbacks.
So, i am using ExtAudioRecorder: http://i-liger.com/sites/default/files/ExtAudioRecorder.java
My problem is that onPeriodNotification is called on the UI thread. What would be an ellegant sollution to this. Making new thread in every onPeriodNotification call seems very excessive. I tried initializing the recorder in new Thread(), but calls were still made on the UI thread.
You can't. But what you can do is post a message to the second thread in the onPeriodNotification, and then return.
I have a thread which is started in onCreate() and this thread fetches some data.
Is it possible that before the thread is terminated should be able to update the ListView?
Now as the thread is not the UI thread, it cannot directly update the listview array adapter.
Is there a way out?
I was thinking that is it possible to trigger a Handler from thread whose runnable gets executed on main UI thread.
Not sure if I understood your problem completely, but I believe there are two ways to achieve what you want:
1- Start an AsyncTask instead of a thread. AsyncTask's onPostExecute() will run in the UI thread, which means you can do anything UI-related in there. You can start the AsyncTask in onCreate(), and, once it finished, it calls a method on your activity which does:
myAdapter.notifyDataSetChanged();
2- Use runOnUIThread()
Use your Activity's runOnUiThread(Runnable action)
link
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.
Can someone explain to me what exactly the UI thread is?
On developer.android.com it says about the runOnUiThread function
public final void runOnUiThread (Runnable action)
Since: API Level 1 Runs the specified action on the UI thread. If the
current thread is the UI thread, then the action is executed
immediately. If the current thread is not the UI thread, the action is
posted to the event queue of the UI thread.
Does the UI thread mean that this will be run everytime the activity is pushed the the background by some ui activity like incoming call or screen dimming etc.? If not, what exactly does the UI thread include ?
Thank you
The UIThread is the main thread of execution for your application. This is where most of your application code is run. All of your application components (Activities, Services, ContentProviders, BroadcastReceivers) are created in this thread, and any system calls to those components are performed in this thread.
For instance, let's say your application is a single Activity class. Then all of the lifecycle methods and most of your event handling code is run in this UIThread. These are methods like onCreate, onPause, onDestroy, onClick, etc. Additionally, this is where all of the updates to the UI are made. Anything that causes the UI to be updated or changed HAS to happen on the UI thread.
For more info on your application's Processes and Threads click here.
When you explicitly spawn a new thread to do work in the background, this code is not run on the UIThread. So what happens if this background thread needs to do something that changes the UI? This is what the runOnUiThread is for. Actually you're supposed to use a Handler (see the link below for more info on this). It provides these background threads the ability to execute code that can modify the UI. They do this by putting the UI-modifying code in a Runnable object and passing it to the runOnUiThread method.
For more info on spawning worker threads and updating the UI from them click here
I personally only use the runOnUiThread method in my Instrumentation Tests. Since the test code does not execute in the UIThread, you need to use this method to run code that modifies the UI. So, I use it to inject click and key events into my application. I can then check the state of the application to make sure the correct things happened.
For more info on testing and running code on the UIThread click here
If you execute blocking code (e.g. a Http-Request) in a separate Thread, consider using AsyncTask. Its doInBackground-Method runs on a separate Thread. AsyncTask provides you with methods onProgressUpdate and onPostExecute which are guaranteed to run on the UI thread.
If you need GUI-progress updates (e.g. via a progressbar) call publishProgress inside doInBackground. This leads to a subsequent call of onPublishProgress which is also guaranteed to run on the UI thread.
onPostExecute is automatically called after doInBackground returns.
All UI drawings etc. happen in a separate thread. Its called the UIThread. If you want to make any change to UI u must use make sure it happens in UIThread's context.
Easiest way of doing it is to make use of runOnUiThread