Tuesday, April 23, 2013

jni jnienv CallStaticBooleanMethod example c c++ java



CallStatic<type>Method Routines - 
CallStaticBooleanMethod
CallStatic<type>MethodA Routines
CallStatic<type>MethodV Routines

NativeType CallStatic<type>Method(JNIEnv *env, jclass clazz,
jmethodID methodID, ...);

NativeType CallStatic<type>MethodA(JNIEnv *env, jclass clazz,
jmethodID methodID, jvalue *args);

NativeType CallStatic<type>MethodV(JNIEnv *env, jclass clazz,
jmethodID methodID, va_list args);

(CallStaticBooleanMethod)This family of operations invokes a static method on a Java object, according to the specified method ID. The methodID argument must be obtained by calling GetStaticMethodID().

The method ID must be derived from clazz, not from one of its superclasses.
(CallStaticBooleanMethod)
CallStatic<type>Method Routines
Programmers should place all arguments that are to be passed to the method immediately following the methodID argument. TheCallStatic<type>Method routine accepts these arguments and passes them to the Java method that the programmer wishes to invoke. (CallStaticBooleanMethod)
CallStatic<type>MethodA Routines
Programmers should place all arguments to the method in an args array of jvalues that immediately follows the methodIDargument. The CallStaticMethodA routine accepts the arguments in this array, and, in turn, passes them to the Java method that the programmer wishes to invoke.
CallStatic<type>MethodV Routines
Programmers should place all arguments to the method in an args argument of type va_list that immediately follows the methodIDargument. The CallStaticMethodV routine accepts the arguments, and, in turn, passes them to the Java method that the programmer wishes to invoke.

The following table describes each of the method calling routines according to their result types. You should replace type inCallStatic<type>Method with the Java type of the method, or one of the actual method calling routine names from the table, and replace NativeType with the corresponding native type for that routine.

Table 4-7a CallStatic<type>Method Calling Routines
CallStatic<type>Method Routine NameNative Type
CallStaticVoidMethod()
CallStaticVoidMethodA()
CallStaticVoidMethodV()

void
CallStaticObjectMethod()
CallStaticObjectMethodA()
CallStaticObjectMethodV()

jobject
CallStaticBooleanMethod()
CallStaticBooleanMethodA()
CallStaticBooleanMethodV()

jboolean
CallStaticByteMethod()
CallStaticByteMethodA()
CallStaticByteMethodV()

jbyte
CallStaticCharMethod()
CallStaticCharMethodA()
CallStaticCharMethodV()

jchar
CallStaticShortMethod()
CallStaticShortMethodA()
CallStaticShortMethodV()

jshort
CallStaticIntMethod()
CallStaticIntMethodA()
CallStaticIntMethodV()

jint
CallStaticLongMethod()
CallStaticLongMethodA()
CallStaticLongMethodV()

jlong
CallStaticFloatMethod()
CallStaticFloatMethodA()
CallStaticFloatMethodV()

jfloat
CallStaticDoubleMethod()
CallStaticDoubleMethodA()
CallStaticDoubleMethodV()

jdouble
LINKAGE:
Indices in the JNIEnv interface function table.
Table 4-7b CallStatic<type>Method Calling Routines
CallStatic<type>Method Routine Name
Index
CallStaticVoidMethod()
CallStaticVoidMethodA()
CallStaticVoidMethodV()
141
143
142
CallStaticObjectMethod()
CallStaticObjectMethodA()
CallStaticObjectMethodV()
114
116
115
CallStaticBooleanMethod()
CallStaticBooleanMethodA()
CallStaticBooleanMethodV()
117
119
118
CallStaticByteMethod()
CallStaticByteMethodA()
CallStaticByteMethodV()
120
122
121
CallStaticCharMethod()
CallStaticCharMethodA()
CallStaticCharMethodV()
123
125
124
CallStaticShortMethod()
CallStaticShortMethodA()
CallStaticShortMethodV()
126
128
127
CallStaticIntMethod()
CallStaticIntMethodA()
CallStaticIntMethodV()
129
131
130
CallStaticLongMethod()
CallStaticLongMethodA()
CallStaticLongMethodV()
132
134
133
CallStaticFloatMethod()
CallStaticFloatMethodA()
CallStaticFloatMethodV()
135
137
136
CallStaticDoubleMethod()
CallStaticDoubleMethodA()
CallStaticDoubleMethodV()
138
140
139
PARAMETERS  of CallStaticBooleanMethod
env: the JNI interface pointer.

clazz: a Java class object.

methodID: a static method ID.
Additional Parameter for CallStatic<type>Method Routines:
arguments to the static method.
Additional Parameter for CallStatic<type>MethodA Routines:
args: an array of arguments.
Additional Parameter for CallStatic<type>MethodV Routines:
args: a va_list of arguments.
RETURNS:
Returns the result of calling the static Java method.
THROWS:
Exceptions raised during the execution of the Java method.
Example of CallStaticBooleanMethod

/**
 * Check if Internet Connection is ONLINE
 */
bool InterfaceJNI::isInternetConnected()
{
    JavaVM* jvm = JniHelper::getJavaVM();
    int status;
    JNIEnv *env;
    jmethodID mid;

    bool isAttached = false;
    bool returnValue = false;

    CCLog("InterfaceJNI isInternetConnected");

    // Get Status
    status = jvm->GetEnv((void **) &env, JNI_VERSION_1_6);

    if(status < 0)
    {
        CCLog("isInternetConnected Status < 0 Failed to get JNI Enviroment!!!");
        status = jvm->AttachCurrentThread(&env, NULL);
        CCLog("isInternetConnected Status 2: %d", status);
        if(status < 0)
        {
            CCLog("isInternetConnected Status < 0 !!!");
            return false;
        }
        isAttached = true;
        CCLog("isInternetConnected Status isAttached: %d", isAttached);
    }

    CCLog("isInternetConnected Status: %d", status);

    CCLog("isInternetConnected Finding Class....");
    jclass mClass = env->FindClass("org/example/SocialNetwork/CCSocialNetwork");

    // Get Static bool isInternetConnection()
    CCLog("isInternetConnected Getting method....");
    mid = env->GetMethodID(mClass, "isInternetConnection", "()Z");
    if (mid == 0)
    {
        CCLog("isInternetConnected FAIL GET METHOD STATIC");
        return false;
    }
    CCLog("isInternetConnected Calling method....");
    // Call bool isInternetConnection()
    jboolean jReturnValue = env->CallStaticBooleanMethod(mClass,mid);
    CCLog("Call done ");
    // Convert value from Java to C++
    returnValue = (bool)jReturnValue;
    CCLog("isInternetConnected Done ");

    if(isAttached)
        jvm->DetachCurrentThread();

    // Change for return value
    return returnValue;
}