简体   繁体   English

Android Cocos2d-x 3.7:如何在cocos2d-x中获取应用程序上下文?

[英]Android Cocos2d-x 3.7: how to get Application Context in cocos2d-x?

I use Android code cocos2dx call encountered a problem. 我在使用Android代码cocos2dx调用时遇到问题。 I will not call in the Android cocos2dx Context, I'm not sure whether there is a problem with the following code, and then the following is the way I need to call in Android , as well as errors such calls occur . 我不会在Android cocos2dx上下文中调用,我不确定以下代码是否存在问题,然后以下是我在Android中调用的方式以及此类调用发生的错误。

void AdmasterConvSDK::AdMasterInitial(std::string appId)
{

    JniMethodInfo methodInfo;
    if (JniHelper::getStaticMethodInfo(methodInfo, "com/admaster/square/api/ConvMobiSDK", "initial",
                                       "(Landroid/content/Context;Ljava/lang/String;)V")) {
        jstring appID = methodInfo.env->NewStringUTF(appId.c_str());

        jclass cl = methodInfo.env->FindClass("org/cocos2dx/lib/Cocos2dxActivity");
        jmethodID methodContext = methodInfo.env->GetStaticMethodID(cl,"getContext","()Landroid/content/Context");
        jobject context = methodInfo.env->CallStaticObjectMethod(cl,methodContext);
        methodInfo.env->CallStaticVoidMethod(methodInfo.classID,methodInfo.methodID,context,appID);

    } else {
        log("ERROR");
    }

}

Android Code: Android代码:

public  static void initial(Context context, String m2id) {
    ConvMobiInstance adjustInstance = ConvMobiSDK.getDefaultInstance();
    adjustInstance.initial(context, m2id);
}

Error: 错误:

08-31 17:03:07.083: A/libc(22452): Fatal signal 11 (SIGSEGV) at 0x0000001c (code=1), thread 22470 (Thread-38567) 08-31 17:03:07.083:A / libc(22452):致命信号11(SIGSEGV)位于0x0000001c(code = 1),线程22470(Thread-38567)

Has been studied for a long time , i hope to have appeared to help answer this question , thanks in advance . 已经研究了很长时间,希望能有出现来帮助回答这个问题,在此先感谢。

you c++ code is run in glThread bug java code need to run in uiThread ,so if you call java method by c++,you need to make you java code run in ui Thread, change your java code: 您的c ++代码在glThread中运行bug Java代码需要在uiThread中运行,因此,如果您通过c ++调用Java方法,则需要使您的Java代码在ui Thread中运行,请更改您的Java代码:

public  static void initial(final Context context, final String m2id) {
    ((Activity)context).runOnUiThread(new Thread(){
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            ConvMobiInstance adjustInstance = ConvMobiSDK.getDefaultInstance();
            adjustInstance.initial(context, m2id);
        }
    });
}

if this function initial() is some initialisation only, then don't call it from c++, instead call it from Cocos2dxActivity.java inside init()/onCreate() function like this 如果此函数initial()仅是一些初始化,那么不要从c ++调用它,而是从init()/ onCreate()函数内部的Cocos2dxActivity.java调用它

ClassName.initial(this, "string_id"); ClassName.initial(this,“ string_id”);

the first argument will give you the application context you can assign it some class variable and use it later. 第一个参数将为您提供应用程序上下文,您可以为其分配一些类变量并在以后使用。

//example class //示例类

public class AdHelper { 公共类AdHelper {

private static Context m_context;

/*
call this function from Cocos2dxActivity.java 
AdsHelper.init(this);
*/
public static void initAds(Context context)
{
    m_context = context;
}

/*
call this from C++ from through JNI
*/
public static void showAds()
{
    ((Activity)m_context).runOnUiThread(new Thread(){
        @Override
        public void run() {

            super.run();
            Adservice adService = Adservice.getDefaultInstance();
            adService.showAds(m_context);
        }
     });

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM