#include "cocos2d.h" #include "JniFun.h" #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID #include #include "android/jni/JniHelper.h" #endif #define JAVA_CLASSNAME "com/jxkh/queyi/Native" USING_NS_CC; namespace JniFun { void longinWX(const char* APP_ID,const char* AppSecret) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME,"LoginWX","(Ljava/lang/String;Ljava/lang/String;)V"); if (isHave) { jstring jAPP_ID = minfo.env->NewStringUTF(APP_ID); jstring jAppSecret = minfo.env->NewStringUTF(AppSecret); minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,jAPP_ID,jAppSecret); minfo.env->DeleteLocalRef(jAPP_ID); minfo.env->DeleteLocalRef(jAppSecret); minfo.env->DeleteLocalRef(minfo.classID); cocos2d::log("JniFun call LoginWX over!"); } else { cocos2d::log("JniFun call LoginWX error!"); } #endif } void shareImageWX(const char* ImgPath,int nType) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME,"ShareImageWX","(Ljava/lang/String;I)V"); if (isHave) { jstring jImgPath = minfo.env->NewStringUTF(ImgPath); minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,jImgPath,nType); cocos2d::log("JniFun call ShareImageWX over!"); minfo.env->DeleteLocalRef(jImgPath); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call ShareImageWX error!"); } #endif } void shareTextWX(const char* kText,int nType) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME,"ShareTextWX","(Ljava/lang/String;I)V"); if (isHave) { jstring jkText = minfo.env->NewStringUTF(kText); minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,jkText,nType); cocos2d::log("JniFun call ShareTextWX over!"); minfo.env->DeleteLocalRef(jkText); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call ShareTextWX error!"); } #endif } void shareUrlWX(const char* kUrl,const char* kTitle,const char* kDesc,int nType) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME,"ShareUrlWX","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V"); if (isHave) { jstring jkUrlt = minfo.env->NewStringUTF(kUrl); jstring jkTitle = minfo.env->NewStringUTF(kTitle); jstring jkDesc = minfo.env->NewStringUTF(kDesc); minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,jkUrlt,jkTitle,jkDesc,nType); cocos2d::log("JniFun call ShareUrlWX over!"); minfo.env->DeleteLocalRef(jkUrlt); minfo.env->DeleteLocalRef(jkTitle); minfo.env->DeleteLocalRef(jkDesc); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call ShareUrlWX error!"); } #endif } void shareImageXL(const char* ImgPath, int nType) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME,"ShareImageXL","(Ljava/lang/String;I)V"); if (isHave) { jstring jImgPath = minfo.env->NewStringUTF(ImgPath); minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,jImgPath,nType); cocos2d::log("JniFun call ShareImageXL over!"); minfo.env->DeleteLocalRef(jImgPath); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call ShareImageXL error!"); } #endif } void shareTextXL(const char* kText, int nType) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "ShareTextXL", "(Ljava/lang/String;I)V"); if (isHave) { jstring jkText = minfo.env->NewStringUTF(kText); minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, jkText, nType); cocos2d::log("JniFun call ShareTextXL over!"); minfo.env->DeleteLocalRef(jkText); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call ShareTextXL error!"); } #endif } void shareUrlXL(const char* kUrl, const char* kTitle, const char* kDesc, int nType) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "ShareUrlXL", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V"); if (isHave) { jstring jkUrlt = minfo.env->NewStringUTF(kUrl); jstring jkTitle = minfo.env->NewStringUTF(kTitle); jstring jkDesc = minfo.env->NewStringUTF(kDesc); minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, jkUrlt, jkTitle, jkDesc, nType); cocos2d::log("JniFun call ShareUrlXL over!"); minfo.env->DeleteLocalRef(jkUrlt); minfo.env->DeleteLocalRef(jkTitle); minfo.env->DeleteLocalRef(jkDesc); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call ShareUrlXL error!"); } #endif } void showWebView(const char* url ) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME,"showWebView","(Ljava/lang/String;)V"); if (isHave) { jstring jurl = minfo.env->NewStringUTF(url); minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,jurl); cocos2d::log("JniFun call showWebView over!"); minfo.env->DeleteLocalRef(jurl); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call showWebView error!"); } #endif } // 版本更新 void versionUpdate(const char* url ,const char* desc, const int filesize, const int isUpdate) { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME, "versionUpdate", "(Ljava/lang/String;Ljava/lang/String;II)V"); if (isHave) { jstring jFileName = minfo.env->NewStringUTF(url); jstring jDes = minfo.env->NewStringUTF(desc); minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,jFileName,jDes, filesize, isUpdate); cocos2d::log("JniFun call versionUpdate over!"); minfo.env->DeleteLocalRef(jFileName); minfo.env->DeleteLocalRef(jDes); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call versionUpdate error!"); } #endif } static std::string kRecordFileName = ""; void startSoundRecord() { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME, "startSoundRecord", "()V"); if (isHave) { minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID); cocos2d::log("JniFun call startSoundRecord over!"); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call startSoundRecord error!"); } #endif } const char* stopSoundRecord() { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID std::string str; JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo,JAVA_CLASSNAME, "stopSoundRecord", "()Ljava/lang/String;"); if (isHave) { jstring jFileName = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); const char *newStr = minfo.env->GetStringUTFChars(jFileName, 0); str = newStr; cocos2d::log("JniFun call stopSoundRecord over :"); cocos2d::log("%s",str.c_str()); minfo.env->ReleaseStringUTFChars(jFileName, newStr); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call stopSoundRecord error!"); } return str.c_str(); #endif return ""; } //获取版本号 int getVersionCode(void) { int nVerCode = 0; #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) nVerCode = 100; #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; if (JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "getVersionCode", "()I")) { nVerCode = minfo.env->CallStaticIntMethod(minfo.classID, minfo.methodID); minfo.env->DeleteLocalRef(minfo.classID); } #endif return nVerCode; } //获取版本号 std::string getVersionName(void) { std::string strVerName; #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) strVerName = "1.0.8"; #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; if (JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "getVersionName", "()Ljava/lang/String;")) { jstring ret = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); strVerName = JniHelper::jstring2string(ret); minfo.env->DeleteLocalRef(ret); minfo.env->DeleteLocalRef(minfo.classID); } #endif return strVerName; } //获取应用名称 std::string getAppName(void) { std::string strAppName; #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) strAppName = "wnmj"; #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; if (JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "getAppName", "()Ljava/lang/String;")) { jstring ret = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); strAppName = JniHelper::jstring2string(ret); minfo.env->DeleteLocalRef(ret); minfo.env->DeleteLocalRef(minfo.classID); } #endif return strAppName; } //获取APP启动参数 std::string getRunParam(void) { std::string strRunAppParam; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; if (JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "getRunParam", "()Ljava/lang/String;")) { jstring ret = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); strRunAppParam = JniHelper::jstring2string(ret); minfo.env->DeleteLocalRef(ret); minfo.env->DeleteLocalRef(minfo.classID); } #endif return strRunAppParam; } //打开链接 void openURL(const std::string &url) { showWebView(url.c_str()); } std::string getSystemTime() { time_t t = time(NULL); struct tm* tt = localtime(&t); char timeString[16] = { 0 }; sprintf(timeString, "%02d:%02d", tt->tm_hour, tt->tm_min); return std::string(timeString); } std::string getDataTime() { time_t t = time(NULL); struct tm* tt = localtime(&t); char timeString[32] = { 0 }; sprintf(timeString, "%04d-%02d-%02d %02d:%02d", tt->tm_year+1900, tt->tm_mon+1, tt->tm_mday, tt->tm_hour, tt->tm_min); return std::string(timeString); } long long getCurrTime() { //return time(NULL); struct timeval tv; gettimeofday(&tv, nullptr); long long ms = tv.tv_sec; return ms * 1000 + tv.tv_usec / 1000; } void startLocation() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "startLocation", "()V"); if (isHave) { minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID); cocos2d::log("JniFun call startLocation over!"); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call startLocation error!"); } #endif } void stopLocation() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; bool isHave = JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "stopLocation", "()V"); if (isHave) { minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID); cocos2d::log("JniFun call stopLocation over!"); minfo.env->DeleteLocalRef(minfo.classID); } else { cocos2d::log("JniFun call stopLocation error!"); } #endif } double distanceBetween(double aLatitude, double aLongitude, double bLatitude, double bLongitude) { double dDistance = 0.f; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; if (JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "distanceBetween", "(DDDD)D")) { dDistance = minfo.env->CallStaticIntMethod(minfo.classID, minfo.methodID, aLatitude, aLongitude, bLatitude, bLongitude); minfo.env->DeleteLocalRef(minfo.classID); } #endif return dDistance; } bool decodeAndSaveImage(const char* data, int dataLen, const char* savePath) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; if (JniHelper::getStaticMethodInfo(minfo, JAVA_CLASSNAME, "decodeAndSaveImage", "([BLjava/lang/String;)Z")) { jbyteArray jBytes = minfo.env->NewByteArray(dataLen); minfo.env->SetByteArrayRegion(jBytes, 0, dataLen, (const jbyte*)data); jstring jSavePath = minfo.env->NewStringUTF(savePath); jboolean result = minfo.env->CallStaticBooleanMethod(minfo.classID, minfo.methodID, jBytes, jSavePath); minfo.env->DeleteLocalRef(jBytes); minfo.env->DeleteLocalRef(jSavePath); minfo.env->DeleteLocalRef(minfo.classID); cocos2d::log("JniFun::decodeAndSaveImage result=%d path=%s", (int)result, savePath); return (bool)result; } else { cocos2d::log("JniFun::decodeAndSaveImage JNI method not found!"); } #endif return false; } }