1 write a class to inherit the service override Onbind method to return a IBinder object (to be passed to a successful connection)
2 The service writes an inner class that inherits IBinder and implements an interface (for extracting methods) that inherits IBinder is an object that can return the inner class in 1 steps, and an object of the inner class can invoke other methods in the service. The method that invokes the service in the method that implements the interface.
3 Write a class to implement the Serviceconnection service connection, overriding the connection success and connection failure method, the connection succeeds in passing in a IBinder object, that is, the above internal class object, so that through this object to invoke the method in the service. The interface is implemented only to restrict the invocation of the related methods in the Access service. To cast an incoming object into an interface object
Defines an interface that defines a method that is implemented by invoking the method specified in the service.
public interface getservice{
void GetService ();
}
define an inner class in the inheritance service to inherit the binder and implement the interface. An inner class can invoke a method in a service, so a method in an interface is implemented using an inner class, and the method in the service is invoked by this method.
public class MyService extends service{
Public IBinder Onbind (Intent Intent) {
Returns a IBinder object that is used to invoke the method in the service when the connection succeeds
return new Midman ();
}
Inner class inherits binder implementation interface
Class Midman extends Binder implements getservice{
public void GetService () {
Doservices ();
}
}
Define and implement the methods in the service we want to use
public void Doservices () {
System.out.println ("The Service method!");
}
Other methods in the service, not to use
public void Dootherservices () {
System.out.println ("inner use!");
}
private Intent int;
private Myserviceconn Conn
getservice gs;
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedInstanceState);
Setcontentview (R.layout.activity_main);
< Span style= "color: #008000;" >< Span style= "color: #008000;" > int = new Intent (this,myservice.class);
conn = new Myserviceconn ();
< Span style= "color: #008000;" >< Span style= "color: #008000;" > Bindservice (int,conn,bind_auto_create);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
< Span style= "color: #008000;" >< Span style= "color: #008000;" > Class Myserviceconn implements serviceconnection{
public void onserviceconnected (Component name,ibinder service) {
The service is the object returned by the above Onbind (), and the strong turn is to allow only the methods in the interface
GS = (GetService) service;
}
public void onservicedisconnected (Component name) {
}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
//uses the method specified in the service
public void Click (View view) {
gs. GetServices ();//getservices () Call the doservices we want ();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}
&NBSP;&NBSP;&NBSP;&NBSP
Methods in the service in Android are extracted as interfaces