AIDL is used between two Android processes.

Source: Internet
Author: User

We assume that two applications are A and B.

My intention is to create Aidl in application B, implement the methods in aidl here, and then remotely call in application:

Okay: I will first create B Application: package com. king. android. service;

Interface IService {

// Set the address
Void setAddress (String address );

// Set the country
Void setCountry (String country );

String disPlay ();

}

In Aidl, do not use any access modifier or static or final modifier. Other data types may be different:

Now, after creating this aidl file, I need to implement the aidl interface method:

Package com. king. android. service;

Import android. OS. RemoteException;

/**

* Description: implements the IService interface.
* Author: Andy. Liu
* Time: 08:41:02
**/
Public class IServiceImpl extends IService. Stub {
Private String mAddress = null;
Private String mCountry = null;

@ Override
Public void setAddress (String address) throws RemoteException {
This. mAddress = address;
}

@ Override
Public void setCountry (String country) throws RemoteException {
This. mCountry = country;
}

@ Override
Public String disPlay () throws RemoteException {

Return "address:" + mAddress + "-------------- mCountry:" + mCountry;
}

}

The above is the implementation class that implements this aidl interface.

 

Expose your aidl for other applications:

Package com. king. android. service;

Import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;

Import com. king. android. service. IService. Stub;

/**

* Description: exposes the implemented aidl interface to the client.
* Author: Andy. Liu
* Time: 07:01:49
**/
Public class MyRemoteSerivce extends Service {
Public static final String MY_SERIVCE = "com. king. android. MY_REMOTE_SERIVCE ";

Private Stub iService = new IServiceImpl ();
@ Override
Public IBinder onBind (Intent intent ){
Return iService;
}

}

In addition, you must register this service in the Androidmenifest. xml file. And filter...

In this way, the preparation work in program B is completed...

 

Preparations for Project:

Copy the package of the aidl file in application B and the Aidl file to application. And save...

Now, we can call the interface methods provided by application B in application...

Package com. king. android. controls;

Import com. king. android. R;

Import android. app. Activity;
Import android. app. Service;
Import android. content. ComponentName;
Import android. content. Intent;
Import android. content. IntentFilter;
Import android. content. ServiceConnection;
Import android. OS. Bundle;
Import android. OS. IBinder;
Import android. OS. RemoteException;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;

Public class MainActivity extends Activity {
Public static final String MY_SERIVCE = "com. king. android. MY_REMOTE_SERIVCE"; // The action specified by the Service

Private IService iService;
Private Button btn;
MyReceiver2 receiver ER = new MyReceiver2 ();

@ Override
Protected void onResume (){
Super. onResume ();
IntentFilter filter = new IntentFilter ();
// Instantiate MYReceiver2
RegisterReceiver (receiver, filter );
}
@ Override
Protected void onPause (){
Super. onPause ();
UnregisterReceiver (receiver );
}

@ Override
Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Btn = (Button) findViewById (R. id. btn_voice );
Btn. setVisibility (View. VISIBLE );
Btn. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setAction (MY_SERVICE );
BindService (intent, conn, Service. BIND_AUTO_CREATE );
}
});
}



Private ServiceConnection conn = new ServiceConnection (){
@ Override
Synchronized public void onServiceConnected (ComponentName, IBinder service ){
IPerson = IPerson. Stub. asInterface (service );
If (null! = IPerson ){
// RPC call Method
Try {
IPerson. setAddress ("shenzhen. guangdong ");
IPerson. setCountry ("china ");
String msg = iPerson. display ();
Toast. makeText (MainActivity. this, msg, Toast. LENGTH_LONG). show ();
} Catch (RemoteException e ){

E. printStackTrace ();
}
}
}

@ Override
Public void onServiceDisconnected (ComponentName name ){
}
};

}

Input the required parameters in onServiceConnedted.

Then bind the service.

 

 

 

 

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.