A summary,

Following the previous construction article “Android Combat — Cocos Game container Construction”, this article brings about the implementation and use of Cocos and Android communication, around multi-process communication and CoCOs-Android interoperability to achieve

2. Communication model

If you do not need data from the main process, you can directly use 1->4

Three, how to achieve communication

1.1 the cocos android

cocos/mainUI.ts:

cocosCallNative(action: String, argument: String, callbackId: String) {
    jsb.reflection.callStaticMethod(
        'com.cocos.bridge.CocosCallNative'.'invoke'.'(Ljava/lang/String; Ljava/lang/String; Ljava/lang/String;) V',
        action,
        argument,
        callbackId
    )
}
Copy the code

1.2 Android accepts COCOS

android/CocosCallNative.java:

public class CocosCallNative {
    public static void invoke(String action, String argument, String callbackId) {
        CocosBridgeHelper.log("Cocos process gets game side data"."Argument." + argument + ",action: " + action + ",callbackId: " + callbackId);
        // To the listener of the cocos process (used when the main process does not need to fetch data)
        CocosBridgeHelper.getInstance().dispatchCocosListener(action, argument, callbackId);
        try {
            The 2.0 cocos process sends a message to the main process
            CocosActivity.mIAIDLCocos2Main.cocos2Main(action, argument, callbackId);
        } catch(RemoteException e) { e.printStackTrace(); }}}Copy the code

2.1 The COCOS process sends a message to the main process

2.1.1 write aidl

android/IAIDLCocos2Main.aidl

interface IAIDLCocos2Main {
    void cocos2Main(String action, String argument, String callbackId);
    // Pass the IAIDLCallBack of the cocos process to the main process to send messages to the Cocos process
    void setAIDLCallBack(IAIDLCallBack iAIDLCallBack);
}
Copy the code
2.1.2 Creating a service Cocos2MainService

android/Cocos2MainService.java

public class Cocos2MainService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    IAIDLCocos2Main.Stub mBinder = new IAIDLCocos2Main.Stub() {

        @Override
        public void cocos2Main(String action, String argument, String callbackId) {
            CocosBridgeHelper.log("Main process received Cocos process message"."action:" + action + "argument:" + argument + "callbackId:" + callbackId);
            // The main process receives the message and sends it to the listener of the main process
            CocosBridgeHelper.getInstance().dispatchMainListener(action, argument, callbackId);
        }

        @Override
        public void setAIDLCallBack(IAIDLCallBack iAIDLCallBack) {
            // Pass the IAIDLCallBack of the cocos process to the main process to send messages to the Cocos processCocosBridgeHelper.getInstance().setIAIDLCallBack(iAIDLCallBack); }}; }Copy the code
2.1.3 Related to starting the service in coCOS process

android/CocosActivity.java

public static IAIDLCocos2Main mIAIDLCocos2Main;

@Override
protected void onCreate(Bundle savedInstanceState) {... Intent intent =new Intent(this, Cocos2MainService.class);
    bindService(intent, this, Context.BIND_AUTO_CREATE);
}

// Death proxy to ensure that the service communication channel is normal
private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
    @Override
    public void binderDied(a) {
        if (mIAIDLCocos2Main == null) return;

        mIAIDLCocos2Main.asBinder().unlinkToDeath(mDeathRecipient, 0);
        mIAIDLCocos2Main = null;
        // Rebind the remote service
        Intent intent = new Intent(CocosActivity.this, Cocos2MainService.class);
        bindService(intent, CocosActivity.this, Context.BIND_AUTO_CREATE); }};@Override
public void onServiceConnected(ComponentName name, IBinder service) {
    mIAIDLCocos2Main = IAIDLCocos2Main.Stub.asInterface(service);
    try {
        // Register a death agent
        service.linkToDeath(mDeathRecipient, 0);
        // Pass the IAIDLCallBack of the cocos process to the main process to send messages to the Cocos process
        mIAIDLCocos2Main.setAIDLCallBack(iAidlCallBack);
    } catch(RemoteException e) { e.printStackTrace(); }}@Override
public void onServiceDisconnected(ComponentName name) {
    mIAIDLCocos2Main = null;
}

Copy the code

2.1 The Main Android process Receives a Message

android/Cocos2MainService.java

@Override
public void cocos2Main(String action, String argument, String callbackId) {
    CocosBridgeHelper.log("Main process received Cocos process message"."action:" + action + "argument:" + argument + "callbackId:" + callbackId);
    CocosBridgeHelper.getInstance().dispatchMainListener(action, argument, callbackId);
}
Copy the code

3.1 The main process returns a message to the COcos process

android/IAIDLCallBack.aidl

interface IAIDLCallBack {
    void main2Cocos(String action, String argument, String callbackId);
}
Copy the code

android/CocosBridgeHelper.java

public void main2Cocos(String action, String argument, String callbackId) {
    try {
        mIAIDLCallBack.main2Cocos(action, argument, callbackId);
    } catch(RemoteException e) { e.printStackTrace(); }}Copy the code

3.2 The COcos process receives messages from the main process

android/CocosActivity.java

private IAIDLCallBack iAidlCallBack = new IAIDLCallBack.Stub() {
    @Override
    public void main2Cocos(String action, String argument, String callbackId) {
        CocosBridgeHelper.log("Cocos process received main process message"."action: " + action + ", argument: " + argument + ", callbackId: "+ callbackId); CocosBridgeHelper.getInstance().nativeCallCocos(action, argument, callbackId); }};Copy the code

4.1 the Android cocos

android/CocosBridgeHelper.java

public void nativeCallCocos(String action, String argument, String callbackId) {
    CocosHelper.runOnGameThread(() -> CocosJavascriptJavaBridge.evalString(String.format("cc.nativeCallCocos('%s', '%s', '%s');", action, argument, callbackId)));
}
Copy the code

4.2 CoCOS receives messages from Android

cocos/mainUI.ts

cc.nativeCallCocos = function(action: String, argument: String, callbackId: String) {
    if (action == ACTION_SHOW_STARDIALOG) {
        uiManager.instance.showDialog('common/tips', [argument]);
    } else if (action == ACTION_APP_VERSION) {
        uiManager.instance.showDialog('common/tips', [argument]); }};Copy the code

Four, how to use

1. The Best of 2015

2. Implement the pop-up Android Dialog, select the result to coCOS display (do not need the main process data, can be directly 1->4)

android/CocosGameActivity.kt

private val showArray = arrayOf("Andy Lau"."Chau Wah Kin")
private val cocosListenerInCocos: CocosDataListener = CocosDataListener { action, argument, callbackId ->
    CocosBridgeHelper.log("Receive InCocos", action)
    if (action == "action_showStarDialog") {
        runOnUiThread {
            AlertDialog.Builder(this)
                .setTitle("Choice")
                .setItems(showArray) { _, index ->
                    CocosBridgeHelper.getInstance().nativeCallCocos(action, showArray[index], callbackId)
                }
                .create()
                .show()
        }
    }

}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    CocosBridgeHelper.getInstance().addCocosListener(cocosListenerInCocos)
}
override fun onDestroy(a) {
    super.onDestroy()
    CocosBridgeHelper.getInstance().removeCocosListener(cocosListenerInCocos)
}
Copy the code

2. Get data from the main process to coCOS display

android/MainActivity.kt

private val cocosListenerInMain: CocosDataListener = CocosDataListener { action, argument, callbackId ->
    CocosBridgeHelper.log("Receive InMain", action)
    if (action == "action_appVersion") {
        CocosBridgeHelper.getInstance().main2Cocos(action, packageManager.getPackageInfo(packageName, 0).versionName, callbackId)
    }
}
override fun onCreate(savedInstanceState: Bundle?) {
    CocosBridgeHelper.getInstance().addMainListener(cocosListenerInMain)
}

override fun onDestroy(a) {
    super.onDestroy()
    CocosBridgeHelper.getInstance().removeMainListener(cocosListenerInMain)
}
Copy the code

Five,This complete code


End, scatter flower 🎉