How can I manage a ProgressBar in the background? - android

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

Related

Android progress bar and service

Pretty standard task: have a service with long running task. And service has to update progress bar on main screen
There are two options:
Use broadcast intent to send progress and handle this in activity
Use handle with runnable to update ui right from Service code
Which approach looks "better" for you?
thanks
I would use an EventBus such as Otto (by Square) or EventBus (by greendroid), This is much less painful.
Hope this will help.

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.

How to share the progressbar's progress in two different activities

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();

Data exchange between a service and an activity

I have to publish the progress from a background service on the UI continuously on a progress bar. Any ideas on how to go about it. Intents won't work I guess coz they can only send the data once the activity is started. Ant other suggestions?
Update : The progress on the UI happens on a progress Bar
Extend Application, which is created once for entire application.
When Activity starts, store its reference to a field in your Application object. (Note that you can access Application using Activity.getApplication). Set this field to Activity reference or null in onPause/onResume calls.
Then in Service, you have also access to your Application by Service.getApplication. So look if your Activity reference is non-null, meaning that your Activity is shown to user, and update UI as needed in such case, by calling methods on your Activity.
Thanks for the help mice, but since I needed to update progress bars on different activities in my app depending on which one was visible, I found it easier to implement through Broadcast Intents and Recievers and Intent Filters. All I had to do was to Broadcast the progress in my service wrapped up in a bundle via a broadcast Intent (with a custom Intent Filter applied) and register (in onResume()) an inner subclass of BroadcastReciever in the activities which needed the progress (having the same intent filter). One can also unregister these recievers in the onPause() method of the activity to save memory headspace.

File Downloading in android with service in Background

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.

Categories

Resources