I have an app that has an initial activty that list some files within a list view. When an item is clicked within the list it takes you to a detail activity of that specific file.
In the detail view I have a button called download, when you click on download it starts a IntentService which sets off the file to be downloaded as such:
downloadButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(AppDetailsActivity.this, AppDownloadService.class);
intent.putExtra(Consts.APP_DOWNLOAD_RECEIVER_KEY, new DownloadReceiver(new Handler()));
startService(intent);
}
});
the DownloadReceiver class is used to update a progress bar within the the download file's detailed activity.
Now what I want to do...
If the user is currently downloading the file from the detail activity, and then goes back to the previous activity, I want the activity to somehow subscribe/bind to the same IntentService and retrieve the results so that it can show a progress bar within the list item of the file that is downloading.
To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.
Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
Service will always run on the main thread. intent service always runs on the worker thread triggered from the main thread. There is a chance of blocking the main thread. tasks will be performed on a queue basis i.e, first come first serve basis.
Work is submitted to the queue by creating an Intent and then passing that Intent to the StartService method. It is not possible to stop or interrupt the OnHandleIntent method IntentService while it is working.
For a download you sure can use an IntentService
. It runs on a different thread. Also, keep in mind that calling startService()
several times for that IntentService
it will create a queue and will handle one intent at a time.
To show the progress on an Activity
I don't belive broadcasts are the right thing. Although it is highly likely they are process in order, the framework or Android itself won't guarantee an order upon delivery. This means that you can get the broadcast for 50% and then the broadcast for 40%. For this I recommend that you take a look on this example: http://developer.android.com/reference/android/app/Service.html#LocalServiceSample
It can look a little bit more complicated but you'll have a 2 way communication that you can rely on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With