Update UI from another thread (progressDialog) - android

I searched and found what looked like the exact question I have (http://stackoverflow.com/questions/4369537/update-ui-from-thread). Unfortunately I'm still not able to get this working.
I have an ImageView on my layout. When a button is pressed, a ProgressDialog is created and shown. As the thread processes data, it calls the incrementProgressBy() method on a handler in the UI. Inside that handler, I update the ImageView using setImageBitmap(), and then I Invalidate it. It never updates. Next, I tried calling ImageView.postInvalidate() from the progress bar's thread. It still does not update until the progress bar completes and is removed.
Any ideas?

The solution here was to use an AsyncTask. It's able to communicate with the UI via handlers.

Related

Call function 1 time after layout is inflated

I'm making an android app that is pulling a list of objects to be displayed on the screen from a REST service.
What I need to happen is a progress dialog show while the results are being fetched. The issue I'm having is that I'm not sure which function to put this in. I've tried OnWindowsFocusChanged but it's being called repeatedly and the progress dialog doesn't pop up. I've tried onCreate, onStart, onResume but they're all called before the view is inflated so nothing renders.
Does anyone know how this is done?
Thank you for any help,
Brad
Alright, first of all those apis wont work for you, what you need is AsyncTask,here is what you do,
First implement AsyncTask, put all your downloading code into doInBackground(), once done.
display a progress bar, right before you exceute your asynctask calling task.execute(..)
once your downloding form rest is complete, return your result from doInBackGround
Now control comes to onPostExecute of your AsyncTask, after populating your list, just dismiss the progressbar.
If you wish to indicate downloading percentage to user, you can do same via onProgressUpdate API to which you can push, update via doInBackground.
for more info, read http://developer.android.com/reference/android/os/AsyncTask.html
Hope this help.

AsyncTask that calls JNI Background process with update progress (Android)

I have an AsyncTask that calls a native method, and I want to report the progress.
Is it possible? I can't change the Native since its a black box to me, but I can read its output which is a file, which I can parse as progress.
Thanks,
Eli
As much as I know, its not possible using async task, but its doable using combination of threads and handler.
you can use this pattern:
Use a Class with 2 threads, one for the worker and one for the progress report.
The last updates the progress bar (which is a member).
When finished, call the handler message method to dismiss the progress bar.
You can grab the code from here.
Eli

Android: Blocking RPC with progress dialog

I have an Android App that uses an RPC mechanism to set/get information to/from a server. I call the RPCs from whithin the main thread (blocking) and I want them to be blocking. However, sometimes a call can last for some seconds and I'd like to display an indeterminate progress dialog after some specified time (e.g. 1 second).
I tried to spawn a new thread that makes the call and the main thread waits in a loop (with sleeps) until the call has been finished. Inside this loope I show the progress dialog but this is not working.
Is it possible to show and update the progress dialog inside another Thread or does anybody know a better solution that allows me to use blocking calls?
You cannot do both, make the main thread wait in a loop and show a progress dialog, at the same time. Either the main thread waits or shows the dialog.
Why do you want to block the main thread? Communication over the internet should always be done in a background thread because you never know how long it will take to complete. Do that stuff in AsyncTask and show the progress dialog in main.
Try using AsyncTask. It is an android mechanism which is used to make such network calls. Get the brief detailing of AsyncTask here:
Using AsynTask to show progress bar while attempting to SSH to Server
Using a AsyncTask you can block the user from proceeding ahead. You have to show a progress dialog in the onPreExecute() method of the AsyncTask. All your network related activities will take place in doInBackground(). After the background action is completed there will be a call to onPostExecute() where the progress dialog will be dismissed.
AsyncTask is a asynchronous call because you have 2 threads working simultaneously, one is the UI thread on which you are showing your progress dialog and the other is the non-ui background thread which is fetching your data from the server.
Hope this explanation helps.
I have found a solution that seems to work (at least I havent noticed any problems yet). I know the proper way would be to use somethn like AsyncTask but in my case I have no benefit from it and it complicates the program logic.
To update the UI within a new thread:
new Thread()
{
public void run()
{
Looper.prepare();
... do UI stuff here
Looper.loop();
}
}.start();

Thread UI sleep withtout freezing scrollview

I'm trying to implements a game AI, and I got the following problem :
I'm calling a method from another class my UI Activity class, this method call itself some methods of the UI Activity class (to simulate click on screen among other things), and the things is, at the end of this method, I need to "pause" the game a few seconds to let the user see what did the AI.
So I tried running the method in another thread, but I got the error message providing from editing a widget from another thread. I tried to sleep the UI thread, but by doing that, the user can't use the scrollview anymore, and the changes aren't display before the sleep but after.
So I'd like to know how can I do this ?
(I've read some topics about AsyncTask, Handler, but can't make it work the way I need)
Thank's
You need runOnUiThread.
http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
http://steve.odyfamily.com/?p=12

global indication of activity in android

in my app, i'm uploading some files that can take up to several minutes. i'm thinking of a way to notify the user about activity going on passively by adding a progress bar in my custom title bar. what i want to do is have every activity, each which uses the custom titles, appear with the progress bar until the thread finishes and does a callback which would make invisible the progress bar. can something like this be accomplished?
what seems to make this impossible is that if the user is in an activity with the view loaded, the thread finishing callback would have to manipulate the loaded view resources to disable the progess bar which doesn't seem feasible. are there any suggestions to accomplish this or alternative solutions in keeping a global and passive indication of something going in the background?
You can use a service to achieve this. Services
Basically how it would work, is you bind to the service in each activity when you create the activity. You use this service to start your upload method.
When you bind to the service you pass a handler, which is then used to update your UI in that specific activity. The service will never directly affect the UI (it will be running on a separate thread) instead the handler passes a message back to the UI thread with data in a Bundle, such as upload progress, or a bool to say it's finished.

Categories

Resources