1

Ok guys, I did ask before on how to create a way to run a code always in my app. It's similar to how say iMobeter or something like that check your hp, if it's less than max it'll add to it every 5 mins.

Some people told me to use service which sounded good. Problem is, I need a service that is always running in the background. Plus, I could not create a service that can access the database. I cant use the cursor there as in it does not support the method.

I would appreciate pointing me to a good example or a better way to do it.

Hok
  • 51
  • 1
  • 9

1 Answers1

3

Problem is, I need a service that is always running in the background.

That is not possible. Users can stop your service whenever they want, and Android will stop your service if it lives too long.

Plus, I could not create a service that can access the database. I cant use the cursor there as in it does not support the method.

Yes, it does. Use SQLiteOpenHelper, the same way you would with an activity, content provider, etc.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thx for the answer, about the first issue, how does a game like mobster or the like calculate the time to add to the hp for example? (my game will have no server side though)... about the second one, I can open the data base and run queries from there, but I can't use the startManagingCursor(cursor) method... – Hok Aug 15 '11 at 14:05
  • "how does a game like mobster or the like calculate the time to add to the hp for example" -- I have absolutely no idea what you are talking about. "but I can't use the startManagingCursor(cursor) method" -- correct, so do not use it. Managed cursors are unique to activities. Simply `close()` your `Cursor` when you are done with it. – CommonsWare Aug 15 '11 at 14:10
  • ooh, nice... about the first issue.. lets say a game like farmvile, where you create a house and it needs 2hrs to finish. wither you turn ur phone off, the app off or the service, after 2hrs it'll finish. hope that was clear? – Hok Aug 15 '11 at 14:15
  • @Hok: Use `AlarmManager` and a `_WAKEUP`-style alarm. – CommonsWare Aug 15 '11 at 15:06
  • I thought of another idea of storing the current time onDestroy of the service then make some calculations when it restarts... but I guess your idea also works thank you for the answers :) – Hok Aug 15 '11 at 17:21