CPP section calls Java static functions via JNI
Header file:
#include <jni.h> #include "cocos2d.h" #include "platform/android/jni/jnihelper.h"
Define methods in the CPP class to invoke the vibrate method in Cocos2dxsound.java:
static void Vibratejni (long longtime) {Jnimethodinfo methodinfo;if (! Jnihelper::getstaticmethodinfo (MethodInfo, "Org/cocos2dx/lib/cocos2dxsound", "Vibrate", "(J) V")) {Cclog ("Error: Vibrate is not there! "); return;} Methodinfo.env->callstaticvoidmethod (methodinfo.classid,methodinfo.methodid,time);methodInfo.env-> Deletelocalref (METHODINFO.CLASSID);}
In Android engineering Src/org/cocos2dx/lib/cocos2dxsound.java define the vibrate method:
private static Context mContext2;
In the Cocos2dxsound () method, add
This.mcontext2 = PContext;
Defining the Vibrate method
public static void vibrate (long time) {Vibrator V = (vibrator) mcontext2.getsystemservice (Context.vibrator_service); V.vibrate (time);}
In the CPP code, you need to call the following
Jniutil::vibratejni (500);