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.