Communication with a local service

The app I'm building uses local Service

to download files from the internet to the phone's SD card. In this application, users can view lists of books and read them online. The user can also download a pdf

copy of the book for offline viewing.

I use locally linked to handle downloads Service

. I don't want this to Service

run all the time, only when uploading files. So it Service

can close when its tasks are completed, I do not bind to the service, instead I send the command "enqueue for download" via Intent

passed to Context.startService

.

The books available for download are shown in the list. The user can choose to download the book by clicking its row in the list. When loading, I need to show the progress of the download using ProgressBar

in the line of the list of real books. I need to also show in lines if a workbook is loaded into the queue, or if its download has completed or failed. Books can appear in different actions throughout the application - for example, in a search or in a list of favorites. When books are displayed in different places, they are not the same objects, but they are uniquely identified by them bookId

.

Since I don't want to bind to the service from each Activity

, my preliminary plan was to use public static final HashMap

in the class itself Service

to contain the mapping bookId

for loading the state, a enum

from the queue, loading, canceling, etc. Each book view when rendered will check this static HashMap, and if the bookId is on the map, get and display its status. I don't really like this idea, but at the moment this is the only way I can get the status from the Service without having to contact it and launch it.

Also, I need to get the download completion percentage from Service

for a given bookId

one if it is an active download. Again, I'd rather not bind to the service from every action, so I'm not sure how to do the search for the current progress from Service

. My current plan is to use some kind of single user broker that Service

will trigger updates and the views can be read. But I am not very happy with this idea.

The reason I would like to avoid linking to Service

from every activity is 1.) I am already doing the others Service

and 2.) the linking is verbose and I wish you didn't have to go around the link to Service

(but admittedly this not too much of a problem).

Perhaps binding to local is Service

n't expensive enough to warrant this different setup? Should I not worry about binding to it from each one Activity

? Maybe this is not a problem?

+2


a source to share


1 answer


Reason I want to avoid binding to a Service from every activity 1.) I am already starting another service

Then consider combining the two.

2.) The linking is verbose and I would like a link to the Service



Each action must have its own connection to the service.

Perhaps binding to a local service isn't expensive enough to warrant this is a different setup?

The act of binding to the local service is cheap.

+2


a source







All Articles