Android ApiDemos example resolution (39): App-& gt; Service-& gt; Local S

Source: Internet
Author: User

The Activity code of this example and the following Local Service Controller is defined in LocalServiceActivities. Java and implemented as the internal class of LocalServiceActivities. The called Service is LocalService.

LocalService can be either a "Started" Service or a "Bound" Service.

A "Bound" Service can be provided in Client/Service mode. It runs an application component (such as Activity) to bind with it, then receives the request and returns a response or provides an inter-process communication mechanism. its lifecycle is usually associated with the component that calls it (such as Activity) same. For LocalService, that is, "Started" Service, and "Bound" Service, if LocalService has been Started as "Started" Service, even if all clients are disconnected from its binding, localService will not stop either.

If a Service needs to be Bound to other components as a "Bound" Service, the onBind method must be implemented. This method returns an IBound object to the Client. The Client can call the Service method through this IBind object.

The Client can bindService to bind the "Bound" Service. The binding between the Service and the Client is implemented asynchronously. Therefore, the Client needs to monitor the connection with the Service through the ServiceConnection interface. When the Client calls bindService, bindService returns immediately. After the Android system establishes a connection between the Client and Service, it calls the onServiceConnection of ServiceConnection to notify the connection between the Client and Service to be established successfully, return the IBind object provided by the Service to the Client.

LocalService is only available to components of the same Application and does not provide inter-process communication interfaces. Therefore, you only need to derive a subclass of the Binder if the function calls the interface directly.

[Java]
// Class for clients to access. Because we know
// This service always runs in the same process
// Its clients, we don't need to deal with IPC.
Public class LocalBinder extends Binder {
LocalService getService (){
Return LocalService. this;
}
}
// Class for clients to access. Because we know
// This service always runs in the same process
// Its clients, we don't need to deal with IPC.
Public class LocalBinder extends Binder {
LocalService getService (){
Return LocalService. this;
}
}
LocalBinder only provides the getService method and returns the LocalService object itself.

The onBind method of LocalService is defined as follows:

[Java]
@ Override
Public IBinder onBind (Intent intent ){
Return mBinder;
}

// This is the object that calls es interactions from clients. See
// RemoteService for a more complete example.
Private final IBinder mBinder = new LocalBinder ();
@ Override
Public IBinder onBind (Intent intent ){
Return mBinder;
}
 
// This is the object that calls es interactions from clients. See
// RemoteService for a more complete example.
Private final IBinder mBinder = new LocalBinder ();
The returned value of onBind is mBinder, which is a LocalBinder class object. It defines a method getService.

Let's take a look at the implementation of LocalServiceActivities. Binding in the Client class:

[Java]
Private LocalService mBoundService;

Private ServiceConnection mConnection = new ServiceConnection (){
Public void onServiceConnected (ComponentName className, IBinder service ){
// This is called when the connection with the service has been
// Established, giving us the service object we can use
// Interact with the service. Because we have bound to a explicit
// Service that we know is running in our own process, we can
// Cast its IBinder to a concrete class and directly access it.
MBoundService = (LocalService. LocalBinder) service). getService ();

// Tell the user about this for our demo.
Toast. makeText (Binding. this, R. string. local_service_connected,
Toast. LENGTH_SHORT). show ();
}

Public void onServiceDisconnected (ComponentName className ){
// This is called when the connection with the service has been
// Unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we shoshould never
// See this happen.
MBoundService = null;
Toast. makeText (Binding. this, R. string. local_service_disconnected,
Toast. LENGTH_SHORT). show ();
}
};
Private LocalService mBoundService;
 
Private ServiceConnection mConnection = new ServiceConnection (){
Public void onServiceConnected (ComponentName className, IBinder service ){
// This is called when the connection with the service has been
// Established, giving us the service object we can use
// Interact with the service. Because we have bound to a explicit
// Service that we know is running in our own process, we can
// Cast its IBinder to a concrete class and directly access it.
MBoundService = (LocalService. LocalBinder) service). getService ();
 
// Tell the user about this for our demo.
Toast. makeText (Binding. this, R. string. local_service_connected,
Toast. LENGTH_SHORT). show ();
}
 
Public void onServiceDisconnected (ComponentName className ){
// This is called when the connection with the service has been
// Unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we shoshould never
// See this happen.
MBoundService = null;
Toast. makeText (Binding. this, R. string. local_service_disconnected,
Toast. LENGTH_SHORT). show ();
}
};
MConnection defines the ServiceConnection interface. onServiceConnected and onServiceDisconnected are called when a connection is established between the Service and the Client, and the connection is closed. The IBinder service is the response object of the onBind of the Service. Here, it is converted to LocalService or mBoundService by type.

By calling bindService, the Client establishes a binding relationship with the Service:

[Java]
BindService (new Intent (Binding. this,
LocalService. class), mConnection, Context. BIND_AUTO_CREATE );
BindService (new Intent (Binding. this,
LocalService. class), mConnection, Context. BIND_AUTO_CREATE );
To disconnect the binding from the Service, call unbindService.

[Java] www.2cto.com
UnbindService (mConnection );
UnbindService (mConnection); [java] view plaincopyprint?
<P> if you are familiar with Socket programming, the method for binding "Bound" Service to the Client is very similar to that for using Socket communication. </P> <a href = http://www.bkjia.com/uploads/allimg/131211/0542355I9-0.png "> </a> </p>
<P> if you are familiar with Socket programming, the method for binding "Bound" Service to the Client is very similar to that for using Socket communication. </P> <a href = http://www.bkjia.com/uploads/allimg/131211/0542355I9-0.png "> </a> </p>

Author: mapdigit
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.