Realize the goal: the client calls the interface of the server add
to calculate the sum of the two numbers and return it.
Server implementation
Create IDL file
Right-click on main
the folder to create .idl
a file, enter the file name such as IMyIdlInterface
the directory structure as follows: the definition method
in IMyIdlInterface.idl is as follows:
// IMyIdlInterface.idl
// Declare any non-default types here with sequenceable or interface statements
interface work.wxmx.aidlstudyserver.IMyIdlInterface {
/*
* Example of a service method that uses some parameters
*/
void serviceMethod1([in] int anInt);
int add([in] int a, [in] int b);
}
Build After
the build, the corresponding interface and class will be generated, as shown in the figure below.
Create the ServiceAbility
implementation as follows:
public class ServiceAbility extends Ability {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
private MyIdl mMyIdl = new MyIdl("MyIDL");
class MyIdl extends MyIdlInterfaceStub {
public MyIdl(String descriptor) {
super(descriptor);
}
@Override
public void serviceMethod1(int anInt) throws RemoteException {
}
@Override
public int add(int a, int b) throws RemoteException {
HiLog.info(LABEL_LOG, "add " + a + "");
return a + b;
}
}
@Override
public IRemoteObject onConnect(Intent intent) {
return mMyIdl;
}
}
To modify config.json, expose this Service
{
"module": {
"abilities": [
//...
{
"name": "work.wxmx.aidlstudyserver.ServiceAbility",
"icon": "$media:icon",
"description": "$string:serviceability_description",
"type": "service",
"visible": true // 新增此行
}
]
}
}
client implementation
Create another project, and .idl
copy the files and packages of the server to client
the end. The project structure is as follows: bind the Service of the server
in it, and call the method defined in MainAbilitySlice
the callback after the binding is successful .idl
add
public class MainAbilitySlice extends AbilitySlice {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0x002, "TAG");
IMyIdlInterface proxy;
private IAbilityConnection connection = new IAbilityConnection() {
@Override
public void onAbilityConnectDone(ElementName element, IRemoteObject remote, int resultCode) {
HiLog.error(LABEL_LOG, "AAA", "");
proxy = MyIdlInterfaceStub.asInterface(remote);
//MainAbilitySlice.this.proxy = new MyIdlInterfaceProxy(remote);
int add = 0;
try {
add = MainAbilitySlice.this.proxy.add(1, 1);
} catch (RemoteException e) {
e.printStackTrace();
}
HiLog.error(LABEL_LOG, " " + add);
}
@Override
public void onAbilityDisconnectDone(ElementName element, int resultCode) {
proxy = null;
}
};
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
}
@Override
public void onActive() {
super.onActive();
Intent intent = new Intent();
ElementName elementName = new ElementName("", "work.wxmx.aidlstudyserver",
"work.wxmx.aidlstudyserver.ServiceAbility");
intent.setElement(elementName);
connectAbility(intent, connection);
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}