cpp通过Message掉java

1.文件引入

 

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include <android/log.h>
#endif

 

 

2.按钮部分代码

 

void HelloWorld::menuCloseCallback(CCObject* pSender) {
	bool isHave;
	const char* msgRet;
	JniMethodInfo minfo;
	const char* fileName = "testWei";

	isHave = JniHelper::getStaticMethodInfo(minfo, "org/test/MyStatic",
			"sendWeibo", "(Ljava/lang/String;)Ljava/lang/String;");
	if (isHave) {
		jstring jTitle = minfo.env->NewStringUTF(fileName);
		jobject obj = minfo.env->CallStaticObjectMethod(minfo.classID,
				minfo.methodID, jTitle);
		jstring jRet = (jstring) obj;
		msgRet = (minfo.env)->GetStringUTFChars(jRet, NULL);
	}
}

 

 

3.java 有static类方法

public class MyStatic {

	private static Handler mHandler;
	public static final int SHOW_DIALOG = 0x0001;

	public static void init(Handler handler) {
		MyStatic.mHandler = handler;
	}

	public static String sendWeibo(String pCon) {

		System.out.println("sendWeibo  :  " + pCon);
		Message msg = mHandler.obtainMessage(); // 发送给那个类
		msg.what = MyStatic.SHOW_DIALOG;
		msg.sendToTarget();

		return "";
	}

}

 

4.java接受3发过来的消息代码

onCreate..里

MyStatic.init(mHandler);

public Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MyStatic.SHOW_DIALOG:

				System.out.println("SHOW_DIALOG  SHOW_DIALOG");
				break;
			}
		}
	};
	

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(message)