开发者

Service runs tasks on an interval; should it be its own process?

开发者 https://www.devze.com 2023-03-21 22:46 出处:网络
I\'m creating a service that runs in the background. It does the following: Gathers the user\'s data (with permission)

I'm creating a service that runs in the background. It does the following:

  • Gathers the user's data (with permission)
  • Runs certain tasks every X minutes, and sends this data to a server every Y minutes
  • I'd like if other people could write their own UI, widgets and other cool stuff.

Currently, the service continues to run between task runs/network sends (without a wakelock).

The service listens for validation and runtime changes; this requires a separate process. There are ways around this but they would involve using IPC (which I don't t开发者_JAVA百科hink would cause a big performance hit).

Questions:

  • Should the service be allowed to die between tasks or should I let it run without a wakelock?
  • Is it more effective to remain alive than to open a database every minute or so?
  • Can people use my service if it's not in a separate process?


The question the arises, should my service be in it's own process?

No. By which I mean it should run in the same process as all your other components.

I think it'd be nice if other people could write their own UI, widgets and other cool stuff. It doesn't have to be in a separate process for this right?

No. It will automatically be in a separate process from the code from the "other people".

Right now the service stays running between task runs/network sends (not keeping a wakelock though) as I figure it will be more effective than opening a database/doing setup every minute or so.

Your users may disagree with this plan. Everlasting services are the reason why users attack developers with task killers and force-stops from the Settings app.

Should it be allowed to die between or should I let it run without a wakelock?

I recommend that you use an IntentService (since you need the background thread anyway for the network I/O) and let the service shut down in between polls. Also, please allow the user to control the values of X and Y from your opening paragraph.

Tasks run on an interval, should my service die in between (having to reopen the DB)?

Generally, yes. Opening the database takes a very small amount of time (e.g., handful of milliseconds), unless the flash storage is busy. That is a small price to pay to avoid complaints from users about your service running all of the time.

Can people use my service if it's not in a separate process?

Yes, so long as you are exposing some API (AIDL, documented set of Intents to send as commands via startService(), etc.).


It seems to me it might be worth dedicating an app purely to the service (i.e., no other components except a 'settings' Activity) and do everything through Intents.

As long as the manifest has all possible Intents registered using <intent-filter> blocks, anyone can communicate with it (your own apps as well as any 3rd party apps).

Also, you might want to use an IntentService which will process commands as they arrive and then shut itself down when finished.

Without fully understanding your requirements, i.e., what exactly the service is processing, it's difficult to advise further.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号