I am learning Android Service from http://developer.android.com/guide/components/bound-services.html, but after practicing by writing some sample code, I start to have following questions:
First, I would like to know how do we send delayed msg by Messenger class in Android.
Second, why don't we have methods like sendDelayedMsg() or sendMsgAt() in Messenger class ?
Thank you~
I'll answer your 2nd question first and your 1st question second, since that's probably the more logical way to explain this.
Messenger is a wrapper around a binder which is used for interprocess communication. As such, you don't have direct access to a Handler of a thread on the target process. So, you can't do something like Handler.postDelayed() or Handler.postAtTime() which you seem to be alluding to with your sendDelayedMsg() and sendMsgAt().
Now, as to your 1st question: You can implement a "send delayed msg" using Messenger as follows:
In your service class (which is run by your target process), create a Handler object for receiving messages from the Messenger. Your Handler object should extend the Handler class and in which you implement the "handleMessage(Message msg)".
Your "handleMessage(Message msg)" method receives messages from the Messengers. So, for each type of message (i.e., Message.what) that you want to delay, post it to your Handler object by calling postDelayed() or postAtTime(). In other words, when your target process/service receives a message that you want to delay, it schedules that a delay for that message.