cocos2d中间层调用Android(三)

前端之家收集整理的这篇文章主要介绍了cocos2d中间层调用Android(三)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ios通过.cpp文件进行调用AppActivity.java的方法

js_bindings_lcm_api.cpp:

//
//  js_bindings_lcm_api.cpp
//  MoonWarriors
//
//  Created by xiaohei on 2/21/17.
//
//

#include "js_bindings_lcm_api.h"

#ifdef ANDROID
#include <jni/JniHelper.h>
#endif

using namespace LCMJSB;
using namespace cocos2d;

//LCMAPI* LCMAPI::instance_ = nullptr;

LCMAPI* LCMAPI::getInstance()
{
    static LCMAPI instance_;
    return std::addressof(instance_);
}

LCMAPI::LCMAPI()
{  
}

LCMAPI::~LCMAPI()
{
}

void LCMAPI::continueProcessing()
{
#ifdef ANDROID
    JniMethodInfo minfo;
    bool find = JniHelper::getStaticMethodInfo(minfo,"org/cocos2dx/javascript/AppActivity","continueProcessing","()V");
    if (find) {
        minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID);
        minfo.env->DeleteLocalRef(minfo.classID);
    }
#endif
}

std::string LCMAPI::getLTotalBalance()
{
    std::string ret = "";
#ifdef ANDROID
    JniMethodInfo minfo;
    bool find = JniHelper::getStaticMethodInfo(minfo,"getLTotalBalance","()Ljava/lang/String;");
    if (find) {
        jstring jret = (jstring)minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID);
        ret = JniHelper::jstring2string(jret);
        minfo.env->DeleteLocalRef(jret);
        minfo.env->DeleteLocalRef(minfo.classID);
    }
#endif
    return ret;
}

bool LCMAPI::getLIsShowBalance()
{
    bool ret = false;
#ifdef ANDROID
    JniMethodInfo minfo;
    bool find = JniHelper::getStaticMethodInfo(minfo,"getLIsShowBalance","()Z");
    if (find) {
        jboolean jret = (jboolean)minfo.env->CallStaticBooleanMethod(minfo.classID,minfo.methodID);
        ret = (bool) jret;
        minfo.env->DeleteLocalRef(minfo.classID);
    }
#endif
    return ret;
}

void LCMAPI::lcmNotifyEveryday(int hour,int min,std::string title,std::string txt,std::string icon,int code)
{
#ifdef ANDROID
    JniMethodInfo minfo;
    bool find = JniHelper::getStaticMethodInfo(minfo,"lcmNotifyEveryday","(IILjava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
    if (find) {
        jint jhour = (jint)hour;
        jint jmin = (jint)min;
        jint jcode = (jint)code;
        jstring jtitle = minfo.env->NewStringUTF(title.c_str());  
        jstring jtxt = minfo.env->NewStringUTF(txt.c_str());
        jstring jicon = minfo.env->NewStringUTF(icon.c_str());
        minfo.env->CallStaticVoidMethod(minfo.classID,minfo.methodID,jhour,jmin,jtitle,jtxt,jicon,jcode);
        minfo.env->DeleteLocalRef(jtitle);
        minfo.env->DeleteLocalRef(jtxt);
        minfo.env->DeleteLocalRef(jicon);
        minfo.env->DeleteLocalRef(minfo.classID);
    }
#endif
}
注意传入的参数和返回值的表示,注意转换过程

猜你在找的Cocos2d-x相关文章