Running Two Threads in an Android Service
I would like to create a service that when started, initializes a connection to google spreadsheets and then periodically sends data to the spreadsheet from a database.
Is it possible to create two threads: One that connects to google spreadsheets and a second that check for changes in the database and updates the spreadsheet.
I was thinking of running the first thread (connecting to google spreadsheets) from the onCreate and the second from the onStartCommand. Is this bad practice? Does anyone have any feedback about how I can do this?
开发者_StackOverflowThanks, and please let me know if anything I have said is unclear.
Why do you need to run two threads? Your work seems to be sequential: first get data from the database, then post it to a spreadsheet. You might save a second or two if you are already authenticated to Google Docs by the time you get the database data, but if this needs to run in a background service, it won't be visible. And since it seems this only needs to run from time to time, you should schedule your service to start at certain intervals using AlarmManager
, and let it shut itself down once it's done it's work. Consider using an IntentService
for this, it will start a background thread to do work and shut down automatically.
精彩评论