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

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

Related

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.

I have two Activities: SearchActivity (search for items) and DownloadsActivity (show downloads). How do I show ProgressBar in DownloadsActivity?

I'm writing a simple downloader app which has two activities:
SearchActivity: shows a list of items you can download given a query. pressing an item should start downloading the item, but stay in the SearchActivity. Pressing an item currently runs an AsyncTask to handle the downloading logic
DownloadsActivity: shows a list of currently downloading items and ProgressBars (with percent) for each item.
I've set everything up except the ProgessBars. I'm wondering what is the best way to have the ProgressBars in the DownloadsActivity update correctly. Within the download AsyncTask run within the SearchActivity, how do I update the respective progressbar in DownloadsActivity if DownloadsActivity is being viewed?
You should likely use the Service component for the downloads purpose. This would allow you to run AsyncTasks (or just separate threads) from within the Service itself. Afterwards, the status of the particular download may be retrieved by any Activity by invoking some method (written by you) on the Service object which, in fact, is managing all the downloads. Oh, and in the same way you may want to start downloads.
BTW, to retrieve Service instance object from within Activity, you need to bind to the service first and then get its instance object using the IBinder interface. Further info on Services could be found here.

How can I manage a ProgressBar in the background?

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

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