开发者

android : trigger method of a running service to act

I have a service that keeps running, and a Timer in there to execute a certain function every X minutes.

From an activity, I'd like this function to be called immediately when something happens, so we don't need to wait untill the function is called by the Timer. Is that possible?

If you like a less 'general' question:

  • I have a database with queued messages (queuing is done i开发者_运维技巧n case no network is available).
  • Every X minutes, my service checks if network is available, and flushes the queue to the server
  • Now, what I'd like to achieve is that I can trigger this "flushing" of the queue as soon as I've added an item to it (from within an activity).

Is that possible?


One approach could be to use a Messenger. When a FLUSH_QUEUE message comes in, handle it by flushing the queue immediately.

  • The service can create an AsyncTask or thread that sends messages based on a timer, so every X minutes, a message is sent and handled, and the queue is flushed
  • An activity can bind to the service, and send a message whenever it wants, flushing the queue on demand
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜