在代码身上,用心去飞翔

你是我的小啊小code,怎么爱你都不嫌多

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, "org/cocos2dx/javascript/AppActivity", "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, "org/cocos2dx/javascript/AppActivity", "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, "org/cocos2dx/javascript/AppActivity", "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
}

注意传入的参数和返回值的表示,注意转换过程
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhenyu5211314/article/details/52367157
文章标签: cocos2d-x android
个人分类: Js
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭