1

I studied both, and IntentService is really simple, while service is somewhat confusing for me.. Ive seen a lot of question here and im leaning in IntentService, but im afraid that in some point of my work, IntentService isn't the one to go in the first place, .. here's what my game will do..Somewhat similar in Farmville

  1. Create New Game, with In-game Date is per Week, Starting at Week 1.. Data is store in SQLiteDB
  2. Start IntentService, IntentService will do this thing. After every task, Notify the user via NotificationManager

    • update the week every 8 hours. (1 week = 8 hours, using Alarm manager) when Week is updated, i.e Week 1 to Week 2, IntentService will check what "Plant is ready to harvest"
    • IntentService also do randomizing the weather every week
    • Check and Update Animals/Plants Condition
    • Do more Random Things
    • Stop intentService
    • Run the Intentservice again after 8 hours.

That's what my game should do, is IntentService the way to go, or Service? can IntentService can handle this? i really wanted the Service, but in my understanding of what i read in articles and here, it should be only use for "multiple process at the sametime" which my game can afford not to use that feature..

Also,can i make another IntentService? this service must run every 5mins or so.. if not, thats ok.. thanks

mcr619619
  • 435
  • 1
  • 4
  • 13

2 Answers2

2

An IntentService runs in the background on a seperate Thread so anything done in the service will not effect the UI. BUT once the service finishes doing what it is suppose to, the service terminates itself.

A service also does stuff in the background BUT on the UI thread so if you have any time consuming things that need to be done it will hold up the UI. this also runs until you tell it to stop for the most part.

If you are using alarms to trigger everything I would say the IntentService is fine.

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • is IntentService Ok for updating the database? the intentService will probably contain about 500lines, depending on the if-else of the code. – mcr619619 Nov 03 '12 at 03:11
1

I would recommend using IntentService + AlarmManager pair in your case.

IntentService creates a queue and background thread to handle jobs so you can be assured that everything runs one after another. It also finishes and shuts down after the end of your 500 lines code, it can do database updates as well.

Service is more for on going work in the background like streaming a music online etc. You start the service, shutdown the service manually. For IntentService its automatic.

Rejinderi
  • 11,694
  • 2
  • 31
  • 40
  • Thanks!.. though your answer is simple, it really helpful more than you think..thanks.. anyway, how to close a Question? – mcr619619 Nov 03 '12 at 07:33
  • 1
    Np glad it helped. Delete a question u mean? If not you can always accept a answer or just leave it. – Rejinderi Nov 03 '12 at 07:39
  • Ok thanks... i mean to close it..well, ill just leave it, thanks again – mcr619619 Nov 03 '12 at 08:11
  • @Rejinderi...I used IntentService + AlarmManager in my app and i am calling String sql = "VACUUM;" ;databaseInstance.execSQL(sql); databaseInstance.setTransactionSuccessful(); //// But my app size get increase by self and again it get decrease by it-self. What is happing there? Do you have any idea? – DJhon May 30 '14 at 07:21
  • @Rejinderi can you help me out with this one http://stackoverflow.com/questions/29047144/background-process-to-scan-the-location-of-the-user-at-regular-intervals-and-upd – Sagar Devanga Mar 16 '15 at 07:16
  • @SagarDevanga Sorry. I wasn't on stack overflow for the past few days. – Rejinderi Mar 27 '15 at 09:48