在使用Cocos2dx进行Android开发的时候,有时候想在c++中使用java编写的本地界面,并进行跳转。
首先在Android中添加本地activity:
package org.cocos2dx.cpp; import android.app.Activity; import android.os.Bundle; public class CocoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } }
<activity android:name="org.cocos2dx.cpp.CocoActivity"></activity>
本地有一个和Cocos2dx绑定的界面AppActivity:
public class AppActivity extends Cocos2dxActivity { static AppActivity context;//声明静态变量代表本界面 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); context = this;//此处为问题关键把本界面赋值给静态变量,以便获取本变量 } /*本静态方法用来获取本界面*/ public static Object getCon() { return context; } /*该非静态方法用来进行页面跳转*/ public void startActivity() { Intent intent = new Intent(this,Coco.class); startActivity(intent); } }
JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,/*JniMethodInfo的引用*/ "org/cocos2dx/cpp/AppActivity",/*类的路径*/ "getCon",/*函数名*/ "()Ljava/lang/Object;");/*函数类型简写*/ jobject activityObj;//调用静态方法获取要跳转的界面的object if (isHave) { //CallStaticObjectMethod调用java函数,并把返回值赋值给activityObj activityObj = minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID); } isHave = JniHelper::getMethodInfo(minfo,/*JniMethodInfo的引用*/ "org/cocos2dx/cpp/AppActivity",/*类的路径*/ "startActivity",/*函数名*/ "()V");/*函数类型简写*/ if (isHave) { //CallStaticObjectMethod调用java函数,并把返回值赋值给activityObj minfo.env->CallVoidMethod(activityObj,minfo.methodID);//通过获取的object调用非静态方法来进行界面跳转 }
原文链接:https://www.f2er.com/cocos2dx/346826.html