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
Related
I'm developing an Android app that has a basic structure: activity that requests some action from AsyncTask implementer. The implementer has 3 custom methods that should be able to update UI thread with a Dialog and a postExecute() that should update UI thread with a failuer Dialog if an exception is thrown. Here are some questions:
Where should I create the Dialog object? In the activity class or the AsyncTask implementer? What general guidelines should I follow?
Can I update UI thread with a Dialog without waiting for postExecute()?
How can I update UI thread with a picture? Should I create a custom Dialog or is there an easier way?
If the updates - as dialogs with pictures - come one after another, in a sequence how should I deal with it? Should I create some kind of queue? How would you do it?
Thank you in advance :)
1)There's a couple of different ways you can do this. But personally I usually create the dialog in onPreExecute of the AsyncTask, so that the UI for the task is completely self contained.
2)Yes. You can do it in onProgressUpdate. doInBackground should call publishProgress() which will cause onProgressUpdate to be called on the UI thread.
3)Too few details- where do you want the picture? In an existing image view? On top of the current layout? If you just want to display it in a dialog box, an AlertDialog with custom layout would probably work.
4)Depends on the app. Do you want the user to see all the images, or is it ok to miss images in the middle if a new one is sent?
i am making lot of HTTP calls in my applications & switches between the views, now i'm handling the Http calls in a thread, but i want to make user to wait when the http request in progress. How to do this?. I just need to show a wait cursor or loading string.
You can use a ProgressDialog whit a Handler.
Android Progress Dialog Example
Android's indeterminate ProgressDialog tutorial
Cheers
I find the name a bit misleading, but you should show a ProgressBar while background operations conclude.
May be this will helpful. You have to make a background operation using thread concept like AsyncTask. Using this you can hide the actual work from the UI part. And AsyncTask will get unallocated after your operations are completed.
Create a subclass of AsyncTask
Use AsyncTask to do background work
Call onPreExecute() to initialize task
Use a progressbar with setIndeterminate(true) to enable the indeterminate mode
Call onProgressUpdate() to animate your progressbar to let the user know some work is being done
Use incrementProgressBy() for increment progressbar content by a specific value
Call doInBackground()and do the background work here
Catch an InterruptedException object to find end of background operation
Call onPostExecute() to denote the end of operation and show the result
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();
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.
When I download a file from web in android, then I want to show a progress bar in notification area of status bar through service, but I am not able to do this. How can i do it? I am not able to pass the file length in service. I am giving URL in EditText, and I am clicking Download Button. After Click A class will be called on Click Listener, this class is having a function. In that function I am doing processing or functionality of downloading, Now I want to show the progress bar in Notification Area Through service, but I can not able to do this. Please help me. Thanks in advance.
You should not be running networking code (or any long-running/blocking code) inside event handlers. This blocks the main thread (the EDT) and makes application unresponsive, possibly producing ANR. You should use AsyncTask.
To show a notification just create a new Notification(..) and then call NotificationManager.notify(). You can safely create notification in AsyncTask.onPreExecute() method, and them update it from within AsyncTask.doInBackground() method by calling publishProgress(..)
An example ProgressBar in Notification Area. You must use this inside AsyncTask as described in 2.
It should be the service which is downloading the file, not the activity (as I understand you do now). When finished service will notify the activity and (optionally) pass the result. In that case you will be able to update notification progress bar and still interact with a user.