x界面跳转到新的Activity

废话不多说,直接上源码

1、Java层

1)首先在org.cocos2dx.cpp目录下添加新类UserInfoActivity.java,该类自行定义即可,,代码如下:

package org.cocos2dx.cpp;import com.pactera.jni.R;//注意路径import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.content.Intent; public class UserInfoActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.userinfo);Intent intent = getIntent();String string = intent.getStringExtra("name");TextView textView = new TextView(this);textView.setTextSize(40);textView.setText(string);setContentView(textView);} }2)AppActivity.java类

package org.cocos2dx.cpp;import org.cocos2dx.lib.Cocos2dxActivity;import org.cocos2dx.lib.Cocos2dxGLSurfaceView;import android.content.Intent;import android.os.Bundle;public class AppActivity extends Cocos2dxActivity{public static final int SHOW_DIALOG = 0x0001;protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);}//设置OpenGL的相关信息public Cocos2dxGLSurfaceView onCreateView() {Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);// GuideLayer should create stencil bufferglSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);return glSurfaceView;}//加载Cocos2d-x的库static {System.loadLibrary("cocos2dcpp");}public static void Share(){ //Jni的Java层nativa代码,在Jni目录下的test.cpp实现,并被C++调用 Intent intent = new Intent();intent.setClass(AppActivity.getContext(), UserInfoActivity.class);intent.putExtra("name", "Hello World");AppActivity.getContext().startActivity(intent);}}

2、Jni层jni目录下添加test类的.h和.cpp

test.h

#ifndef TEST_H_#define TEST_H_extern "C"{//C++调Java的函数接口,该方法在HelloWorldScene中menuCallback函数中使用。void Share();}#endiftest.cpp

#include "test.h"#include "cocos2d.h"#include "platform/android/jni/JniHelper.h"#include "../../../Classes/JniTest.h"#include <jni.h>//#define CLASS_NAME "org/cocos2dx/cpp/JniTestHelper"#define CLASS_NAMENEW "org/cocos2dx/cpp/AppActivity"using namespace cocos2d;extern "C"{void Share(){bool hasMethod;JniMethodInfo t;hasMethod = JniHelper::getStaticMethodInfo(t,CLASS_NAMENEW,"Share","()V");if(hasMethod){log("Share function");if(t.methodID){t.env->CallStaticVoidMethod(t.classID,t.methodID);log("function share() was called");}else{log("function share was not called");}}else{log("function share was not found");}}}

3、C++层void HelloWorld::menuCloseCallback(Ref* pSender){#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)Share();#endif#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)exit(0);#endif}4、注意事项添加平台判断头文件#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include "../proj.android/jni/hellocpp/test.h"#endif5、运行结果

快乐不是因为得到的多而是因为计较的少!

x界面跳转到新的Activity

相关文章:

你感兴趣的文章:

标签云: