1. Communication between processes

  • useBundle
  • File sharing (file locking)
  • useMessenger(Serial, unidirectional)
  • useAIDL

2. To createserver 端

Here a module is recreated as the server side.

  • To create aaidlFile;
  • createserviceimplementationaidlInterface in;
  • exposedservice ;

2.1 createaidlfile

Directly in thejavaCreate a new folderaidlFile, and it will be generated automaticallyaidlFolder, let’s create oneIEasyService.aidl

package com.test.server;

// Declare any non-default types here with import statements

interface IEasyService {

    void connect(String mes);
    void disConnect(String mes);
}

Copy the code

2.2 createserviceImplementing an interface

public class EasyService extends Service { @Override public IBinder onBind(Intent intent) { Log.i("qq","onBind: intent="+intent.toString()); return mBinder; } IEasyService.Stub mBinder = new IEasyService.Stub() { @Override public void connect(String mes) throws RemoteException  { Log.i("qq","connect: mes = "+mes); } @Override public void disConnect(String mes) throws RemoteException { Log.i("qq","disConnect: mes = "+mes); }}; @Override public boolean onUnbind(Intent intent) { Log.i("qq","onUnbind: "); return super.onUnbind(intent); } @Override public void onDestroy() { Log.i("qq","onDestroy: "); super.onDestroy(); }}Copy the code

If you find yourIEasyService.StubNone of the methods implemented in itconnect, just manually click the icon 👉 。

2.3 exposedservice

Register the service in the manifest file.

<! <service android:name=".EasyService" Android :enabled="true" Android :exported="true" android:process=":remote"> <intent-filter> <action android:name="com.test.server.IEasyService"/> </intent-filter> </service>Copy the code

3. Create aclient 端

theservertheaidlCopy files toclient, the package name must also be the same 👌.

Then you can click the button, bind the remote service, get the proxy object of the remote service, and then call the remote method ~😊 code as follows 👇

@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); SetOnClickListener (new View.onClickListener () {@override public void onClick(View v) {// Bind the remote service  Intent intent = new Intent("com.test.server.IEasyService"); intent.setPackage("com.test.server") ; bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE); }}); findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mEasyService ! = null ){ try { mEasyService.connect("Client connect"); } catch (RemoteException e) { e.printStackTrace(); }}}}); } private IEasyService mEasyService; ServiceConnection mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mEasyService = IEasyService.Stub.asInterface(service); Log.i("qq"," connected to remote service "); } @Override public void onServiceDisconnected(ComponentName name) { mEasyService = null; Log. I ("qq"," disconnected remote service "); }};Copy the code

Run it and see the result: 🙈