A simple demo to learn Android remote service (use of aidl)

Source: Internet
Author: User
Tags call back

Original article address

Http://miloisbadboy.com/archives/109

This is a post that Milo wrote on the Forum a long time ago. Now I have sorted it out and Milo will review it.
Generally, four components of Android run in the same process, but the remote service runs in different processes. The android Binder Mechanism is used for inter-process communication. In Android, services are classified into local and remote services. Local services are easy to use, while remote services are slightly more complex to use. The following is a usage of aidl.

Aidl is the android interface definition language. There are too many posts on the Internet to introduce the concept. This article only describes the usage of aidl. Because aidl is used in a recently developed player project. Aidl is used for inter-process communication. In this example, the activity (Client Side) communicates with the Service (server side.
First, define the aidl file. For example, the interface exposed to activity in the service can be defined in the aidl file, and vice versa. In this article, the Service uses the serviceaidl. aidl interface file for the activity.
<PRE lang = "language" line = "1">
Package com. miloisbadboy. aidl;
Import com. miloisbadboy. aidl. activityaidl;
/**
* The interface exposed to activity in service can be defined in the aidl file.
*/
Interface serviceaidl {
Void callservice ();
/**
* Register activityaidl in the activity to the service so that the service can call the method in activityaidl.
**/
Void registactivitycallback (activityaidl callback );
}
</PRE>
Activityaidl. aidl used by activity to the service
<PRE lang = "language" line = "1">
Package com. miloisbadboy. aidl;
/**
* The interface exposed to service in activity can be defined in the aidl file.
*/
Interface activityaidl {
Void callactivity ();
}
</PRE>
The above two aidl will automatically generate the corresponding Java file under the gen directory.
Step 2: Write the service. Write a myservice to inherit from the service class and return the serviceaidl. Stub object in the onbind () method.
See the following code for details:
Myservice. Java
<PRE lang = "language" line = "1">
Package com. miloisbadboy;

Import Android. App. Service;
Import Android. content. intent;
Import Android. OS. ibinder;
Import Android. OS. RemoteException;
Import Android. util. log;
Import Android. widget. Toast;

Import com. miloisbadboy. aidl. activityaidl;
Import com. miloisbadboy. aidl. serviceaidl;

/**
* Remote service
*/
Public class myservice extends Service {
Public static final string SERVICE_NAME = "com. miloisbadboy. Start. myservice ";
Public static final string tag = "myservice ";

Private activityaidl;

@ Override
Public ibinder onbind (intent ){
Return mbinder;
}

Private serviceaidl. Stub mbinder = new serviceaidl. Stub (){

@ Override
Public void callservice () throws RemoteException {
Log. I (TAG, "activity call service's method ****** callservice ()");
Toast. maketext (getapplicationcontext (), "call service's method ****** callservice ()",
1000). Show ();

Activityaidl. callactivity ();
}

@ Override
Public void registactivitycallback (activityaidl callback) throws RemoteException {
Activityaidl = callback;
}

};
}

</PRE>
Myservice has a reference of activiyaidl. This type of instance is in serviceaidl. stud gets a value in the registactivitycallback (activityaidl callback) method. This method registers the activityaidl interface in the activity to the service.
The callservice () and registactivityaidl (activityaidl) methods in serviceaidl. Stub mbinder = new serviceaidl. Stub () {} are called in the activity.
Finally, let's look at an activity class.
<PRE lang = "language" line = "1">
Package com. miloisbadboy;

Import Android. App. activity;
Import Android. content. componentname;
Import Android. content. context;
Import Android. content. intent;
Import Android. content. serviceconnection;
Import Android. OS. Bundle;
Import Android. OS. ibinder;
Import Android. OS. RemoteException;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. Toast;

Import com. miloisbadboy. aidl. activityaidl;
Import com. miloisbadboy. aidl. serviceaidl;
/**
* Test remote service
*/
Public class testaidlactivity extends activity implements onclicklistener {

Private Static final string tag = "testaidlactivity ";

Private button start;
Private button stop;
Private button callservice;
/**
* Service call back
*/
Private serviceaidl;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Start = (button) findviewbyid (R. Id. start_service );
Stop = (button) findviewbyid (R. Id. stop_service );
Callservice = (button) findviewbyid (R. Id. call_service );

Start. setonclicklistener (this );
Stop. setonclicklistener (this );
Callservice. setonclicklistener (this );
}
@ Override
Public void onclick (view v ){
If (V = Start ){
Intent service = new intent (myservice. SERVICE_NAME );
// Bind the service and pass in the serviceconnection instance
Bindservice (Service, serviceconn, context. bind_auto_create );
Stop. setvisibility (view. Visible );
} Else if (V = stop ){
Unbindservice (serviceconn );
} Else {
Try {
If (serviceaidl! = NULL ){
Serviceaidl. callservice ();
} Else {
Toast. maketext (this, "service is not started! ", 1000). Show ();
}
} Catch (RemoteException e ){
E. printstacktrace ();
}
}
}
Private serviceconnection serviceconn = new serviceconnection (){

@ Override
Public void onservicedisconnected (componentname name ){
Serviceaidl = NULL;
Toast. maketext (getapplication (), "service is unbind! ", 1000). Show ();
}

@ Override
Public void onserviceconnected (componentname name, ibinder Service ){
Serviceaidl = serviceaidl. stub. asinterface (service );
// Register the interface that exposes the activity to the service
Try {
Serviceaidl. registactivitycallback (activityaidl );
} Catch (RemoteException e ){
E. printstacktrace ();
}
}
};
/**
* Implement the activityaidl INTERFACE IN THE ACTIVITY
*/
Private activityaidl = new activityaidl. Stub (){

@ Override
Public void callactivity () throws RemoteException {
Log. I (TAG, "callactivity ()");
Toast. maketext (getapplicationcontext (), "service call activity", 1000). Show ();
}
};
}
</PRE>
There are three buttons in the activity: Start service, stop service, and callservice.
The action of each button is as the name suggests. Special attention. In activity serviceconnection, The serviceaidl instance is obtained through serviceaidl. stub. asinterface (service), and the reference of activityaidl is registered in service.
In the Start Service button event, bindservice (Service, serviceconn, context. bind_auto_create) binds the service to the activity.

The program runs in the following order: after the activity is started, start service and click callservicebtn, The callservice () interface implemented in myservice will be called, in callservice, activityaidl is called, that is, the callback callactivity () method of the activity.
In this way, the communication between the activity and the service is simulated, that is, the objects of the other side are called each other.
This article just makes a small summary of the usage of aidl.

 

 

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.