1.编写java端
打开Cocos2dxHelper.java
编写函数
/*time震动时间*/
public static void vibrate(int vibrateTime) {
@H_301_25@Vibrator vib = (Vibrator) sActivity.getSystemService(Service.VIBRATOR_SERVICE);
@H_301_25@vib.vibrate(vibrateTime);
@H_301_25@}
@H_301_25@
/*取消震动*/
public void cancelVibrate(){
@H_301_25@Vibrator vib = (Vibrator)sActivity.getSystemService(Service.VIBRATOR_SERVICE);
@H_301_25@vib.cancel();
@H_301_25@}
2.编写cocos2d-x端
.h文件
@H_301_25@static void vibrate(int time);
@H_301_25@static void cancelVibrate();
.cpp
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include <android/log.h>
#include "platform/android/jni/JniHelper.h"
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxHelper"
#endif
void HelloWorld::vibrate(int time)
{
@H_301_25@ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t,CLASS_NAME,"vibrate","(I)V"))
{
t.env->CallStaticVoidMethod(t.classID,t.methodID,time);
t.env->DeleteLocalRef(t.classID);
}
#endif
}
void HelloWorld::cancelVibrate()
{
@H_301_25@#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t,"cancelVibrate","()V"))
{
t.env->CallStaticVoidMethod(t.classID,t.methodID);
t.env->DeleteLocalRef(t.classID);
}
#endif
}
3.使用方法
vibrate(5000);
原文链接:https://www.f2er.com/cocos2dx/341913.html