本文共 944 字,大约阅读时间需要 3 分钟。
cpp部分通过jni调用java静态函数
头文件:
#include在cpp类中定义方法去调用Cocos2dxSound.java中vibrate方法:#include "cocos2d.h"#include "platform/android/jni/JniHelper.h"
static void vibrateJNI(long longtime) { JniMethodInfo methodInfo; if(!JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxSound", "vibrate","(J)V")) { CCLOG("Error: vibrate is 不存在。"); return; } methodInfo.env->CallStaticVoidMethod(methodInfo.classID,methodInfo.methodID,time); methodInfo.env->DeleteLocalRef(methodInfo.classID); }在androidprojectsrc/org/cocos2dx/lib/Cocos2dxSound.java定义vibrate方法:
private static Context mContext2;Cocos2dxSound()方法中增加
this.mContext2 = pContext;定义vibrate方法
public static void vibrate(long time) { Vibrator v = (Vibrator) mContext2.getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(time); }致此在cpp代码中须要震动处调用下面就可以
JNIUtil::vibrateJNI(500);
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5396795.html,如需转载请自行联系原作者