Feb 11 | 8:55 AM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Feb 11 | 9:00 AM |
Yaniv S. | has entered the room |
Mark M. |
hello, Yaniv!
|
Mark M. |
how can I help you today?
|
Yaniv S. |
Hi Mark
|
Yaniv S. |
my problem is this
|
Yaniv S. |
I have a service that runs on BOOT COMPLETED
|
Yaniv S. |
and basically never destroyed
|
Yaniv S. |
it listens for messages
|
Yaniv S. |
what I want is for other applications to be able to use this service like a server
|
Yaniv S. |
and bind to it, so when a message is received it is sent to other apps that bind to it
|
Yaniv S. |
I coudn't find how to do that
|
Yaniv S. |
how to send message from a service to other apps
|
Yaniv S. |
every guide shows how to send a message from the client to the service
|
Mark M. |
not every guide
|
Mark M. |
for example, you are a subscriber to my book
|
Mark M. |
and my book certainly covers other options
|
Mark M. |
particularly in the "Remote Services and the Binding Pattern" chapter
|
Mark M. |
starting on page 2471 in the PDF edition of Version 6.4 of the book
|
Yaniv S. |
is there an example on how to do that?
|
Mark M. |
well, first we need to determine what the right answer is for "that"
|
Feb 11 | 9:05 AM |
Mark M. |
you wrote: "so when a message is received it is sent to other apps that bind to it"
|
Feb 11 | 9:05 AM |
Yaniv S. |
I was thinking , 5 min ago , that maybe creating custom broadcast receiver is also an option
|
Mark M. |
that is definitely an option
|
Yaniv S. |
and my service will broadcast the message when it gets it
|
Yaniv S. |
and other apps will pick it up,
|
Mark M. |
as would be publishing a ContentProvider and having the clients register a ContentObserver to find out about changes
|
Mark M. |
so, let me ask a couple of questions:
|
Yaniv S. |
yes but content provider means i need to obtain the messages in some sort of database
|
Yaniv S. |
and i dont want that
|
Mark M. |
1. if a message arrives, what client apps need to find out about it? all of them regardless of whether they are running? or only those currently running?
|
Yaniv S. |
i dont want to save the messages if no other app listens for it
|
Yaniv S. |
umm.. not sure I understand
|
Mark M. |
OK
|
Mark M. |
let's suppose that your app is installed on a phone
|
Mark M. |
and a message comes in
|
Yaniv S. |
ok
|
Mark M. |
there are 2,137 other applications, also installed on this phone, all of which *could* be interested in your message
|
Mark M. |
however, only 6 of them are running right now
|
Mark M. |
when the message comes in, how many apps should receive it: 6? 2,137? or something else?
|
Yaniv S. |
which ever app is listening for this particular message
|
Yaniv S. |
for this particular service
|
Feb 11 | 9:10 AM |
Yaniv S. |
If im creating my custom broadcast
|
Yaniv S. |
only apps that listen for this specific broadcast will receive the msg , no
|
Yaniv S. |
?
|
Mark M. |
I am trying to understand how all of this is supposed to work at a logical level
|
Mark M. |
are all of these apps (the one with the service and the ones listening for messages) yours?
|
Yaniv S. |
yes
|
Mark M. |
ah, OK, that helps
|
Mark M. |
next: are you building your own hardware?
|
Yaniv S. |
yes sorry for not being clear
|
Mark M. |
or your own ROM mod?
|
Yaniv S. |
no , I have an android based device that Im working on
|
Mark M. |
and these apps will be pre-installed on that device?
|
Mark M. |
or are these apps being publicly distributed, such as through the Play Store?
|
Yaniv S. |
they will be pre installed , sort of
|
Feb 11 | 9:15 AM |
Yaniv S. |
meaning that each client will have his own device with his own applications installed on the device
|
Mark M. |
can the client uninstall these apps? in particular, can the user remove the app that contains this message-collecting service?
|
Yaniv S. |
there wont be play store on the devices
|
Yaniv S. |
well , right now it can be deleted but the goal is that the message collecting service will stay installed and running and could not be removed
|
Mark M. |
OK
|
Mark M. |
in that case, having the central message collecting service is fine
|
Yaniv S. |
lets say that this is a service that it's in the client's interest to delete
|
Mark M. |
you mean, not to delete, right?
|
Yaniv S. |
no i mean to delete .
|
Yaniv S. |
we don't want clients to be able to delete that service is my point
|
Mark M. |
OK, that is what I thought that you meant
|
Mark M. |
next: how big are these messages? is a message something small (such as a bit of JSON) or something large (such as a photo)?
|
Yaniv S. |
so , which options is better, a bound service or a broadcast
|
Mark M. |
we are getting to that -- I need some more design information before I can give you a recommendation
|
Yaniv S. |
the message is small , its basically a bundle of 2 int's and a string
|
Mark M. |
OK, so that is something that most IPC can handle
|
Feb 11 | 9:20 AM |
Mark M. |
now, let's go back to a point I was trying to uncover earlier...
|
Mark M. |
let's call your app with the message-collecting service S
|
Mark M. |
so, S is installed on the device
|
Mark M. |
and let's say that there are three other apps that are supposed to get messages from S, called A, B, and C
|
Mark M. |
when S gets a message that A needs, if A is not running (has no process), what is supposed to happen?
|
Mark M. |
does A need to be started up and handed the message?
|
Mark M. |
does the message get queued somewhere for A to pick up later?
|
Mark M. |
or is the message lost?
|
Yaniv S. |
no, the message is lost and i dont care about it
|
Yaniv S. |
only when A is running and requesting messages will the messages be received
|
Mark M. |
in that case, a broadcast is probably not what you want
|
Mark M. |
as the broadcast would cause A to start running if it was not already running
|
Yaniv S. |
oh
|
Mark M. |
and, since you do not need message persistence, the ContentProvider/ContentObserver approach (that you did not like) is not needed
|
Mark M. |
and so, your original plan of the service binding is probably the right one
|
Mark M. |
in the chapter on "Remote Services and the Binding Pattern", I have a sample showing how to pass data from the service back to the client
|
Yaniv S. |
but is the client a different app?
|
Mark M. |
in the sample, the client is asking the service to download a file, and the service sends back a success/failure indication
|
Mark M. |
yes, they are separate apps
|
Mark M. | |
Feb 11 | 9:25 AM |
Mark M. |
for binding between apps, you need to use AIDL to describe the interface
|
Mark M. |
normally, we think of this as being the service having a Binder based on that AIDL that is the implementation of the interface
|
Mark M. |
and the client has a proxy that allows it to invoke methods on that interface, as if the service were a local object, when in reality is it in another process
|
Mark M. |
however, given that you already have an established client->service binding, you can also use AIDL to send data from service to client
|
Mark M. |
in this case, the *client* has the Binder, and the *service* gets the proxy
|
Mark M. |
and that's what I cover in the book
|
Yaniv S. |
so it sounds like exactly what I need.
|
Mark M. |
particularly, you will want to read the "Servicing the Service" section, starting on page 2486 of the Version 6.4 PDF
|
Yaniv S. |
are you sure thats the page num?
|
Yaniv S. |
it jumps to JVM scripting
|
Mark M. |
it is the page number as seen at the bottom of the page
|
Yaniv S. |
oh ok
|
Mark M. |
if your PDF viewer ignores my page numbering, try page 2535
|
Yaniv S. |
page 2486 is "Traceview"
|
Feb 11 | 9:30 AM |
Mark M. |
are you looking at Version 6.4?
|
Yaniv S. |
oh no! I have 6.2
|
Yaniv S. |
oops
|
Yaniv S. |
im not updated :(
|
Mark M. |
the material is in there too, though I rewrote that chapter relatively recently
|
Mark M. |
you're obviously a subscriber, so download Version 6.4 from the Warescription site when you get a chance
|
Yaniv S. |
ok so just to be clear , I need to read Servicing the service and Remote Services and the Binding Pattern.
|
Mark M. |
yes, where "Servicing the Service" is a section in the "Remote Services and the Binding Pattern" chapter
|
Yaniv S. |
ok Mark, thank you for your help
|
Mark M. |
you are very welcome
|
Yaniv S. |
(Im staying here just because I want the transcript in front of me)
|
Feb 11 | 9:35 AM |
Mark M. |
also note that that chat transcript will be uploaded to http://commonsware.com/office-hours/ shortly after the chat ends
|
Feb 11 | 9:35 AM |
Yaniv S. |
thanks
|
Mark M. |
and, if you have other questions, go right ahead -- it is a quiet chat room today
|
Yaniv S. |
thats it for now , got some reading to do :)
|
Mark M. |
OK
|
Feb 11 | 10:00 AM |
Mark M. |
that's a wrap for today's chat -- the next chat is tomorrow at 4pm US Eastern
|
Mark M. |
have a pleasant day!
|
Yaniv S. | has left the room |
Mark M. | turned off guest access |