android activity使用,android,NativeActivity使用
android現在已經支持C/C++ NativeActivity開發
android提供
頭文件。所以你要使用的話,要引入這個頭文件
1,NativeActivity 入口
入口函數是void?ANativeActivity_onCreate(ANativeActivity*?activity,
void*?savedState,?size_t?savedStateSize)
你也可以自己在manifest里面使用metadata name=”android.app.func_name”定義一個特殊的函數名
因為頭文件就是這么定義的。而且必須有。不然不能使用native activity。它的定義如下:/**
*?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.??You?must?make
*?any?copy?of?this?data?you?need?--?it?will?be?released?after?you?return?from
*?this?function.
*/
typedef?void?ANativeActivity_createFunc(ANativeActivity*?activity,
void*?savedState,?size_t?savedStateSize);
/**
*?The?name?of?the?function?that?NativeInstance?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.
*/
extern?ANativeActivity_createFunc?ANativeActivity_onCreate;
2,定義native activity的生命周期回調接口函數。
這些接口的名字和java端的是類似的。它的定義如下:/**
*?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;?set?to?a?pointer?to?your?own?function
*?to?have?it?called.
*/
typedef?struct?ANativeActivityCallbacks?{
/**
*?NativeActivity?has?started.??See?Java?documentation?for?Activity.onStart()
*?for?more?information.
*/
void?(*onStart)(ANativeActivity*?activity);
/**
*?NativeActivity?has?resumed.??See?Java?documentation?for?Activity.onResume()
*?for?more?information.
*/
void?(*onResume)(ANativeActivity*?activity);
/**
*?Framework?is?asking?NativeActivity?to?save?its?current?instance?state.
*?See?Java?documentation?for?Activity.onSaveInstanceState()?for?more
*?information.??The?returned?pointer?needs?to?be?created?with?malloc();
*?the?framework?will?call?free()?on?it?for?you.??You?also?must?fill?in
*?outSize?with?the?number?of?bytes?in?the?allocation.??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.??See?Java?documentation?for?Activity.onPause()
*?for?more?information.
*/
void?(*onPause)(ANativeActivity*?activity);
/**
*?NativeActivity?has?stopped.??See?Java?documentation?for?Activity.onStop()
*?for?more?information.
*/
void?(*onStop)(ANativeActivity*?activity);
/**
*?NativeActivity?is?being?destroyed.??See?Java?documentation?for?Activity.onDestroy()
*?for?more?information.
*/
void?(*onDestroy)(ANativeActivity*?activity);
/**
*?Focus?has?changed?in?this?NativeActivity's?window.??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.??The?new?configuration?can
*?be?retrieved?from?assetManager.
*/
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;
這里它定義了16個函數指針,你需要在ANativeActivity_onCreate函數里面將這些自己定義的回調函數賦給ANativeActivity的callback的相應函數指針。例如:void?ANativeActivity_onCreate(ANativeActivity*?activity,
void*?savedState,?size_t?savedStateSize)?{
LOGV("Creating:?%p\n",?activity);
activity->callbacks->onDestroy?=?onDestroy;
activity->callbacks->onStart?=?onStart;
activity->callbacks->onResume?=?onResume;
activity->callbacks->onSaveInstanceState?=?onSaveInstanceState;
activity->callbacks->onPause?=?onPause;
activity->callbacks->onStop?=?onStop;
activity->callbacks->onConfigurationChanged?=?onConfigurationChanged;
activity->callbacks->onLowMemory?=?onLowMemory;
activity->callbacks->onWindowFocusChanged?=?onWindowFocusChanged;
activity->callbacks->onNativeWindowCreated?=?onNativeWindowCreated;
activity->callbacks->onNativeWindowDestroyed?=?onNativeWindowDestroyed;
activity->callbacks->onInputQueueCreated?=?onInputQueueCreated;
activity->callbacks->onInputQueueDestroyed?=?onInputQueueDestroyed;
//其他代碼...
}
上面的onDestroy這些都是你自己定義的接口函數。函數類型要和它提供的接口函數指針一樣。
3.manifest文件的配置
manifest里面其他是一樣的,只有actvity和一般的不一樣
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
android:value="NativeActivity"?/>
這里有兩種情況。
1.完全不用java
上面這個例子就是完全不用java的。這時候你的activity的android:name屬性就必須等于
android.app.NativeActivity。另外在application里添加屬性android:hasCode=”false”,表示
沒有java代碼
2,有一個NativeActivity子類
你是java里繼承NativeActivity,這時activity的android:name屬性就等于你自己的activity名字。這個
activity必須是NativeActivity的子類,不然沒法用NDK的東西。application里添加屬性
android:hasCode=”ture”。
以上只是簡單介紹了NativeActivity,你要完全使用它,還需學習ALooper,pipe和pthread等知識。
總結
以上是生活随笔為你收集整理的android activity使用,android,NativeActivity使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 隐藏webview地址栏
- 下一篇: Android 毛小软件,毛库官方app