I'm developing an activity that binds to a local service (in onCreate of the activity):
bindService(new Intent(this, CommandService.class), svcConn, BIND_AUTO_CREATE);
I would like to be able开发者_StackOverflow中文版 to call methods through the IBinder in my lifecycle methods, but can not be sure that onServiceConnected have been called prior to these. I'm thinking of handling this by adding a queue of sorts in the ServiceConnection implementation, so that the method calls (Command pattern) will be executed once the connection is established.
My questions are then:
- Is this stupid, any better ways? :)
- Are there any specification for which thread will be used to execute the ServiceConnection callbacks? More to the point, do I need to worry about synchronizing a queue datastructure?
Edit - something like:
public void onServiceConnected(ComponentName name, IBinder service) {
dispatchService = (DispatchAsync)service;
for(ExecutionTask task : queue){
dispatchService.execute(task.getCommand(), task);
}
}
Are there any specification for which thread will be used to execute the ServiceConnection callbacks?
It should be called on the main application thread, like any other callback.
More to the point, do I need to worry about synchronizing a queue datastructure?
That depends on where you are adding objects to the queue. If it is only from the main application thread, then there should be no contention, AFAIK.
精彩评论