`
Darar
  • 浏览: 87834 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android ndk native_activity.h

 
阅读更多
#ifndef ANDROID_NATIVE_ACTIVITY_H
#define ANDROID_NATIVE_ACTIVITY_H

#include <stdint.h>
#include <sys/types.h>

#include <jni.h>

#include <android/asset_manager.h>
#include <android/input.h>
#include <android/native_window.h>

#ifdef __cplusplus
extern "C" {
#endif

struct ANativeActivityCallbacks;

/**
* This structure defines the native side of an android.app.NativeActivity.
* 该结构体定义了本地端的 android.app.NativeActivity。
*
* It is created by the framework,
* and handed to the application's native code as it is being launched.
* 它是由框架创建且在应用程序的本地代码正在启动时交给它。
*/
typedef struct ANativeActivity {
    /**
     * Pointer to the callback function table of the native application.
     * 指向本地应用程序的回调函数表的指针。
     *
     * You can set the functions here to your own callbacks.
     * 你可以在这里设置你自己的回调函数。
     *
     * The callbacks pointer itself here should not be changed;
     * 回调指针自身在这里将不会改变;
     *
     * it is allocated and managed for you by the framework.
     * 它是由框架为你分配和管理。
     */
    struct ANativeActivityCallbacks* callbacks;

    /**
     * The global handle on the process's Java VM.
     * 进程的 Java 虚拟机全局句柄。
     */
    JavaVM* vm;

    /**
     * JNI context for the main thread of the app.
     * 应用程序主线程用 JNI 环境。
     *
     * Note that this field can ONLY be used from the main thread of the process;
     * 注意这个成员变量仅可以被进程的主线程使用;
     *
     * that is, the thread that calls into the ANativeActivityCallbacks.
     * 即,进程的主线程调用进入到 ANativeActivityCallbacks 里指向的某个回调函数中。
     */
    JNIEnv* env;

    /**
     * The NativeActivity Java class.
     * android.app.NativeActivity Java 类。
     */
    jobject clazz;

    /**
     * Path to this application's internal data directory.
     * 本应用程序的内部数据目录路径。
     */
    const char* internalDataPath;
   
    /**
     * Path to this application's external (removable/mountable) data directory.
     * 本应用程序的外部(可移除的或可安装的)数据目录路径。
     */
    const char* externalDataPath;
   
    /**
     * The platform's SDK version code.
     * 平台的 SDK 版本代码。
     */
    int32_t sdkVersion;
   
    /**
     * This is the native instance of the application.
     * 这是应用程序的本地实例。
     *
     * It is not used by the framework,
     * but can be set by the application to its own instance state.
     * 它是不能被框架使用,但是可被应用程序来设置它自己的实例状态。
     * 注:参见 native_app_glue/android_native_app_glue.c 文件中的
     *   onDestroy 函数体的使用来看,该成员变量是用来指向开发人员自定义的数据结构体。
     */
    void* instance;

    /**
     * Pointer to the Asset Manager instance for the application.
     * 指向应用程序资产管理器实例的指针。
     *
     * The application uses this to access binary assets bundled inside its own .apk file.
     * 应用程序使用它来访问包在它自己 .apk 文件里面的二进制资产。
     */
    AAssetManager* assetManager;
} ANativeActivity;

/**
* These are the callbacks the framework makes into a native application.
* 这些回调使得框架进入一个本地应用程序。
*
* All of these callbacks happen on the main thread of the application.
* 这些回调全部发生在应用程序的主线程上。
*
* By default, all callbacks are NULL;
* 默认情况下,全部回调函数指针都是 NULL 值 ;
* set to a pointer to your own function to have it called.
* 设置回调函数指针值为你自己的允许被调用的函数地址。
*/
typedef struct ANativeActivityCallbacks {
    /**
     * NativeActivity has started.
     * NativeActivity 已经启动。
     *
     * See Java documentation for Activity.onStart() for more information.
     * 参见 Java 文档中的 Activity.onStart 方法的更多信息。
     * 注:该方法说明了将要显示给用户的活动。
     */
    void (*onStart)( ANativeActivity* activity );
   
    /**
     * NativeActivity has resumed.
     * NativeActivity 已经(中断后)继续。
     *
     * See Java documentation for Activity.onResume() for more information.
     * 参见 Java 文档中的 Activity.onResume 方法的更多信息。
     * 注:用户可以开始与活动进行交互时会调用该方法。这个方法非常适合开始播放动画和音乐。
     */
    void (*onResume)( ANativeActivity* activity );
   
    /**
     * Framework is asking NativeActivity to save its current instance state.
     * 框架请求 NativeActivity 去保存它的当前实例状态。
     *
     * See Java documentation for Activity.onSaveInstanceState() for more information.
     * 参见 Java 文档中的 Activity.onSaveInstanceState 方法的更多信息。
     * 注:Android调用该方法的作用是让活动可以保存每个实例的状态,如光标在文本字段中的位置。
     *
     * The returned pointer needs to be created with malloc();
     * 返回的指针是用 malloc 函数创建的;
     *
     * the framework will call free() on it for you.
     * 框架将为你调用 free 函数释放它。
     *
     * You also must fill in outSize with the number of bytes in the allocation.
     * 你同样必须用 outSize 传出参数来得到分配的字节数量。
     *
     * Note that the saved state will be persisted,
     * so it can not contain any active entities (pointers to memory, file descriptors, etc).
     * 注意已保存的状态将是持久的,所以它不能包含任何活动的实体(指向内存的指针值、文件描述符,等等)。
     */
    void* (*onSaveInstanceState)( ANativeActivity* activity,
                                  size_t*          outSize );
   
    /**
     * NativeActivity has paused.
     * NativeActivity 已经暂停。
     *
     * See Java documentation for Activity.onPause() for more information.
     * 参见 Java 文档中的 Activity.onPause 方法的更多信息。
     * 注:活动将要进入后台时会运行该方法,活动进入后台的原因通常是在前台启动了另一个活动。
     *   还应该在该方法中保存程序的持久性状态,如正在编辑的数据库记录。
     */
    void (*onPause)( ANativeActivity* activity );
   
    /**
     * NativeActivity has stopped.
     * NativeActivity 已经停止。
     *
     * See Java documentation for Activity.onStop() for more information.
     * 参见 Java 文档中的 Activity.onStop 方法的更多信息。
     * 注:用户无需看到某个活动,或者在一段时间内不需要某个活动时,可以调用该方法。
     *   如果内存不足,可能永远都不会调用该方法,系统可能只是终止进程。
     */
    void (*onStop)( ANativeActivity* activity );
   
    /**
     * NativeActivity is being destroyed.
     * NativeActivity 正在被销毁。
     *
     * See Java documentation for Activity.onDestroy() for more information.
     * 参见 Java 文档中的 Activity.onDestroy 方法的更多信息。
     * 注:销毁活动前会调用该方法。
     *   如果内存不足,可能永远都不会调用该方法,系统可能只是终止进程。
     */
    void (*onDestroy)( ANativeActivity* activity );

    /**
     * Focus has changed in this NativeActivity's window.
     * 在这个 NativeActivity 的窗口里焦点已经改变。
     * This is often used, for example, to pause a game when it loses input focus.
     * 这是常常被使用的,例如,当一个游戏失去输入焦点时暂停。
     */
    void (*onWindowFocusChanged)( ANativeActivity* activity,
                                  int              hasFocus );
   
    /**
     * The drawing window for this native activity has been created.
     * 这个本地活动的绘图窗口已经创建完成。
     *
     * You can use the given native window object to start drawing.
     * 你可以使用给定的本地窗口对象开始绘图。
     */
    void (*onNativeWindowCreated)( ANativeActivity* activity,
                                   ANativeWindow*   window );

    /**
     * The drawing window for this native activity has been resized.
     * 这个本地活动的绘图窗口调整大小已完成。
     *
     * You should retrieve the new size from the window and ensure
     * that your rendering in it now matches.
     * 你将重新得到窗口的新大小并且确保现在你的绘制与它相一致。
     */
    void (*onNativeWindowResized)( ANativeActivity* activity,
                                   ANativeWindow*   window );

    /**
     * The drawing window for this native activity needs to be redrawn.
     * 这个本地活动的绘图窗口需要重绘。
     *
     * To avoid transient artifacts during screen changes (such resizing after rotation),
     * applications should not return from this function
     * until they have finished drawing their window in its current state.
     * 为预防短暂的伪像在屏幕改变期间(在屏幕旋转(横变纵或纵变横)之后调整大小),
     * 应用程序将不能从这个函数返回直到它们用它们的当前状态绘画完成它们的窗口。
     */
    void (*onNativeWindowRedrawNeeded)( ANativeActivity* activity,
                                        ANativeWindow*   window );

    /**
     * The drawing window for this native activity is going to be destroyed.
     * 这个本地活动的绘图窗口将被销毁。
     *
     * You MUST ensure
     * that you do not touch the window object after returning from this function:
     * 你必须确保在从这个函数返回后你触摸不了窗口对象了:
     *
     * in the common case of drawing to the window from another thread,
     * that means the implementation of this callback must properly synchronize
     * with the other thread to stop its drawing before returning from here.
     * 在由其它线程绘画窗口的通常情况中,
     * 那意味着从这里返回之前该回调的实现必须正确地与其它线程停止它的绘画同步。
     */
    void (*onNativeWindowDestroyed)( ANativeActivity* activity,
                                     ANativeWindow*   window );
   
    /**
     * The input queue for this native activity's window has been created.
     * 这个本地活动窗口的输入队列已经创建完成。
     *
     * You can use the given input queue to start retrieving input events.
     * 你可以使用给定的输入队列开始检索输入事件。
     */
    void (*onInputQueueCreated)( ANativeActivity* activity,
                                 AInputQueue*     queue );
   
    /**
     * The input queue for this native activity's window is being destroyed.
     * 这个本地活动窗口的输入队列是正在被销毁。
     *
     * You should no longer try to reference this object upon returning from this function.
     * 从该函数返回后你将不能再试图引用这个对象了。
     */
    void (*onInputQueueDestroyed)( ANativeActivity* activity,
                                   AInputQueue*     queue );

    /**
     * The rectangle in the window in which content should be placed has changed.
     * 在窗口中将放置发生改变内容的矩形。
     */
    void (*onContentRectChanged)( ANativeActivity* activity,
                                  const ARect*     rect );

    /**
     * The current device AConfiguration has changed.
     * 当前设备的 AConfiguration 结构信息已发生改变。
     * 注:AConfiguration 结构在 configuration.h 中声明。
     *
     * The new configuration can be retrieved from assetManager.
     * 新的配置可以从 activity->assetManager 中重新得到。
     * 注:用 configuration.h 中声明的 AConfiguration_fromAssetManager 函数来重新得到。
     */
    void (*onConfigurationChanged)( ANativeActivity* activity );

    /**
     * The system is running low on memory.
     * 该系统运行内存不足。
     *
     * Use this callback to release resources you do not need,
     * to help the system avoid killing more important processes.
     * 使用这个回调去释放你不需要的资源,以帮助系统避免杀死更多重要的进程。
     */
    void (*onLowMemory)( ANativeActivity* activity );
} ANativeActivityCallbacks;

/**
* This is the function that
* must be in the native code to instantiate the application's native activity.
* 这是必须用本地代码来实例化应用程序的本地活动的函数。
*
* It is called with the activity instance (see above);
* 它是被活动实例调用的(参见上文);
*
* if the code is being instantiated from a previously saved instance,
* the savedState will be non-NULL and point to the saved data.
* 如果代码正在实例化之前保存的实例,那么 savedState 参数值将是非 NULL 值并且指向已保存的数据。
*
* You must make any copy of this data you need --
* it will be released after you return from this function.
* 你必须备份 savedState 参数指向数据中你需要的数据 -- 它将在这个函数返回后被框架释放。
* 注:savedState 和 savedStateSize 参数取值来源,
*   可参见上面 ANativeActivityCallbacks 结构体中的 onSaveInstanceState 回调函数指针。
*/
typedef void
ANativeActivity_createFunc( ANativeActivity* activity,
                            void*            savedState,
                            size_t           savedStateSize );

/**
* The name of the function that Native Instance looks for when launching its native code.
* 在本地实例启动它的本地代码时寻找这个函数名。
*
* This is the default function that is used,
* you can specify "android.app.func_name" string meta-data in
* your manifest to use a different function.
* 这是被用作默认函数,你可以在你的 AndroidManifest.xml 里的 activity 标签下用
* <meta-data android:name="android.app.func_name" android:value="替换的函数名" />
* 来明确说明使用一个不同的函数名。
* 注:别忘了在你的代码中定义实现一个如下函数:
* void ANativeActivity_onCreate( ANativeActivity* activity,
*                                void*            savedState,
*                                size_t           savedStateSize )
* {
*      ... // 具体实现语句
* }
*/
extern ANativeActivity_createFunc ANativeActivity_onCreate;

/**
* Finish the given activity.
* 结束给定的活动。
*
* Its finish() method will be called,
* causing it to be stopped and destroyed.
* android.app.Activity 类的 finish 方法将是被调用,导致活动停止且销毁。
*
* Note that this method can be called from *any* thread;
* 注意该方法可以是被任何线程调用;
*
* it will send a message to the main thread of the process
* where the Java finish call will take place.
* 在 Java 结束调用将发生的地方,它将发送一个消息到进程的主线程。
*/
void
ANativeActivity_finish( ANativeActivity* activity );

/**
* Change the window format of the given activity.
* 改变给定活动的窗口格式(即一个窗口可以使用的像素格式)。
* 注:参见 native_window.h 中的以 WINDOW_FORMAT_ 开头的宏值定义。
*
* Calls getWindow().setFormat() of the given activity.
* 调用给定活动的 getWindow().setFormat() 。
* 注:先调用 android.app.Activity 类对象的 getWindow 方法得到 android.view.Window 类对象,
*   再调用 android.view.Window 类对象的 setFormat 方法设置窗口的格式。
*
* Note that this method can be called from *any* thread;
* 注意该方法可以是被任何线程调用;
*
* it will send a message to the main thread of the process
* where the Java finish call will take place.
* 在 Java 结束调用将发生的地方,它将发送一个消息到进程的主线程。
*/
void
ANativeActivity_setWindowFormat( ANativeActivity* activity,
                                 int32_t          format );

/**
* Change the window flags of the given activity.
* 改变给定活动的窗口标志。
*
* Calls getWindow().setFlags() of the given activity.
* 调用给定活动的 getWindow().setFlags() 。
*
* Note that this method can be called from *any* thread;
* 注意该方法可以是被任何线程调用;
*
* it will send a message to the main thread of the process
* where the Java finish call will take place.
* 在 Java 结束调用将发生的地方,它将发送一个消息到进程的主线程。
*
* See window.h for flag constants.
* 参见 window.h 中的标志常量。
*/
void
ANativeActivity_setWindowFlags( ANativeActivity* activity,
                                uint32_t         addFlags,
                                uint32_t         removeFlags );

/**
* Flags for ANativeActivity_showSoftInput;
* ANativeActivity_showSoftInput 函数的 flags 参数用的标志;
*
* see the Java InputMethodManager API for documentation.
* 参见 Java 中关于 android.view.inputmethod.InputMethodManager API 的文档。
*/
enum {
    /* Flag for showSoftInput(View, int) to indicate
     * that this is an implicit request to show the input window,
     * not as the result of a direct request by the user.
     * 该标志为 ANativeActivity_showSoftInput 函数的 flags 参数的取值之一,
     * 指出这是一个显示输入法窗口不明确的请求,不作为用户直接请求的结果。
     *
     * The window may not be shown in this case.
     * 在这种情况中输入法窗口可以不显示。
     */
    ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT = 0x0001,

    /* Flag for showSoftInput(View, int) to indicate
     * that the user has forced the input method open
     * (such as by long-pressing menu)
     * so it should not be closed until they explicitly do so.
     * 该标志为 ANativeActivity_showSoftInput 函数的 flags 参数的取值之一,
     * 指出用户强制打开输入法(例如通过长按菜单键),因此它将一直保持打开直到用户明确关闭。
     */
    ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED = 0x0002,
};

/**
* Show the IME while in the given activity.
* 在给定活动里显示输入法。
*
* Calls InputMethodManager.showSoftInput() for the given activity.
* 为给定活动调用 android.view.inputmethod.InputMethodManager 类对象的 showSoftInput 方法。
*
* Note that this method can be called from *any* thread;
* 注意该方法可以是被任何线程调用;
*
* it will send a message to the main thread of the process
* where the Java finish call will take place.
* 在 Java 结束调用将发生的地方,它将发送一个消息到进程的主线程。
*/
void
ANativeActivity_showSoftInput( ANativeActivity* activity,
                               uint32_t         flags );

/**
* Flags for ANativeActivity_hideSoftInput;
* ANativeActivity_hideSoftInput 函数的 flags 参数用的标志;
*
* see the Java InputMethodManager API for documentation.
* 参见 Java 中关于 android.view.inputmethod.InputMethodManager API 的文档。
*/
enum {
    /* Flag for hideSoftInputFromWindow(IBinder, int) to indicate
     * that the soft input window should only be hidden
     * if it was not explicitly shown by the user.
     * 该标志为 ANativeActivity_hideSoftInput 函数的 flags 参数的取值之一,
     * 指出如果用户没有明确地要显示输入法窗口的话,则隐藏它。
     */
    ANATIVEACTIVITY_HIDE_SOFT_INPUT_IMPLICIT_ONLY = 0x0001,

    /* Flag for hideSoftInputFromWindow(IBinder, int) to indicate
     * that the soft input window should normally be hidden,
     * unless it was originally shown with SHOW_FORCED.
     * 该标志为 ANativeActivity_hideSoftInput 函数的 flags 参数的取值之一,
     * 指出输入法窗口通常是隐藏的,
     * 除非它起初是用 ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED 标志显示的。
     */
    ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS = 0x0002,
};

/**
* Hide the IME while in the given activity.
* 在给定活动里隐藏输入法。
*
* Calls InputMethodManager.hideSoftInput() for the given activity.
* 为给定活动调用 android.view.inputmethod.InputMethodManager 类对象的 hideSoftInput 方法。
*
* Note that this method can be called from *any* thread;
* 注意该方法可以是被任何线程调用;
*
* it will send a message to the main thread of the process
* where the Java finish call will take place.
* 在 Java 结束调用将发生的地方,它将发送一个消息到进程的主线程。
*/
void
ANativeActivity_hideSoftInput( ANativeActivity* activity,
                               uint32_t         flags );

#ifdef __cplusplus
};
#endif

#endif // ANDROID_NATIVE_ACTIVITY_H
分享到:
评论

相关推荐

    android_app_NativeActivity.rar_NativeActivity_android

    标题中的“android_app_NativeActivity.rar_NativeActivity_android”表明我们正在探讨与Android应用开发相关的主题,特别是涉及到NativeActivity。NativeActivity是Android系统提供的一种特殊类型的Activity,允许...

    NDK_2_3_1.zip

    1. **新建项目**:打开Android Studio,选择“New Project”,在模板中选择“Empty Activity”,并勾选“Include C++ Support”选项,这将为项目自动配置NDK支持。 2. **配置build.gradle**:在app模块的build....

    ndk_launcher.zip学习

    【标题】"ndk_launcher.zip学习"涉及到的核心知识点是Android Native Development Kit (NDK)的使用,特别是关于创建和配置一个原生应用启动器的过程。NDK是Google为Android平台提供的一个工具集,允许开发者使用C/...

    Android_JNI_实例.doc

    $(JNI_H_INCLUDE) \ $(LOCAL_PATH) LOCAL_SHARED_LIBRARIES := \ libutils LOCAL_PRELINK_MODULE := false LOCAL_MODULE := libhello LOCAL_CFLAGS += -DHAVE_LOG include $(BUILD_SHARED_LIBRARY) ``` 5. ...

    native_libyuv.zip

    Java_com_example_NativeActivity_convertRGBAtoNV21(JNIEnv *env, jobject /* this */, jintArray rgba, jbyteArray nv21, jint width, jint height) { // 获取输入和输出缓冲区 jint *rgbaPtr = env-&gt;...

    OpenCV_native.rar

    public class NativeActivity extends AppCompatActivity { static { System.loadLibrary("opencv"); System.loadLibrary("native-lib"); } public native void processImage(Bitmap bitmap); @Override ...

    android面试题_Android.rar

    9. **NDK与JNI**:了解Native层编程,如何使用JNI进行C/C++代码的调用,以及NDK的优点和应用场景。 10. **异构组件化与模块化**:现代Android应用的架构设计,如MVP、MVVM模式,以及如何实现组件化和模块化的项目...

    android native activity项目

    在创建Native Activity时,需要定义一个Android的四大组件之一——Activity,并在AndroidManifest.xml中声明为“android.app.NativeActivity”。 项目中的"HelloNativeActivity"可能包含以下几个关键部分: 1. **...

    ndk_r7_windows使用入门

    对于Android开发者而言,掌握如何使用Android NDK进行开发是一项非常重要的技能。本文将详细介绍如何使用Android NDK r7版本在Windows环境下进行本地C文件的编译,解决在编译过程中可能出现的各种问题。 #### 二、...

    meiti.rar_andriod.proess_andriod.proess.mak_android_meiti

    Android.mk是Android NDK(Native Development Kit)的一部分,用于构建原生库和可执行文件,而CMakeLists.txt是更现代的跨平台构建系统,现在被官方推荐使用。 "meiti"可能是应用的名字或者是与音乐播放相关的组件...

    Android-System-Development_JAVA.pdf.zip_android

    11. **Android NDK**:对于性能要求高的应用,可以使用NDK进行原生C/C++代码开发,实现JNI(Java Native Interface),以利用硬件加速或其他底层功能。 12. **Gradle构建系统**:Gradle用于自动化构建过程,包括...

    native-service-master.zip_Android Native_android_android service

    在Android中,开发者通过JNI调用C/C++代码,实现Native Service的功能,并与Java层的Activity或Service进行通信。 **3. 创建Native Service** 创建Native Service首先需要在Java层定义一个Service类,声明相关的...

    UI_src.rar

    在C语言中实现Android的`Activity`并不常见,因为通常使用Java或Kotlin,但有可能是使用NDK(Native Development Kit)进行原生代码开发。 3. **主文件(Main)**:`main.c`通常是程序的入口点,负责初始化和运行...

    ndk_c_demo3.30.img.ok.AndroidStudio

    NDK(Native Development Kit)是Google提供的一组工具,允许开发者使用C和C++编写Android应用的一部分,以提升性能或者利用特定硬件功能。 【描述】指出开发者已经成功地用NDK编译了C++代码,并且与OpenCV库进行了...

    Android_NDK_JNI

    首先,NDK(Native Development Kit)是Android开发工具的一部分,它提供了一套工具,使得开发者可以在Android平台上编译、构建和运行原生代码。NDK的主要用途包括:性能敏感的应用部分(如游戏引擎)、使用C/C++库...

    android ndk开发实例,不用java代码写出activity

    通常,我们使用Java或Kotlin来创建Activity,但在NDK环境中,我们可以使用Android的原生Activity,即`android.app.NativeActivity`,它允许我们直接用C/C++代码处理UI和事件。 创建一个NDK项目,你需要以下步骤: ...

    sources-25_r01.zip

    6. **JNI与Native代码**:了解Java Native Interface(JNI)如何与C/C++代码交互,以及Android的NDK开发。 7. **Android性能优化**:通过源码分析,找出性能瓶颈,进行内存管理和CPU优化。 8. **Android安全**:...

    Mastering Android NDK(pdf+epub+mobi+code_files).zip

    《Mastering Android NDK》是一本深度探讨Android原生开发工具包(NDK)的专业书籍,包含PDF、EPUB、MOBI三种电子书格式以及配套的代码文件。这本书旨在帮助开发者充分利用C和C++在Android平台上开发高性能的应用...

    android ndk jni so库生成

    NDK(Native Development Kit)是Google提供的一套工具集,用于让开发者在Android应用中使用C/C++代码,实现性能优化或者调用特定硬件功能。本篇我们将深入探讨如何使用Android NDK和JNI来生成本地动态链接库(so库)。 ...

    android ndk开发步骤和实例

    Android NDK(Native Development Kit)是Google为Android平台提供的一套工具集,它允许开发者使用C、C++等原生代码来编写部分应用程序,从而利用这些低级语言的高性能和灵活性。JNI(Java Native Interface)是Java...

Global site tag (gtag.js) - Google Analytics