views:

85

answers:

2

I'm trying to figure out how to launch a Service from a singleton class I've created.

According to the Android documentation, startService is a method of Context. So, in the method call I am making to the singleton class, I figured I would pass on the application Context as a parameter. Easy enough. However, when I try to use that context.startService, it says startService is not a method of Context. So how do I actually launch a Service from my own class?

API 7

Why I'm doing this:

Despite the examples that have been provided with the Android SDK (where database calls are made directly from the UI), Virgil, in his 2010 Google I/O presentation suggested a few models for REST-heavy applications that behave differently. The one I'm attempting to follow is as such (as I understand it):

Activity calls "Service Helper" (singleton), Service Helper launches Service, Service queries database for Cursor, Service also launches thread which calls web service to update database, Service notifies Service Helper that is has completed querying for Cursor, Service Helper notifies Activity that it has Cursor.

A: 

forehead smack

I see my problem:

import sun.org.mozilla.javascript.internal.Context;
Andrew
A: 

I'm trying to implement the exact same architecture that Andrew mentioned above. In my code, I have the ServiceHelper starting the service which of course implements the onServiceConnected() and onServiceDisconnected().

Now, I have several activities that use the Service Helper singleton to call the service methods. The problem I'm running into now is that there are cases where the activity calls the ServiceHelper methods too soon (i.e. before ServiceHelper is connected to the service) and I get a NullPointerException.

One of the ways I solved this problem was by adding a callback when getting the ServiceHelper singleton. This way from my activity, I only call the methods after I get the callback.

Is there a better way to get around this?

Marty