2011-03-18 34 views
7

我有兩個庫(.so),我在java代碼中加載。如何使用本機活動?它可以與傳統活動結合嗎?

但是有幾個具體的操作需要Java(Activity)< - > C++(.so files)調用。

我可以使用Native Activity來實現這些功能的一部分嗎?本土活動是傳統活動的補充或我必須選擇哪種類型的活動?

[編輯]

有一組事件可以在本機代碼由native activity

機器人-NDK /源/機器人/ 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++編寫的,我認爲它會更好通過本地活動在C++中處理。不過,我也有代碼必須在Java中的句柄事件後調用。

問題是...我可以有我的活動的本地版本(本機界面),這將有助於我的一些事件,並在這同一時間的同一活動的傳統java界面?

+0

據我瞭解@noisy你的問題是,「你無法加載兩個本地庫」我是否正確? – 100rabh 2011-03-21 09:59:09

回答

3

我會回答你不能有一個活動的代碼的兩個版本。

  • 您如何在您的清單中指定?

  • 在由谷歌提供的樣品,主要的意見是非常明確的:

它運行在自己的線程,有自己的事件循環用於接收輸入事件和做其他事情

本地活動將處理循環中的所有事件while(1) {...}。混合Java和本地事件是不可能的。

恕我直言,使用本地活動的主要原因是用戶界面。如果您已經在C++中擁有全功能的用戶界面,那麼您可以更輕鬆地使用本地活動,並且更便於使用。您仍然可以自定義您的Android應用程序添加其他Java活動(不要忘記在您的清單中放入android:hasCode="TRUE"!)。在另一種情況下,使用java活動可以讓您完全使用google用戶界面,並在需要時調用您的本地庫。

關於你的性能問題,當你說:

我認爲這將是更好的通過本地活動

來處理這在C++來看看這個:http://developer.android.com/guide/practices/design/performance.html#native_methods

希望這可以幫助!

相關問題