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.
Related
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
I am writing an android app, in which I have 1 button and 1 progress bar as UI elements.
The main aim of this app is when user presses this button, it has to create a database which contains all phone book contacts in customized format, means I am reading Contacts database and manipulating for my requirement.
So I am using SQLiteOpenHelper for database operations. I written a method downloadPhonebook() to perform all required operations. I written app such that when user presses button I am making progress bar visible and calling this method.
In this case, UI was hanged after clicking button and showing a dialog with Force Close and Wait buttons, after 15 seconds.
To avoid this I tried following mechanisms.
-> Broadcast Button click message and call method downloadPhonebook(). Here no use, same problem occurred.
-> Used a Thread and AsyncTask to call this method, here I got Runtime exceptions like Couldn't create Handler inside a Thread, Looper.prepare not called. I tried calling Looper.prepare() and Looper.loop() even exceptions occurred.
-> I tried with Android Service and Broadcast intent, again same problem UI hanged.
If anybody faced this problem or knows the solution or knows how to use Looper.prepare and Looper.loop please reply me. Thanks.
the workflow should be something like this: create a handler in your main class, add a handler in your sql helper class, pass the handler from the main class to the sql helper class when you create it. Run the download on separate thread from your main class, when download is ready, call yourHandler.sendEmptyMessage(0). You should override the Handler.handleMessage (I'm not sure about the exact name of the method) in your main class. You can also send messages to update the progress, read about Andoid Handler for more information
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.
In an activity A I download several files from the server to my device.
A progressBar shows the progress of the current download.
I can do multiple downloads at once and I want an activity B shows a list of all my downloads with progressBar for each download.
-The problem is that I do not really know how to share the progress of each download to activity B when I start it via a menu.
Set up a downloading Service instead of just using an AsyncTask to fetch your files. Then, any Activity that's interested in the progress of said downloads can bind the Service and register a listener for progress updates which are then reflected in whatever UI is appropriate to the respective Activitys.
I am doing something similar and have found success using local broadcasts. I have all the upload logic contained in a Service, which in turn uses an Async task, and when I need to update progress I send out the broadcast. In onResume of my activities I register to receive the notifications and onPause I unregister. That way the current activity can update.
1)Declare Progress Bar inside Application Object(A class that extends Application Supper class). Now Progress bar is accessible throughout the Application.
2)Update Progress Bar in Activity A.
3)Get Instence of ProgressBar in Activity B by calling getApplication();
So, MyActivity will have a progress bar. MyActivity's BroadcastReceiver will catch the intent "START_TASK". After that, as time passes, I want to update MyActivity's progress bar. I was originally trying to handle this from a service, but I don't know how to access MyActivity's progress bar from a service (or how to create a progress bar in the service and place it on MyActivity's screen).
You don't access an Activity's progress bar from a service: you have the service send messages to your activity, at a rate appropriate to whatever it is you're trying to do, and make your activity respond to those messages by updating its own progress bar.
It would help if you could give a little more detail about the nature of the background task you want the progress bar to track: if the background task is the important thing, and the activity is merely showing you how it's progressing, then a service is the right thing to use. If it's short-lived and doesn't make sense outside then activity then you'd probably be using an AsyncTask and updating the progress bar in its onProgressUpdate (although be careful you don't leak the AsyncTask when the screen orientation changes).
Maybe look here: How to have Android Service communicate with Activity