OK I should re-ask my question so its a bit more clearer.
in updateResultsInUI() it is updating the results obtained from the
doSomethingExpensive() which was calculated in the worker thread. Now
what if doSomethingExpensive() is run in a loop continuously and each
time it is executed I need to update something in the UI based on the
result of doSomethingExpensive()? How can I achieve this?
I cannot see how the code below will work for this scenario unless
mHandler.post() was synchronous, i.e the UI thread will get notified
before another doSomethingExpensive() is executed.
This is the example of doSomethingExpensive running in a loop:
protected void startLongRunningOperation() {
// Fire off a thread to do some work that we shouldn't do
directly in the UI thread
Thread t = new Thread() {
public void run() {
while(true) {
mResults = doSomethingExpensive();
mHandler.post(mUpdateResults);
}
}
};
t.start();
}
On Sep 17, 11:07 am, rukiman <[email protected]> wrote:
> OK I'm reading this
> page:http://developer.android.com/guide/appendix/faq/commontasks.html#thre...
>
> The code is as in the example (see below). In my application, the
> worker thread has done loading of large bitmaps, and I need to notify
> the UI thread the filename of the bitmap that was loaded as they get
> loaded. How can I notify the UI thread of the filename of the bitmap
> just loaded? I can see anyway to for updateResultsInUi() to be able to
> take a parameter from the example below.
>
> public class MyActivity extends Activity {
>
> [ . . . ]
> // Need handler for callbacks to the UI thread
> final Handler mHandler = new Handler();
>
> // Create runnable for posting
> final Runnable mUpdateResults = new Runnable() {
> public void run() {
> updateResultsInUi();
> }
> };
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
>
> [ . . . ]
> }
>
> protected void startLongRunningOperation() {
>
> // Fire off a thread to do some work that we shouldn't do
> directly in the UI thread
> Thread t = new Thread() {
> public void run() {
> mResults = doSomethingExpensive();
> mHandler.post(mUpdateResults);
> }
> };
> t.start();
> }
>
> private void updateResultsInUi() {
>
> // Back in the UI thread -- update our UI elements based on
> the data in mResults
> [ . . . ]
> }
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---