Services between two processes need to communicate, and objects must be serialized and sent to each other.
Android provides an aidl
(Android Interface Definition Language) tool to process serialization and communication. In this case, the Service must provide the service interface as an aidl file. The aidl tool will generate a corresponding
Java interface, and the generated Service Interface contains a function called stub service pile class. The service implementation class must inherit this
Stub service piles. The onbind method of the Service will return the object of the implementation class, and then you can use it. Now we can implement a helloworld-level service and
Use this service in activity. The procedure is as follows:
1. Compile the aidl file, which provides the method interface provided by the Service. The file extension is aidl. In our example, name it
The idatatimeservice. aidl file is as follows:
Package Android. IPC;
// Declare the interface.
Interface idatetimeservice {
String getcurrentdatatime ();
}
2. Compile the service implementation class. In this example, It is datatimeservice. java. The content is as follows:
Package Android. IPC;
Import Android. content. intent;
Import Android. App. Service;
Import java. util. date;
Import Android. OS. ibinder;
Public class datetimeservice extends Service {
Public static final string broadcast_action = "com. Services. demo2.datatimeserviceevent ";
/**
* Use the String constant defined above to create an intent instance. If events that occur during the service running, you can use
* The sendbroadcast (broadcast) method sends a broadcast message to notify the activity, and then the receiver registered with the activity processes the message;
*/
Private intent broadcast = new intent (broadcast_action );
Private Final idatetimeservice. Stub binder = new idatetimeservice. Stub (){
Public String getcurrentdatatime (){
Return (getcurrentdatatimeimpl ());
}
};
Synchronized private string getcurrentdatatimeimpl (){
Date = new date ();
Return (date. tolocalestring ());
}
@ Override
Public ibinder onbind (intent ){
Sendbroadcast (broadcast );
Return (binder );
}
}
3. Write an activity to consume the service:
Package Android. IPC;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. content. intent;
Import Android. WebKit. webview;
Import Android. content. serviceconnection;
Import Android. content. componentname;
Import Android. OS. ibinder;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intentfilter;
Public class IPC extends activity {
Private idatetimeservice service = NULL;
Private intent serviceintent = NULL;
Private webview browser;
Private serviceconnection svcconn = new serviceconnection (){
Public void onserviceconnected (componentname classname, ibinder binder ){
Service = idatetimeservice. stub. asinterface (binder );
Browser. postdelayed (New runnable (){
Public void run (){
Updateforecast ();
}
},1000 );
}
Public void onservicedisconnected (componentname classname ){
Service = NULL;
}
};
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Browser = (webview) findviewbyid (R. Id. webview );
Serviceintent = new intent (this, datetimeservice. Class );
// Bind a service
Bindservice (serviceintent, svcconn, bind_auto_create );
}
@ Override
Public void onresume (){
Super. onresume ();
/**
* Register the receiver of the broadcast message. In Android, the Service communicates with the activity by sending the message through broadcast, and then the receiver registered by the activity
* Receives the message and processes it accordingly. Here, datatimeserviceimpl. broadcast_action is used for filtering, indicating that it only receives
* Broadcast messages sent by calling sendbroadcast (broadcast) in datatimeservice;
*/
Registerreceiver (receiver, new intentfilter (
Datetimeservice. broadcast_action ));
}
@ Override
Public void onpause (){
Super. onpause ();
Unregisterreceiver (receiver );
}
@ Override
Public void ondestroy (){
Super. ondestroy ();
Unbindservice (svcconn );
}
Private void updateforecast (){
Try {
String page = service. getcurrentdatatime ();
If (page! = NULL ){
Browser. loaddatawithbaseurl (null, page, "text/html", "UTF -",
Null );
Browser. postdelayed (New runnable (){
Public void run (){
Updateforecast ();
}
},1000 );
}
} Catch (final throwable t ){
Svcconn. onservicedisconnected (null );
}
}
// Recipient instance
Private broadcastreceiver receiver ER = new broadcastreceiver (){
Public void onreceive (context, intent ){
Runonuithread (New runnable (){
Public void run (){
Updateforecast ();
}
});
}
};
}
Reference: http://alex-yang-xiansoftware-com.javaeye.com/blog/607109