global indication of activity in android - 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.

Related

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

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

One loader for all activities

Im building an Android application. It parses a feed which is stored in a DB. Each activity of the app is able to get this data from the DB.
Every activity can also call the Service, and make it refresh the data. When this is done I would like to display a loader. While the Service is downloading, the user is still free to navigate between activities. My question is; how can I display a loader that runs in all activities the user navigates to?
Thanks a lot :)
Here's what I would try to do since it seems to be an uncommon task to me:
I would try to setup a translucent PopupWindow which contains a progress indicator. I would trigger / dismiss this popup to be displayed / or not whenever there's a need to indicate the loading progress...
You could try something like:
Place a "global loader view" in all your activities.
When you start/return to an activity, fire an AsyncTask which will be used to handle updates of the global loader
Override onPreExecute() to prepare the global loader (e.g. setting its state to the current level if the service has been downloading for a while)
Override onProgressUpdate() and use it to update the state of the global loader by asking the Service for the current state (load percentage or something)
In doInBackground you implement:
while( service.isLoading() ) {
publishProgress( service.getLoadPercentage() )
// Maybe add a Thread.sleep() here.
}
Override onPostExecute to set the visibility of the global loader to View.GONE.
You might need to implement a way of killing the AsyncTask if the user switches away from the activity when the loading is not finished yet.

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.

Loading an Alert Box from a Thread when the activity has gone away

I have a comment activity that loads a Thread and sends some data to a server; the activity is immediately finished once the submit button is pressed.
The user is then free to do other things in my application.
When the server responds an AlertDialog is shown.
The problem is that since the initial context has been destroyed, my application crashes.
I tried getApplicationContext() but still get an exception.
Put your network stuff in a Service, then show a status bar notification instead of a dialog.
Take a look at AsyncTask
From JavaDocs:
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

Categories

Resources