gpt4 book ai didi

android - 如何使用 Native Activity?可以和传统 Activity 结合吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:32 26 4
gpt4 key购买 nike

我有两个库 (.so),我用 Java 代码加载它们。

然而,有一些特定的操作需要调用 Java(Activity)<->C++(.so files)。

我可以使用 Native Activity 来实现部分功能吗?原生 Activity 是对传统 Activity 的补充,还是我必须选择使用哪种类型的 Activity ?

[编辑]

native activity 可以在 native 代码中处理一组事件。

android-ndk/sources/android/native_app_glue/android_native_app_glue.h

enum {
/**
* Command from main thread: the AInputQueue has changed. Upon processing
* this command, android_app->inputQueue will be updated to the new queue
* (or NULL).
*/
APP_CMD_INPUT_CHANGED,

/**
* Command from main thread: a new ANativeWindow is ready for use. Upon
* receiving this command, android_app->window will contain the new window
* surface.
*/
APP_CMD_INIT_WINDOW,

/**
* Command from main thread: the existing ANativeWindow needs to be
* terminated. Upon receiving this command, android_app->window still
* contains the existing window; after calling android_app_exec_cmd
* it will be set to NULL.
*/
APP_CMD_TERM_WINDOW,

/**
* Command from main thread: the current ANativeWindow has been resized.
* Please redraw with its new size.
*/
APP_CMD_WINDOW_RESIZED,

/**
* Command from main thread: the system needs that the current ANativeWindow
* be redrawn. You should redraw the window before handing this to
* android_app_exec_cmd() in order to avoid transient drawing glitches.
*/
APP_CMD_WINDOW_REDRAW_NEEDED,

/**
* Command from main thread: the content area of the window has changed,
* such as from the soft input window being shown or hidden. You can
* find the new content rect in android_app::contentRect.
*/
APP_CMD_CONTENT_RECT_CHANGED,

/**
* Command from main thread: the app's activity window has gained
* input focus.
*/
APP_CMD_GAINED_FOCUS,

/**
* Command from main thread: the app's activity window has lost
* input focus.
*/
APP_CMD_LOST_FOCUS,

/**
* Command from main thread: the current device configuration has changed.
*/
APP_CMD_CONFIG_CHANGED,

/**
* Command from main thread: the system is running low on memory.
* Try to reduce your memory use.
*/
APP_CMD_LOW_MEMORY,

/**
* Command from main thread: the app's activity has been started.
*/
APP_CMD_START,

/**
* Command from main thread: the app's activity has been resumed.
*/
APP_CMD_RESUME,

/**
* Command from main thread: the app should generate a new saved state
* for itself, to restore from later if needed. If you have saved state,
* allocate it with malloc and place it in android_app.savedState with
* the size in android_app.savedStateSize. The will be freed for you
* later.
*/
APP_CMD_SAVE_STATE,

/**
* Command from main thread: the app's activity has been paused.
*/
APP_CMD_PAUSE,

/**
* Command from main thread: the app's activity has been stopped.
*/
APP_CMD_STOP,

/**
* Command from main thread: the app's activity is being destroyed,
* and waiting for the app thread to clean up and exit before proceeding.
*/
APP_CMD_DESTROY,
};

因为我知道我的部分代码(应该在特定事件后调用)是用 C++ 编写的,所以我认为通过 native Activity 在 C++ 中处理它会更好。然而,我也有代码必须在 Java 中处理事件后调用。

问题是...我能否拥有 Activity 的 native 版本( native 界面),这将帮助我处理一些事件,同时传统的 Java 界面也可以同时用于同一 Activity ?

最佳答案

我会回答你不能有一个 Activity 的两个版本的代码。

  • 您将如何在您的 list 中指定它?

  • 在谷歌提供的示例中,main 的注释非常明确:

It runs in its own thread, with its own event loop for receiving input events and doing other things

native Activity 将处理循环 while(1) {...} 中的所有事件。混合使用 Java 和 native 事件是不可能的。

恕我直言,使用原生 Activity 的主要原因是 UI。如果您已经在 C++ 中拥有功能齐全的 UI,那么使用 native Activity 对您来说更容易且更便携。您仍然可以为 android 添加其他 java Activity 自定义您的应用程序(不要忘记将 android:hasCode="TRUE" 放在您的 list 中!)。在另一种情况下,使用 java Activity 可以让您充分使用 google UI,并在需要时调用您的本地库。

关于你的表现问题,当你说:

I think it will be better to handle this in C++ via native Activity

看看这个:http://developer.android.com/guide/practices/design/performance.html#native_methods

希望这对您有所帮助!

关于android - 如何使用 Native Activity?可以和传统 Activity 结合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5352910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com