续上一篇搭建篇《Android实战——Cocos游戏容器搭建篇》,本篇带来cocos和Android通信篇的实现和使用, 围绕着多进程通信和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
)
}
android/CocosCallNative.java:
public class CocosCallNative {
public static void invoke(String action, String argument, String callbackId) {
CocosBridgeHelper.log("cocos进程获得游戏端数据", "argument: " + argument + ",action: " + action + ",callbackId: " + callbackId);
// 下发给cocos进程的监听者(不需要去主进程取数据时使用)
CocosBridgeHelper.getInstance().dispatchCocosListener(action, argument, callbackId);
try {
// 2.0 cocos进程发消息给主进程
CocosActivity.mIAIDLCocos2Main.cocos2Main(action, argument, callbackId);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
android/IAIDLCocos2Main.aidl
interface IAIDLCocos2Main {
void cocos2Main(String action, String argument, String callbackId);
// 将cocos进程的IAIDLCallBack传到主进程,用来主进程发消息给Cocos进程
void setAIDLCallBack(IAIDLCallBack iAIDLCallBack);
}
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("主进程收到Cocos进程消息", "action:" + action + "argument:" + argument + "callbackId:" + callbackId);
// 主进程收到消息,下发给主进程的监听者
CocosBridgeHelper.getInstance().dispatchMainListener(action, argument, callbackId);
}
@Override
public void setAIDLCallBack(IAIDLCallBack iAIDLCallBack) {
// 将cocos进程的IAIDLCallBack传到主进程,用来主进程发消息给Cocos进程
CocosBridgeHelper.getInstance().setIAIDLCallBack(iAIDLCallBack);
}
};
}
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);
}
// 死亡代理,保证服务通信通道正常
private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() {
@Override
public void binderDied() {
if (mIAIDLCocos2Main == null) return;
mIAIDLCocos2Main.asBinder().unlinkToDeath(mDeathRecipient, 0);
mIAIDLCocos2Main = null;
// 重新绑定远程服务
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 {
// 注册死亡代理
service.linkToDeath(mDeathRecipient, 0);
//将cocos进程的IAIDLCallBack传到主进程,用来主进程发消息给Cocos进程
mIAIDLCocos2Main.setAIDLCallBack(iAidlCallBack);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mIAIDLCocos2Main = null;
}
android/Cocos2MainService.java
@Override
public void cocos2Main(String action, String argument, String callbackId) {
CocosBridgeHelper.log("主进程收到Cocos进程消息", "action:" + action + "argument:" + argument + "callbackId:" + callbackId);
CocosBridgeHelper.getInstance().dispatchMainListener(action, argument, callbackId);
}
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();
}
}
android/CocosActivity.java
private IAIDLCallBack iAidlCallBack = new IAIDLCallBack.Stub() {
@Override
public void main2Cocos(String action, String argument, String callbackId) {
CocosBridgeHelper.log("Cocos进程收到主进程消息", "action: " + action + ", argument: " + argument + ", callbackId: " + callbackId);
CocosBridgeHelper.getInstance().nativeCallCocos(action, argument, callbackId);
}
};
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)));
}
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]);
}
};
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)
}
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
完结,撒花🎉