Reputation: 11
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.
Upvotes: 1
Views: 4219
Reputation: 80330
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.
Upvotes: 2
Reputation: 5593
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.
Upvotes: 1