public class SimpleServiceConnection<S extends android.app.Service>
extends java.lang.Object
implements android.content.ServiceConnection
Modifier and Type | Class and Description |
---|---|
static interface |
SimpleServiceConnection.InstanceBinder<BS extends android.app.Service>
The binder provided by the service needs to implement this interface,
which provides us with a standard way to reach into the service's own
public methods.
|
static interface |
SimpleServiceConnection.Listener<LS extends android.app.Service>
Because connections can take a while, the object that is waiting for the
connection to be completed should implement this interface to receive a
signal when the connection is established.
|
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BINDING_FLAGS
By default, the connection will bind at a level above the consumer, and
auto-create the service if it has not yet been started.
|
protected boolean |
m_bBound
Indicates whether the connection is, indeed, bound to a service.
|
protected java.lang.Class<S> |
m_clsService
Stores a consistent hint to the service class to which this connection
will bind.
|
protected S |
m_srvInstance
The bound instance.
|
protected java.util.Vector<SimpleServiceConnection.Listener<S>> |
m_vListeners
A collection of objects listening to this connection.
|
Constructor and Description |
---|
SimpleServiceConnection(java.lang.Class<S> cls)
Initializes a simple connection for a specified service class.
|
Modifier and Type | Method and Description |
---|---|
SimpleServiceConnection<S> |
addListener(SimpleServiceConnection.Listener<S> l)
Registers a listener to this collection, if it is not already listening.
|
SimpleServiceConnection<S> |
connect(android.content.Context ctx)
Attempts to connect to the service, using the default binding control
flags.
|
SimpleServiceConnection<S> |
connect(android.content.Context ctx,
int bmFlags)
Attempts to connect to the service.
|
SimpleServiceConnection<S> |
disconnect(android.content.Context ctx)
Attempts to break the connection to the service.
|
protected java.util.Vector<SimpleServiceConnection.Listener<S>> |
getListeners()
Accesses the pool of listeners, verifying first that such a pool has been
initialized.
|
protected java.lang.String |
getLogTag()
For log messages generated by methods of this class, or any descendant
class, use this method, which examines the class of the instance from
which it is called, and returns that class's simple name.
|
java.lang.Class<S> |
getServiceClass()
Accessor for the service class hint.
|
S |
getServiceInstance()
Accessor for the service instance, if the connection is bound.
|
protected SimpleServiceConnection<S> |
initListeners()
Initializes the pool of connection listeners.
|
boolean |
isBound()
Indicates whether the connection is bound.
|
boolean |
isConnected()
Perhaps more useful than
isBound() , this function verifies not
only that the connection is bound, but also that the persistent reference
to the service is not null. |
<C extends android.app.Service> |
isServiceClass(java.lang.Class<C> cls)
Indicates whether the connection is indeed bound to an instance of the
specified class.
|
void |
onServiceConnected(android.content.ComponentName cn,
android.os.IBinder binder)
Called by the Android OS when a the service accepts the connection's
binding.
|
void |
onServiceDisconnected(android.content.ComponentName cn)
Called by the Android OS when a connection's binding to the service must
be broken.
|
SimpleServiceConnection<S> |
removeListener(SimpleServiceConnection.Listener<S> l)
Unregisters a listener to this collection, if it is found in the pool.
|
public static final int DEFAULT_BINDING_FLAGS
connect(android.content.Context, int)
methods in absence of any flag set
specified by the caller.protected java.lang.Class<S extends android.app.Service> m_clsService
protected S extends android.app.Service m_srvInstance
protected boolean m_bBound
protected java.util.Vector<SimpleServiceConnection.Listener<S extends android.app.Service>> m_vListeners
public SimpleServiceConnection(java.lang.Class<S> cls)
cls
- the service classprotected final java.lang.String getLogTag()
protected SimpleServiceConnection<S> initListeners()
protected java.util.Vector<SimpleServiceConnection.Listener<S>> getListeners()
public SimpleServiceConnection<S> addListener(SimpleServiceConnection.Listener<S> l)
l
- the listener to be addedpublic SimpleServiceConnection<S> removeListener(SimpleServiceConnection.Listener<S> l)
l
- the listener to be removedpublic java.lang.Class<S> getServiceClass()
public <C extends android.app.Service> boolean isServiceClass(java.lang.Class<C> cls)
cls
- the class to which we should compare our bound servicepublic boolean isBound()
public boolean isConnected()
isBound()
, this function verifies not
only that the connection is bound, but also that the persistent reference
to the service is not null.public S getServiceInstance()
public SimpleServiceConnection<S> connect(android.content.Context ctx, int bmFlags) throws java.lang.IllegalArgumentException
SimpleServiceConnection.Listener.onServiceConnected(net.zer0bandwidth.android.lib.services.SimpleServiceConnection<LS>)
.ctx
- the context in which to bind to the servicebmFlags
- a mask of optional binding flags; see Context
java.lang.IllegalArgumentException
- if the context is nullpublic SimpleServiceConnection<S> connect(android.content.Context ctx)
SimpleServiceConnection.Listener.onServiceConnected(net.zer0bandwidth.android.lib.services.SimpleServiceConnection<LS>)
.ctx
- the context in which to bind to the serviceDEFAULT_BINDING_FLAGS
public SimpleServiceConnection<S> disconnect(android.content.Context ctx)
Activity.onStop()
) should call this method to free the
service binding. The caller should not assume that the binding was
successfully released; any other consequences of releasing this binding
should be handled by implementing SimpleServiceConnection.Listener.onServiceDisconnected(net.zer0bandwidth.android.lib.services.SimpleServiceConnection<LS>)
.ctx
- the context in which the binding should be brokenpublic void onServiceConnected(android.content.ComponentName cn, android.os.IBinder binder)
onServiceConnected
in interface android.content.ServiceConnection
cn
- the name of the service componentbinder
- a binder to the servicepublic void onServiceDisconnected(android.content.ComponentName cn)
onServiceDisconnected
in interface android.content.ServiceConnection
cn
- the name of the service component