SDL_AndroidGetActivity

Use this function to retrieve the Java instance of the activity class in an Android application. 使用此函数可以在Android程序中获取Activity的JAVA对象实例。

Syntax 语法

  1. void* SDL_AndroidGetActivity(void)

Return Value 返回值

Returns the jobject representing the instance of the Activity class of the Android application, or NULL on error. 如果成功则返回Android应用程序中的以jobject类型的Activity对象实例,出错则返回NULL。 The jobject returned by the function is a local reference and must be released by the caller. See the PushLocalFrame() and PopLocalFrame() or DeleteLocalRef() functions of the Java native interface (in Oracle’s documentation). 由于函数返回的jobject类型是本地映射的,所以如果要释放的话就必须由调用者去释放。

 Code Examples 代码示例

  1. #include "SDL.h"
  2. #include <jni.h>
  3. // This example requires C++ 这个示例需要使用C++
  4. // Calls the void showHome() method of the Java instance of the activity. 调用无效的showHome()方法进行activity的JAVA实例。
  5. void showHome(void)
  6. {
  7. // retrieve the JNI environment.
  8. JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();
  9. // retrieve the Java instance of the SDLActivity
  10. jobject activity = (jobject)SDL_AndroidGetActivity();
  11. // find the Java class of the activity. It should be SDLActivity or a subclass of it.
  12. jclass clazz(env->GetObjectClass(activity));
  13. // find the identifier of the method to call
  14. jmethodID method_id = env->GetMethodID(clazz, "showHome", "()V");
  15. // effectively call the Java method
  16. env->CallVoidMethod(activity, method_id);
  17. // clean up the local references.
  18. env->DeleteLocalRef(activity);
  19. env->DeleteLocalRef(clazz);
  20. // Warning (and discussion of implementation details of SDL for Android):
  21. // Local references are automatically deleted if a native function called
  22. // from Java side returns. For SDL this native function is main() itself.
  23. // Therefore references need to be manually deleted because otherwise the
  24. // references will first be cleaned if main() returns (application exit).
  25. }

Remarks 注意

The prototype of the function in SDL’s code actually declares a void return type, even if the implementation returns a jobject. The rationale being that it allows not to include jni.h in the headers of the SDL. 这个SDL函数原型实际上声明了void的返回类型,甚至返回了jobject。值得注意的是允许SDL不包含jni.h的头文件。

Version 版本

This function is available since SDL 2.0.0. 这个函数来自SDL 2.0.0

Related Functions 相关函数

SDL_AndroidGetJNIEnv


@SixerMe Translated