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

0

I use Android code cocos2dx call encountered a problem. 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 .

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:

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)

Has been studied for a long time , i hope to have appeared to help answer this question , thanks in advance .

android
cocos2d-x
android-context
asked on Stack Overflow Aug 25, 2015 by Lily • edited Aug 25, 2015 by Vishwa

2 Answers

0

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:

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);
        }
    });
}
answered on Stack Overflow Aug 26, 2015 by zongchao Xu
0

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

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 {

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);
        }
     });

}

}

answered on Stack Overflow Aug 26, 2015 by Pratik

User contributions licensed under CC BY-SA 3.0