Hongmeng beginner inter-process communication (IDL)

Realize the goal: the client calls the interface of the server addto calculate the sum of the two numbers and return it.

Introduction to Interface Description Language
Idl

Server implementation

Create IDL file

Right-click on mainthe folder to create .idla file, enter the file name such as IMyIdlInterface
the directory structure as follows: the definition method
insert image description here
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
insert image description here
the build, the corresponding interface and class will be generated, as shown in the figure below.
insert image description here
Create the ServiceAbility
insert image description here
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 .idlcopy the files and packages of the server to clientthe end. The project structure is as follows: bind the Service of the server
insert image description here
in it, and call the method defined in MainAbilitySlicethe callback after the binding is successful .idladd

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);
    }
}

The result is as follows

insert image description here

Guess you like

Origin blog.csdn.net/qq_41359651/article/details/119594523