Android實(shí)戰(zhàn)——Cocos游戲容器多進(jìn)程通信

一、概要

續(xù)上一篇搭建篇《Android實(shí)戰(zhàn)——Cocos游戲容器搭建篇》,本篇帶來(lái)cocos和Android通信篇的實(shí)現(xiàn)和使用, 圍繞著多進(jìn)程通信和cocos-android互調(diào)來(lái)實(shí)現(xiàn)

二、通信模型

image

如果不需要主進(jìn)程的數(shù)據(jù),可以直接1->4

三、如何實(shí)現(xiàn)通信

1.1 cocos調(diào)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
    )
}

1.2 android接收cocos

android/CocosCallNative.java:

public class CocosCallNative {
    public static void invoke(String action, String argument, String callbackId) {
        CocosBridgeHelper.log("cocos進(jìn)程獲得游戲端數(shù)據(jù)", "argument: " + argument + ",action: " + action + ",callbackId: " + callbackId);
        // 下發(fā)給cocos進(jìn)程的監(jiān)聽(tīng)者(不需要去主進(jìn)程取數(shù)據(jù)時(shí)使用)
        CocosBridgeHelper.getInstance().dispatchCocosListener(action, argument, callbackId);
        try {
            // 2.0 cocos進(jìn)程發(fā)消息給主進(jìn)程
            CocosActivity.mIAIDLCocos2Main.cocos2Main(action, argument, callbackId);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

2.1 cocos進(jìn)程發(fā)消息給主進(jìn)程

2.1.1 編寫(xiě)aidl

android/IAIDLCocos2Main.aidl

interface IAIDLCocos2Main {
    void cocos2Main(String action, String argument, String callbackId);
    // 將cocos進(jìn)程的IAIDLCallBack傳到主進(jìn)程,用來(lái)主進(jìn)程發(fā)消息給Cocos進(jìn)程
    void setAIDLCallBack(IAIDLCallBack iAIDLCallBack);
}
2.1.2 創(chuàng)建服務(wù)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("主進(jìn)程收到Cocos進(jìn)程消息", "action:" + action + "argument:" + argument + "callbackId:" + callbackId);
            // 主進(jìn)程收到消息,下發(fā)給主進(jìn)程的監(jiān)聽(tīng)者
            CocosBridgeHelper.getInstance().dispatchMainListener(action, argument, callbackId);
        }

        @Override
        public void setAIDLCallBack(IAIDLCallBack iAIDLCallBack) {
            // 將cocos進(jìn)程的IAIDLCallBack傳到主進(jìn)程,用來(lái)主進(jìn)程發(fā)消息給Cocos進(jìn)程
            CocosBridgeHelper.getInstance().setIAIDLCallBack(iAIDLCallBack);
        }
    };
}
2.1.3 在cocos進(jìn)程啟動(dòng)服務(wù)相關(guān)

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

// 死亡代理,保證服務(wù)通信通道正常
private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
    @Override
    public void binderDied() {
        if (mIAIDLCocos2Main == null) return;

        mIAIDLCocos2Main.asBinder().unlinkToDeath(mDeathRecipient, 0);
        mIAIDLCocos2Main = null;
        // 重新綁定遠(yuǎn)程服務(wù)
        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 {
        // 注冊(cè)死亡代理
        service.linkToDeath(mDeathRecipient, 0);
        //將cocos進(jìn)程的IAIDLCallBack傳到主進(jìn)程,用來(lái)主進(jìn)程發(fā)消息給Cocos進(jìn)程
        mIAIDLCocos2Main.setAIDLCallBack(iAidlCallBack);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

@Override
public void onServiceDisconnected(ComponentName name) {
    mIAIDLCocos2Main = null;
}

2.1 Android主進(jìn)程接收到消息

android/Cocos2MainService.java

@Override
public void cocos2Main(String action, String argument, String callbackId) {
    CocosBridgeHelper.log("主進(jìn)程收到Cocos進(jìn)程消息", "action:" + action + "argument:" + argument + "callbackId:" + callbackId);
    CocosBridgeHelper.getInstance().dispatchMainListener(action, argument, callbackId);
}

3.1主進(jìn)程回消息給cocos進(jìn)程

android/IAIDLCallBack.aidl

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

android/CocosBridgeHelper.java

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

3.2 cocos進(jìn)程接受主進(jìn)程的消息

android/CocosActivity.java

private IAIDLCallBack iAidlCallBack = new IAIDLCallBack.Stub() {
    @Override
    public void main2Cocos(String action, String argument, String callbackId) {
        CocosBridgeHelper.log("Cocos進(jìn)程收到主進(jìn)程消息", "action: " + action + ", argument: " + argument + ", callbackId: " + callbackId);
        CocosBridgeHelper.getInstance().nativeCallCocos(action, argument, callbackId);
    }
};

4.1 Android調(diào)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)));
}

4.2 cocos接收Android發(fā)來(lái)的消息

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

};

四、如何使用

1. 先來(lái)看效果

image

2. 實(shí)現(xiàn)彈出Android的Dialog,選擇后把結(jié)果傳給cocos顯示(不需要主進(jìn)程的數(shù)據(jù),可以直接1->4)

android/CocosGameActivity.kt

private val showArray = arrayOf("劉德華", "周華健")
private val cocosListenerInCocos: CocosDataListener = CocosDataListener { action, argument, callbackId ->
    CocosBridgeHelper.log("接收InCocos", action)
    if (action == "action_showStarDialog") {
        runOnUiThread {
            AlertDialog.Builder(this)
                .setTitle("選擇")
                .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() {
    super.onDestroy()
    CocosBridgeHelper.getInstance().removeCocosListener(cocosListenerInCocos)
}

2. 實(shí)現(xiàn)從主進(jìn)程取數(shù)據(jù)給cocos顯示

android/MainActivity.kt

private val cocosListenerInMain: CocosDataListener = CocosDataListener { action, argument, callbackId ->
    CocosBridgeHelper.log("接收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() {
    super.onDestroy()
    CocosBridgeHelper.getInstance().removeMainListener(cocosListenerInMain)
}

五、本篇完整code


完結(jié),撒花??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容