[android-developers] Re: Service onCreate is asynchronous
You may want to try the Messaging Design Pattern (MDP) or the Command Design Pattern. MDP provides improvements in terms of coupling, encapsulation and reusability. Messaging Design Pattern (MDP) and pattern implementation - Published in the 17th conference on Pattern Languages of Programs (PLoP 2010). https://jt.dev.java.net/files/documents/5553/150311/designPatterns.pdf The Jt design pattern framework provides implementations for both of these patterns: Jt Design Pattern framework http://www.ibm.com/developerworks/webservices/library/ws-designpattern/index.html I hope this helps. On Nov 2, 3:21 pm, jotobjects wrote: > Further even the Service constructor is called asynchronously. > > It looks like all operations on a Local Service have to go through the > ServiceConnection, or at least operations on the service have to be > deferred until after onServiceConnected has been called. Is that > correct? > > Any other pointers about the correctdesignpatternto use for a > LocalService? > > On Nov 2, 11:54 am, jotobjects wrote: > > > I have tried both startService() and bindService(). Both methods > > return before Service.onCreate() is called. So work I was expecting > > to complete in Service.onCreate() is not done. > > > Is it necessary to do initialization steps in the Service > > constructor? If so what use is the Service.onCreate method? Any > > suggestions about how others handle this problem? > > > I found this thread which describes the problem, but the poster's > > solution is not described. > > >http://groups.google.com/group/android-developers/browse_thread/threa... -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Service onCreate is asynchronous
On Nov 3, 2:30 pm, Brion Emde wrote: > > This result might (I'm speculating) be because the onCreate() methods > > of the Activity and the Service are guaranteed to complete in their > > lifecycle. So maybe Android only lets one of them run at a time? > > This is very true. Since the Service runs in the UI thread, along with > the Activity, all that is really happening is that a message is being > sent to start the Service. It isn't actually started until you have > left the Activity and the UI thread gets around to processing the > messaging queue and starts the Service. > Thanks for clarifying that. Makes sense. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Service onCreate is asynchronous
On Nov 3, 2:12 pm, Frank Weiss wrote: > I can't help but think this is just a misunderstanding or failure to RTFM. > The method ref says "This defines a dependency between your application and > the service". Perhaps this is also a confusion about what an Android service > really is. Excuse me? What is your point? This doesn't seem to be a helpful comment IMHO -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Service onCreate is asynchronous
> This result might (I'm speculating) be because the onCreate() methods > of the Activity and the Service are guaranteed to complete in their > lifecycle. So maybe Android only lets one of them run at a time? This is very true. Since the Service runs in the UI thread, along with the Activity, all that is really happening is that a message is being sent to start the Service. It isn't actually started until you have left the Activity and the UI thread gets around to processing the messaging queue and starts the Service. On Nov 3, 4:56 pm, jotobjects wrote: > What I discovered was the locus of my problem is that if you call > bindService() from either Activity.onCreate() or Activity.onStart() > then bindService() will return "true" indicating the Service is bound > to the Activity BUT the Service will NOT have been constructed and the > Service onCreate() method will NOT have been called. > > This is a little non-intuitive since you would think that if the > Service is bound, then service would at least exist. But in fact it > might not even exist yet! > > This result might (I'm speculating) be because the onCreate() methods > of the Activity and the Service are guaranteed to complete in their > lifecycle. So maybe Android only lets one of them run at a time? > > On Nov 2, 4:57 pm, jotobjects wrote: > > > > > On Nov 2, 2:43 pm, Kostya Vasilyev wrote: > > > > Services are a mechanism for doing tasks that take a long time (among > > > another things). Based on this, the result of a service doing something > > > is typically available after a delay, so services are asynchronous by > > > their nature. > > > Right. Long running tasks have asynchronous results. That's why you > > put them in a Service (and run the actual tasks in a thread). > > > > If you have a piece of code that you wish to run synchronously, just > > > call that code directly (remembering to avoid code that can lead to ANRs). > > > > The only time I can see synchronous start / bind could be useful is > > > updating an Activity with the current state of some process managed by a > > > service. > > > Right, that's 101 as Frank said. I'm not trying to do anything > > synchronously. But what tripped me up was that I have new work for > > the Service to do from time to time based on other things going on in > > the application. There is no way to actually know when the Service > > exists. So you can't reliably communicate with the Service EXCEPT via > > Intents with startService (as Mark pointed out) or somehow with the > > ServiceConnection by deferring communication with the servie until > > after onServiceConnected(0 is called. > > > Some of the Local Service examples out there can lead one to believe > > that the service can be used directly and that's not true. > > > > But even this case should work pretty well, because at the time onCreate > > > is called (and presumable, that's where service start / bind is called), > > > the activity is not fully visible yet (since its content view is only > > > specified inside onCreate). > > > > -- Kostya > > > > 03.11.2010 0:30, jotobjects пишет: > > > > > On Nov 2, 1:33 pm, Frank Weiss wrote: > > > >> GUI Programming 101 > > > > >> All GUIs I've seen use an event queue. This is one of the biggest > > > >> prardigm > > > >> shifts to overcome for someone who is used to sequential, non-GUI > > > >> applications. > > > > The Service object by definition has nothing to with GUI programming, > > > > but you sort of have the right idea. > > > > -- > > > Kostya Vasilyev -- WiFi Manager + pretty widget > > > --http://kmansoft.wordpress.com- Hide quoted text - > > - Show quoted text - -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Service onCreate is asynchronous
I can't help but think this is just a misunderstanding or failure to RTFM. The method ref says "This defines a dependency between your application and the service". Perhaps this is also a confusion about what an Android service really is. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Service onCreate is asynchronous
Right. bindService returning "true" means that the call succeeded. The connection (at least the part exposed to the application) is made later. -- Kostya 03.11.2010 23:56, jotobjects пишет: What I discovered was the locus of my problem is that if you call bindService() from either Activity.onCreate() or Activity.onStart() then bindService() will return "true" indicating the Service is bound to the Activity BUT the Service will NOT have been constructed and the Service onCreate() method will NOT have been called. This is a little non-intuitive since you would think that if the Service is bound, then service would at least exist. But in fact it might not even exist yet! This result might (I'm speculating) be because the onCreate() methods of the Activity and the Service are guaranteed to complete in their lifecycle. So maybe Android only lets one of them run at a time? On Nov 2, 4:57 pm, jotobjects wrote: On Nov 2, 2:43 pm, Kostya Vasilyev wrote: Services are a mechanism for doing tasks that take a long time (among another things). Based on this, the result of a service doing something is typically available after a delay, so services are asynchronous by their nature. Right. Long running tasks have asynchronous results. That's why you put them in a Service (and run the actual tasks in a thread). If you have a piece of code that you wish to run synchronously, just call that code directly (remembering to avoid code that can lead to ANRs). The only time I can see synchronous start / bind could be useful is updating an Activity with the current state of some process managed by a service. Right, that's 101 as Frank said. I'm not trying to do anything synchronously. But what tripped me up was that I have new work for the Service to do from time to time based on other things going on in the application. There is no way to actually know when the Service exists. So you can't reliably communicate with the Service EXCEPT via Intents with startService (as Mark pointed out) or somehow with the ServiceConnection by deferring communication with the servie until after onServiceConnected(0 is called. Some of the Local Service examples out there can lead one to believe that the service can be used directly and that's not true. But even this case should work pretty well, because at the time onCreate is called (and presumable, that's where service start / bind is called), the activity is not fully visible yet (since its content view is only specified inside onCreate). -- Kostya 03.11.2010 0:30, jotobjects пишет: On Nov 2, 1:33 pm, Frank Weisswrote: GUI Programming 101 All GUIs I've seen use an event queue. This is one of the biggest prardigm shifts to overcome for someone who is used to sequential, non-GUI applications. The Service object by definition has nothing to with GUI programming, but you sort of have the right idea. -- Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com -- Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Service onCreate is asynchronous
What I discovered was the locus of my problem is that if you call bindService() from either Activity.onCreate() or Activity.onStart() then bindService() will return "true" indicating the Service is bound to the Activity BUT the Service will NOT have been constructed and the Service onCreate() method will NOT have been called. This is a little non-intuitive since you would think that if the Service is bound, then service would at least exist. But in fact it might not even exist yet! This result might (I'm speculating) be because the onCreate() methods of the Activity and the Service are guaranteed to complete in their lifecycle. So maybe Android only lets one of them run at a time? On Nov 2, 4:57 pm, jotobjects wrote: > On Nov 2, 2:43 pm, Kostya Vasilyev wrote: > > > Services are a mechanism for doing tasks that take a long time (among > > another things). Based on this, the result of a service doing something > > is typically available after a delay, so services are asynchronous by > > their nature. > > Right. Long running tasks have asynchronous results. That's why you > put them in a Service (and run the actual tasks in a thread). > > > > > If you have a piece of code that you wish to run synchronously, just > > call that code directly (remembering to avoid code that can lead to ANRs). > > > The only time I can see synchronous start / bind could be useful is > > updating an Activity with the current state of some process managed by a > > service. > > Right, that's 101 as Frank said. I'm not trying to do anything > synchronously. But what tripped me up was that I have new work for > the Service to do from time to time based on other things going on in > the application. There is no way to actually know when the Service > exists. So you can't reliably communicate with the Service EXCEPT via > Intents with startService (as Mark pointed out) or somehow with the > ServiceConnection by deferring communication with the servie until > after onServiceConnected(0 is called. > > Some of the Local Service examples out there can lead one to believe > that the service can be used directly and that's not true. > > > > > But even this case should work pretty well, because at the time onCreate > > is called (and presumable, that's where service start / bind is called), > > the activity is not fully visible yet (since its content view is only > > specified inside onCreate). > > > -- Kostya > > > 03.11.2010 0:30, jotobjects пишет: > > > > On Nov 2, 1:33 pm, Frank Weiss wrote: > > >> GUI Programming 101 > > > >> All GUIs I've seen use an event queue. This is one of the biggest > > >> prardigm > > >> shifts to overcome for someone who is used to sequential, non-GUI > > >> applications. > > > The Service object by definition has nothing to with GUI programming, > > > but you sort of have the right idea. > > > -- > > Kostya Vasilyev -- WiFi Manager + pretty widget > > --http://kmansoft.wordpress.com -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Service onCreate is asynchronous
On Nov 2, 2:43 pm, Kostya Vasilyev wrote: > Services are a mechanism for doing tasks that take a long time (among > another things). Based on this, the result of a service doing something > is typically available after a delay, so services are asynchronous by > their nature. Right. Long running tasks have asynchronous results. That's why you put them in a Service (and run the actual tasks in a thread). > > If you have a piece of code that you wish to run synchronously, just > call that code directly (remembering to avoid code that can lead to ANRs). > > The only time I can see synchronous start / bind could be useful is > updating an Activity with the current state of some process managed by a > service. Right, that's 101 as Frank said. I'm not trying to do anything synchronously. But what tripped me up was that I have new work for the Service to do from time to time based on other things going on in the application. There is no way to actually know when the Service exists. So you can't reliably communicate with the Service EXCEPT via Intents with startService (as Mark pointed out) or somehow with the ServiceConnection by deferring communication with the servie until after onServiceConnected(0 is called. Some of the Local Service examples out there can lead one to believe that the service can be used directly and that's not true. > > But even this case should work pretty well, because at the time onCreate > is called (and presumable, that's where service start / bind is called), > the activity is not fully visible yet (since its content view is only > specified inside onCreate). > > -- Kostya > > 03.11.2010 0:30, jotobjects пишет: > > > On Nov 2, 1:33 pm, Frank Weiss wrote: > >> GUI Programming 101 > > >> All GUIs I've seen use an event queue. This is one of the biggest prardigm > >> shifts to overcome for someone who is used to sequential, non-GUI > >> applications. > > The Service object by definition has nothing to with GUI programming, > > but you sort of have the right idea. > > -- > Kostya Vasilyev -- WiFi Manager + pretty widget > --http://kmansoft.wordpress.com -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
Re: [android-developers] Re: Service onCreate is asynchronous
Services are a mechanism for doing tasks that take a long time (among another things). Based on this, the result of a service doing something is typically available after a delay, so services are asynchronous by their nature. If you have a piece of code that you wish to run synchronously, just call that code directly (remembering to avoid code that can lead to ANRs). The only time I can see synchronous start / bind could be useful is updating an Activity with the current state of some process managed by a service. But even this case should work pretty well, because at the time onCreate is called (and presumable, that's where service start / bind is called), the activity is not fully visible yet (since its content view is only specified inside onCreate). -- Kostya 03.11.2010 0:30, jotobjects пишет: On Nov 2, 1:33 pm, Frank Weiss wrote: GUI Programming 101 All GUIs I've seen use an event queue. This is one of the biggest prardigm shifts to overcome for someone who is used to sequential, non-GUI applications. The Service object by definition has nothing to with GUI programming, but you sort of have the right idea. -- Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Service onCreate is asynchronous
On Nov 2, 1:33 pm, Frank Weiss wrote: > GUI Programming 101 > > All GUIs I've seen use an event queue. This is one of the biggest prardigm > shifts to overcome for someone who is used to sequential, non-GUI > applications. The Service object by definition has nothing to with GUI programming, but you sort of have the right idea. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: Service onCreate is asynchronous
Further even the Service constructor is called asynchronously. It looks like all operations on a Local Service have to go through the ServiceConnection, or at least operations on the service have to be deferred until after onServiceConnected has been called. Is that correct? Any other pointers about the correct design pattern to use for a LocalService? On Nov 2, 11:54 am, jotobjects wrote: > I have tried both startService() and bindService(). Both methods > return before Service.onCreate() is called. So work I was expecting > to complete in Service.onCreate() is not done. > > Is it necessary to do initialization steps in the Service > constructor? If so what use is the Service.onCreate method? Any > suggestions about how others handle this problem? > > I found this thread which describes the problem, but the poster's > solution is not described. > > http://groups.google.com/group/android-developers/browse_thread/threa... -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en