Android remote service aidl usage

Source: Internet
Author: User

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 and the activity calls back the service interface in the activityaidl. aidl file.

Package org. August. Music;
Import org. August. Music. activityaidl;
Interface serviceaidl {
Package org. August. Music;
Interface activityaidl {
Void callactivity ();
}

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:

Package org. August. Music;

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

Public class myservice extends Service {
Public static final string SERVICE_NAME = "org. August. Music. Start. myservice ";

Private Static final string tag = "myservice ";
Private activityaidl activityaidlcallback;
@ Override
Public void oncreate (){
Super. oncreate ();
Log. I (TAG, "oncreate ");
}
@ Override
Public void onstart (intent, int startid ){
Super. onstart (intent, startid );
Log. I (TAG, "onstart ");
}

@ Override
Public void ondestroy (){
Super. ondestroy ();
Log. I (TAG, "ondestroy ");
}
@ Override
Public void onrebind (intent ){
Super. onrebind (intent );
Log. I (TAG, "onrebind ");
}
@ Override
Public Boolean onunbind (intent ){
Log. I (TAG, "onunbind ");
Return super. onunbind (intent );
}
@ Override
Public ibinder onbind (intent ){
Return mbinder;
}

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

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

Activityaidlcallback. callactivity ();
}

@ Override
Public void registactivityaidl (activityaidl)
Throws RemoteException {
Activityaidlcallback = activityaidl;
}
};

}

Note that the callservice () and registactivityaidl (activityaidl) methods in serviceaidl. Stub mbinder = new serviceaidl. Stub () {} are called in the activity. Registactivityaidl (activityaidl) registers the callback method of the Service to the Service in the activity, so that the service can call the activity interface.

Package org. August. Music;

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. widget. Button;
Import Android. widget. Toast;

Public class mainactivity extends activity {
Private Static final string tag = "mainactivity ";
Private button startbtn;
Private button stopbtn;
Private button callservicebtn;

Private serviceaidl myservice;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. main_activity );
Init ();
}
Private void Init (){
Startbtn = (button) findviewbyid (R. Id. start_service );
Stopbtn = (button) findviewbyid (R. Id. stop_service );
Callservicebtn = (button) findviewbyid (R. Id. call_service_method );

Startbtn. setonclicklistener (btnlistener );
Stopbtn. setonclicklistener (btnlistener );
Callservicebtn. setonclicklistener (btnlistener );

}
Private view. onclicklistener btnlistener = new view. onclicklistener (){

@ Override
Public void onclick (view v ){
Switch (V. GETID ()){
Case R. Id. start_service:
Startservice ();
Break;
Case R. Id. stop_service:
Stopservice ();
Break;
Case R. Id. call_service_method:
If (myservice = NULL ){
Toast. maketext (getapplicationcontext (), "service is blank", 1000). Show ();
Return;
}
Try {
Myservice. callservice ();
} Catch (RemoteException e ){
E. printstacktrace ();
}
Break;
}
}
};
Private void startservice (){
Intent intent = new intent ();
Intent. setaction (myservice. SERVICE_NAME );
Bindservice (intent, serviceconn, context. bind_auto_create );
Startservice (intent );
Log. I (TAG, "********* startservice ()");
}
Private void stopservice (){

}
Private serviceconnection serviceconn = new serviceconnection (){

@ Override
Public void onservicedisconnected (componentname name ){
Myservice = NULL;
}

@ Override
Public void onserviceconnected (componentname name, ibinder Service ){
Myservice = serviceaidl. stub. asinterface (service );
Try {
Myservice. registactivityaidl (activityaidl );
} Catch (RemoteException e ){
E. printstacktrace ();
}
}
};
Private activityaidl = new activityaidl. Stub (){

@ Override
Public void callactivity () throws RemoteException {
Log. I (TAG, "callactivity ()");
Toast. maketext (getapplicationcontext (), "Call activity", 1000). Show ();
}
};
}

There are three buttons in the activity: Start service, stop service, and callservice.
The action of each button is as the name suggests. In the activity, you will see the serviceaidl (myservice) instance will be obtained in serviceconnection.
And register the reference of activityaidl to myservice.

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 to call the callback callactivity () 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.

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.