正确的方法退出android_main

时间:2012-08-17 10:55:29

标签: android android-ndk

退出本机活动的正确方法是什么?也就是说,我如何结束我的android_main功能。如果我什么都不做,那么程序会挂起,最终设备会终止它。如果我调用exit(0)然后它会退出,但它会在日志文件中打印一个转储,表明它没有正确清理。

那么我应该如何正确退出应用程序?

编辑:我正在纯粹用C ++编写本机活动。我问的是如何返回/完成我的android_main程序。我在这个项目中有 Java代码。

日志失败的顶部片段:

V/threaded_app( 5419): NativeWindowDestroyed: 0x29c490 -- 0x29d788
I/DEBUG   ( 1055): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   ( 1055): Build fingerprint: 'LENOVO/IdeaPad_Tablet_A1_07/A1_07:2.3.4/GRJ22/eng.user.20120209.100319:user/release-keys'
I/DEBUG   ( 1055): pid: 5419, tid: 5427  >>> eu.eversystems.sample <<<
I/DEBUG   ( 1055): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000024
I/DEBUG   ( 1055):  r0 0029d788  r1 463ddb64  r2 5f776e64  r3 00000000

3 个答案:

答案 0 :(得分:1)

我没有试过这个,但我想可以在原生活动指针上调用ANativeActivity_finish()

答案 1 :(得分:0)

如果您查看NDK中的本机活动示例,android_main是一个void函数,因此当您想要“退出”时,您只需“返回”。

但是你应该注意从android_main返回的方式/时间。 Android上的应用程序在正常情况下并没有真正“结束”。它们被带入和离开前景。当然,如果进程在后台并且不再需要OS设备,它将按照您的说法终止。在这种情况下,我希望设置android_native_app_glue库中的“destroyRequested”字段,这应该触发android_main的返回。这将允许您优雅地退出应用程序。

在所有其他情况下,您只需要应用程序来兑现它可能会发现自己的各种状态。

即:

/**
 * 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,

示例代码在此处:

http://developer.android.com/reference/android/app/NativeActivity.html

答案 2 :(得分:-1)

你可以使用

android:noHistory="true" 

在所有活动的清单中:)然后

finish() 

您当前的活动..完成了! :)我希望它有所帮助