[SOLVED] Call Java method from native code

[SOLVED] Call Java method from native code
0.0 0


I need call a java method, which open a link in the Google Play or browser.

In my java class (biz/ideus/puzzler/Puzzler.java) which extended cocos2dxActivity i added this code:

public void openMarket() {
        Intent anIntent = new Intent(Intent.ACTION_VIEW);
        final String appName = "com.example";
        try {
            anIntent.setData(Uri.parse("market://details?id=" + appName));
        } catch (android.content.ActivityNotFoundException anfe) {
                            + appName));

    public native void callOpenMarketMethod();

Next, using javah made a header file (biz_ideus_puzzler_Puzzler.h);

in biz_ideus_puzzler_Puzzler.cpp i implemented my method:

JNIEXPORT void JNICALL Java_biz_ideus_puzzler_Puzzler_callOpenMarketMethod(JNIEnv* env, jobject  obj) {
    jclass clazz = env->FindClass("biz/ideus/puzzler/Puzzler");
    jmethodID openMarket = env->GetMethodID(clazz, "openMarket", "()V");
    env->CallObjectMethod(obj, openMarket);

But i have trouble with next step. How add this method to libgame (or maybe i need make new Android.mk for this module)? And how can i call this method from my game?


UPDATE: i solved this

Ok, i made a Android.mk for my module and added it to libgame Android.mk. Now i can call the method from native code in this way: In OnCreate method i add call native method callOpenMarketMethod(), which call a method openMarket() (from java class). But how can i call callOpenMarketMethod() from c++ code? For example: i have menuItemImage *buy with menu_selector buyGame, and when i press to “buy” button, application must open Android Play.