Note: AIDL is required. 1. Create an aidl file first, as shown in IPerson. aidl.
2. After creation, refresh the file. You can see the Iperson. java file implemented by the proxy under R. gen.
3. Implement the aidl interface. For example, IpersonImpl. java implements the interface in IPerson. aidl.
4. Use the Service to expose the interface to the client for calling. MyRemoteService. java
5. The client can call this interface.
Postscript: data is called between two applications.
Package com. king. android. controls;
// IPerson Interface
Interface IPerson {
// Set the age
Void setAge (int age );
// Set the name
Void setName (String name );
// Information Display
String display ();
}
Package com. king. android. controls;
Import android. OS. RemoteException;
/**
* Description: implements the Iperson. aidl interface.
* Author: Andy. Liu
* Time: 10:44:27
**/
Public class IPersonImpl extends IPerson. Stub {
Private int age;
Private String name;
@ Override
Public String display () throws RemoteException {
Return "name:" + name + "; age =" + age;
}
@ Override
Public void setAge (int age) throws RemoteException {
This. age = age;
}
@ Override
Public void setName (String name) throws RemoteException {
This. name = name;
}
}
Package com. king. android. controls;
Import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;
Import com. king. android. controls. IPerson. Stub;
/**
* Description: exposes an interface to the client using the service.
* Author: Andy. Liu
* Time: 10:47:47
**/
Public class MyRemoteService extends Service {
// Declare the Iperson Interface
Private Stub iPerson = new IPersonImpl ();
@ Override
Public IBinder onBind (Intent intent ){
Return iPerson;
}
}
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. 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;
/**
* Description: Test Aidl.
* Author: Andy. Liu
* Time: 10:54:00
**/
Public class MainActivity extends Activity {
Private static final String MY_SERVICE = "com. king. android. controls. MY_REMOTE_SERVICE"; // action specified by the Service
// Declare the IPerson Interface
Private IPerson iPerson;
Private Button btn;
// Instantiate Serviceconnection
@ 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. setName ("king. android. iphone ");
IPerson. setAge (30 );
String msg = iPerson. display ();
Toast. makeText (MainActivity. this, msg, Toast. LENGTH_LONG). show ();
} Catch (RemoteException e ){
E. printStackTrace ();
}
}
}
@ Override
Public void onServiceDisconnected (ComponentName name ){
}
};
}