gogoWebsite

Android: How to call a java program through c++: Continue with the previous article

Updated to 2 hours ago

Android: How to call a java program through c++: Continue with the previous article

Device/Engine: Mac (11.6)/cocos

Development Tools: Android Studio (4.1.2)

Development language: c++/java

Development requirements: Save pictures to mobile phone album

The previous article talked about how to save pictures to mobile phone albums, but all codes are written in Java. If the project is mainly based on C++, you need to consider calling Java code through C++.

Part of the code for calling c++ is as follows:

	JniMethodInfo minfo;
    bool isHave = JniHelper::getStaticMethodInfo(minfo,"Package Name/MainActivity","rtnActivity","()L package name/MainActivity;");
    if(!isHave)
    {
        CCLOG("jni: Cannot get singleton");
    }
    else
    {
        CCLOG("jni: Can get singleton");
        jobject jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);  //Get singleton object
        bool isHaveNoStaticMethod = JniHelper::getMethodInfo(minfo,"Package Name/MainActivity","Methods in java", "(parameter)V");  //If the parameters are not of string type, you can write them directly: such as: IF-integer, floating-point type, if it is a string type, you need to write them like this: Ljava/lang/String;
        if (!isHaveNoStaticMethod)
        {
            CCLOG("jni:alertDiaShowt method does not exist");
        }

        else
        {
            CCLOG("jni:alertDiaShow method exists");
            jstring jdata = minfo.env->NewStringUTF(name);
            minfo.env->CallVoidMethod(jobj, minfo.methodID, jdata);
        }
    }
    CCLOG("Jni-java function execution completed");

Just call the method in java at the specified location.

Hope to provide some help to everyone! ! ! If you have any questions, please comment on private messagesWelcome to discuss~