Wiki Page Content

Differences between revisions 1 and 2
Revision 1 as of 2013-11-16 18:09:05
Size: 1692
Editor: Julien
Comment: Initial documentation of the function
Revision 2 as of 2013-12-07 13:21:50
Size: 1699
Comment: Changed return type, see SDL_system.h, real type is in description.
Deletions are marked like this. Additions are marked like this.
Line 13: Line 13:
JNIEnv* SDL_AndroidGetJNIEnv(void) void* SDL_AndroidGetJNIEnv(void)
Line 17: Line 17:
Returns a pointer to Java native interface object to which the current thread is attached, or 0 on error. Returns a pointer to Java native interface object (JNIEnv) to which the current thread is attached, or 0 on error.

DRAFT

SDL_AndroidGetJNIEnv

Use this function to retrieve the Java native interface object (JNIEnv) of the current thread on Android builds.

Syntax

void* SDL_AndroidGetJNIEnv(void)

Return Value

Returns a pointer to Java native interface object (JNIEnv) to which the current thread is attached, or 0 on error.

Code Examples

// Calls the void MyActivity.showHome() method of the Java instance of MyActivity.
void showHome(void)
{
    // retrieve the JNI environment.
    JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();

    // retrieve the Java instance of the SDLActivity
    jobject activity = (jobject)SDL_AndroidGetActivity();

    // find the Java class of the activity. It should be SDLActivity or a subclass of it.
    jclass clazz( env->GetObjectClass(activity) );

    // find the identifier of the method to call
    jmethodID method_id = env->GetMethodID( clazz, "showHome", "()V" );

    // effectively call the Java method
    env->CallVoidMethod( activity, method_id );
    
    // clean up the local references.
    env->DeleteLocalRef(activity);
}

Remarks

The prototype of the function in SDL's code actually declare a void* return type, even if the implementation returns a pointer to a JNIEnv. The rationale being that it allows not to include jni.h in the headers of the SDL.

Version

This function is available in SDL 2.0.0


CategoryAPI, CategoryHeader

None: SDL_AndroidGetJNIEnv (last edited 2016-11-20 22:03:47 by PhilippWiesemann)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit