The Native Android API

Overview

Android exposes a native API to applications through the Native Development Kit (NDK). It is described in the $NDK/docs/STABLE-APIS.html document and summarized below:

NameIncludeLinking (LOCAL_LDLIBS :=...)Android version
C runtime library (libc)#include ...Automatic1.5
Java Native Interface#include <jni.h>Automatic1.5
System properties#include <sys/system_properties.h>Automatic1.5
Math#include <math.h>Automatic (-lm not needed)1.5
POSIX Threads (pthreads)#include <pthread.h>Automatic (-lpthread not needed)1.5
C++ subset#include <cstddef>
#include <new>
#include <stl_pair>
#include <utility>
Automatic (-lstdc++ not needed)1.5
Android logging API#include <android/log.h>-llog1.5
Zlib#include <zconf.h>
#include <zlib.h>
-lz1.5
Dynamic linker#include <dlfcn.h>-ldl1.5
OpenGL ES 1.x#include <GLES/gl.h>
#include <GLES/glext.h>
-lGLESv1_CM1.6
OpenGL ES 2.0#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
-lGLESv22.0
Android bitmap API#include <android/bitmap.h>-ljnigraphics2.2
EGL#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <EGL/eglplatform.h>
-lEGL2.3
OpenSL ES#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Platform.h>
#include <SLES/OpenSLES_Android.h>
#include <SLES/OpenSLES_AndroidConfiguration.h>
-lOpenSLES2.3
Android native application API#include <android/native_activity.h>
#include <android/looper.h>
#include <android/input.h>
#include <android/keycodes.h>
#include <android/sensor.h>

Window/Surface management:
#include <android/rect.h>
#include <android/window.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <android/configuration.h>
#include <android/asset_manager.h>
#include <android/storage_manager.h>
#include <android/obb.h>
-landroid2.3

C runtime library (libc)

Android uses a custom C library named Bionic, which has a smaller API than a traditional Unix-like C library (it does not claim POSIX compliance). Basically, if one header is not there at build time, it's because its implementation is not available. An overview is provided in the libc/docs/OVERVIEW.TXT file.

Java Native Interface (JNI)

Android uses the same JNI as standard java. Two great resources are the official Programmer's Guide and Specification as well as Android JNI tips.

System properties

Including the header file <sys/system_properties.h> gives access to native functions for accessing system properties like System#getProperty(String) does in java:

     
     
#define PROP_NAME_MAX 32
#define PROP_VALUE_MAX 92
typedef struct prop_info {
     char name [ PROP_NAME_MAX ];
     unsigned volatile serial ;
     char value [ PROP_VALUE_MAX ];
} prop_info ;
/* Look up a system property by name, copying its value and a
** \0 terminator to the provided pointer. The total bytes
** copied will be no greater than PROP_VALUE_MAX. Returns
** the string length of the value. A property that is not
** defined is identical to a property with a length 0 value.
*/
int __system_property_get ( const char * name , char * value );
/* Return a pointer to the system property named name, if it
** exists, or NULL if there is no such property. Use
** __system_property_read() to obtain the string value from
** the returned prop_info pointer.
**
** It is safe to cache the prop_info pointer to avoid future
** lookups. These returned pointers will remain valid for
** the lifetime of the system.
*/
const prop_info * __system_property_find ( const char * name );
/* Read the value of a system property. Returns the length
** of the value. Copies the value and \0 terminator into
** the provided value pointer. Total length (including
** terminator) will be no greater that PROP_VALUE_MAX.
**
** If name is nonzero, up to PROP_NAME_MAX bytes will be
** copied into the provided name pointer. The name will
** be \0 terminated.
*/
int __system_property_read ( const prop_info * pi , char * name , char * value );
/* Return a prop_info for the nth system property, or NULL if
** there is no nth property. Use __system_property_read() to
** read the value of this property.
**
** This method is for inspecting and debugging the property
** system. Please use __system_property_find() instead.
**
** Order of results may change from call to call. This is
** not a bug.
*/
const prop_info * __system_property_find_nth ( unsigned n );

POSIX threads (pthreads)

Bionic provides built-in support for pthreads, so no additional linking (-lpthreads) is necessary. It does not implement full POSIX threads functionality and leaves out support for pthread_cancel() and read/write locks. Read the Bionic OVERVIEW.txt for more information.

C++ subset

Android officially only supports a small C++ subset limited to the <cstddef>, <new>, <stl_pair> and <utility> headers. Exceptions and RTTI however works from Android version 1.6 onwards. For the standard C++ library a customized version of the NDK is available at crystax.net.

Android logging API

The API for logging from native code is defined in <android/log.h> and matches that of the java android.util.Log class:

     
     
/*
* Android log priority values, in ascending priority order.
*/
typedef enum android_LogPriority {
     ANDROID_LOG_UNKNOWN = 0 ,
     ANDROID_LOG_DEFAULT , /* only for SetMinPriority() */
     ANDROID_LOG_VERBOSE ,
     ANDROID_LOG_DEBUG ,
     ANDROID_LOG_INFO ,
     ANDROID_LOG_WARN ,
     ANDROID_LOG_ERROR ,
     ANDROID_LOG_FATAL ,
     ANDROID_LOG_SILENT , /* only for SetMinPriority(); must be last */
} android_LogPriority ;
/*
* Send a simple string to the log.
*/
int __android_log_write ( int prio , const char * tag , const char * text );
/*
* Send a formatted string to the log, used like printf(fmt,...)
*/
int __android_log_print ( int prio , const char * tag , const char * fmt , ...);
/*
* A variant of __android_log_print() that takes a va_list to list
* additional parameters.
*/
int __android_log_vprint ( int prio , const char * tag , const char * fmt , va_list ap );
/*
* Log an assertion failure and SIGTRAP the process to have a chance
* to inspect it, if a debugger is attached. This uses the FATAL priority.
*/
void __android_log_assert ( const char * cond , const char * tag , const char * fmt , ...);
view raw log.h This Gist brought to you by GitHub.

Using variadic macros one may write portable logging such as:

     
     
#define LOG_TAG "mylib"
#ifdef ANDROID
# define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
# define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
#else
# define QUOTEME_(x) #x
# define QUOTEME(x) QUOTEME_(x)
# define LOGI(...) printf("I/" LOG_TAG " (" __FILE__ ":" QUOTEME(__LINE__) "): " __VA_ARGS__)
# define LOGE(...) printf("E/" LOG_TAG "(" ")" __VA_ARGS__)
#endif

Zlib

The standard zlib compression library may be used by linking with libz.

Dynamic linker

The dynamic linking functions dlopen(), dlsym(), dlclose() and dlerror() may be used by linking with libdl.

EGL

EGL (http://www.khronos.org/egl) exposes API to list supported EGL configurations, allocate and release OpenGLES surfaces and swap/Flip surfaces for display (eglSwapBuffers).

OpenGL ES

Android support for OpenGL ES, a subset of OpenGL, is exposed both in Java and native API.

All android devices supports version OpenGL ES version 1.0, though some may use a software renderer. Applications which requires (as opposed to may optionally use features from through runtime checks) a higher version must specify so in their AndroidManifest.xml file with the android:glEsVersion attribute on the <uses-feature/> tag, as a 32-bit number with the higher and lower 16 bits representing the major and minor OpenGL version number:

<-- require OpenGL ES version 1.0 (default) -->
<uses-feature android:glEsVersion="0x00010000"/>

<-- require OpenGL ES version 1.1 -->
<uses-feature android:glEsVersion="0x00010001"/>

<-- require OpenGL ES version 2.0 -->
<uses-feature android:glEsVersion="0x00020000"/>

Note that the Android emulator does not support OpenGL ES version 2.x - a physical device is required for testing applications using that version.

Android bitmap API

The API for handling android.util.Bitmap instances from native code is defined in <android/bitmap.h>:

     
     
#define ANDROID_BITMAP_RESUT_SUCCESS 0
#define ANDROID_BITMAP_RESULT_BAD_PARAMETER -1
#define ANDROID_BITMAP_RESULT_JNI_EXCEPTION -2
#define ANDROID_BITMAP_RESULT_ALLOCATION_FAILED -3
enum AndroidBitmapFormat {
     ANDROID_BITMAP_FORMAT_NONE = 0 ,
     ANDROID_BITMAP_FORMAT_RGBA_8888 = 1 ,
     ANDROID_BITMAP_FORMAT_RGB_565 = 4 ,
     ANDROID_BITMAP_FORMAT_RGBA_4444 = 7 ,
     ANDROID_BITMAP_FORMAT_A_8 = 8 ,
};
typedef struct {
     uint32_t width ;
     uint32_t height ;
     uint32_t stride ;
     int32_t format ;
     uint32_t flags ; // 0 for now
} AndroidBitmapInfo ;
/**
* Given a java bitmap object, fill out the AndroidBitmap struct for it.
* If the call fails, the info parameter will be ignored
*/
int AndroidBitmap_getInfo ( JNIEnv * env , jobject jbitmap , AndroidBitmapInfo * info );
/**
* Given a java bitmap object, attempt to lock the pixel address.
* Locking will ensure that the memory for the pixels will not move
* until the unlockPixels call, and ensure that, if the pixels had been
* previously purged, they will have been restored.
*
* If this call succeeds, it must be balanced by a call to
* AndroidBitmap_unlockPixels, after which time the address of the pixels should
* no longer be used.
*
* If this succeeds, *addrPtr will be set to the pixel address. If the call
* fails, addrPtr will be ignored.
*/
int AndroidBitmap_lockPixels ( JNIEnv * env , jobject jbitmap , void ** addrPtr );
/**
* Call this to balanace a successful call to AndroidBitmap_lockPixels
*/
int AndroidBitmap_unlockPixels ( JNIEnv * env , jobject jbitmap );
view raw bitmap.h This Gist brought to you by GitHub.

OpenSL ES

The OpenSL ES API, as well as some Android-specific extensions, is supported since Android version 2.3. The OpenSL ES 1.0.1 specification is bundledwith the NDK under $NDK/opensles/OpenSL_ES_Specification_1.0.1.pdf and Android-specific information is available under $NDK/opensles/index.html.

Android native application API

The Android native application API is an umbrella for accessing Android functionality otherwise exposed in java.

Looper

     
     
/**
* ALooper
*
* A looper is the state tracking an event loop for a thread.
* Loopers do not define event structures or other such things; rather
* they are a lower-level facility to attach one or more discrete objects
* listening for an event. An "event" here is simply data available on
* a file descriptor: each attached object has an associated file descriptor,
* and waiting for "events" means (internally) polling on all of these file
* descriptors until one or more of them have data available.
*
* A thread can have only one ALooper associated with it.
*/
struct ALooper ;
typedef struct ALooper ALooper ;
/**
* Returns the looper associated with the calling thread, or NULL if
* there is not one.
*/
ALooper * ALooper_forThread ();
enum {
     /**
* Option for ALooper_prepare: this looper will accept calls to
* ALooper_addFd() that do not have a callback (that is provide NULL
* for the callback). In this case the caller of ALooper_pollOnce()
* or ALooper_pollAll() MUST check the return from these functions to
* discover when data is available on such fds and process it.
*/
     ALOOPER_PREPARE_ALLOW_NON_CALLBACKS = 1 << 0
};
/**
* Prepares a looper associated with the calling thread, and returns it.
* If the thread already has a looper, it is returned. Otherwise, a new
* one is created, associated with the thread, and returned.
*
* The opts may be ALOOPER_PREPARE_ALLOW_NON_CALLBACKS or 0.
*/
ALooper * ALooper_prepare ( int opts );
enum {
     /**
* Result from ALooper_pollOnce() and ALooper_pollAll():
* The poll was awoken using wake() before the timeout expired
* and no callbacks were executed and no other file descriptors were ready.
*/
     ALOOPER_POLL_WAKE = - 1 ,
     /**
* Result from ALooper_pollOnce() and ALooper_pollAll():
* One or more callbacks were executed.
*/
     ALOOPER_POLL_CALLBACK = - 2 ,
     /**
* Result from ALooper_pollOnce() and ALooper_pollAll():
* The timeout expired.
*/
     ALOOPER_POLL_TIMEOUT = - 3 ,
     /**
* Result from ALooper_pollOnce() and ALooper_pollAll():
* An error occurred.
*/
     ALOOPER_POLL_ERROR = - 4 ,
};
/**
* Acquire a reference on the given ALooper object. This prevents the object
* from being deleted until the reference is removed. This is only needed
* to safely hand an ALooper from one thread to another.
*/
void ALooper_acquire ( ALooper * looper );
/**
* Remove a reference that was previously acquired with ALooper_acquire().
*/
void ALooper_release ( ALooper * looper );
/**
* Flags for file descriptor events that a looper can monitor.
*
* These flag bits can be combined to monitor multiple events at once.
*/
enum {
     /**
* The file descriptor is available for read operations.
*/
     ALOOPER_EVENT_INPUT = 1 << 0 ,
     /**
* The file descriptor is available for write operations.
*/
     ALOOPER_EVENT_OUTPUT = 1 << 1 ,
     /**
* The file descriptor has encountered an error condition.
*
* The looper always sends notifications about errors; it is not necessary
* to specify this event flag in the requested event set.
*/
     ALOOPER_EVENT_ERROR = 1 << 2 ,
     /**
* The file descriptor was hung up.
* For example, indicates that the remote end of a pipe or socket was closed.
*
* The looper always sends notifications about hangups; it is not necessary
* to specify this event flag in the requested event set.
*/
     ALOOPER_EVENT_HANGUP = 1 << 3 ,
     /**
* The file descriptor is invalid.
* For example, the file descriptor was closed prematurely.
*
* The looper always sends notifications about invalid file descriptors; it is not necessary
* to specify this event flag in the requested event set.
*/
     ALOOPER_EVENT_INVALID = 1 << 4 ,
};
/**
* For callback-based event loops, this is the prototype of the function
* that is called. It is given the file descriptor it is associated with,
* a bitmask of the poll events that were triggered (typically ALOOPER_EVENT_INPUT),
* and the data pointer that was originally supplied.
*
* Implementations should return 1 to continue receiving callbacks, or 0
* to have this file descriptor and callback unregistered from the looper.
*/
typedef int ( * ALooper_callbackFunc )( int fd , int events , void * data );
/**
* Waits for events to be available, with optional timeout in milliseconds.
* Invokes callbacks for all file descriptors on which an event occurred.
*
* If the timeout is zero, returns immediately without blocking.
* If the timeout is negative, waits indefinitely until an event appears.
*
* Returns ALOOPER_POLL_WAKE if the poll was awoken using wake() before
* the timeout expired and no callbacks were invoked and no other file
* descriptors were ready.
*
* Returns ALOOPER_POLL_CALLBACK if one or more callbacks were invoked.
*
* Returns ALOOPER_POLL_TIMEOUT if there was no data before the given
* timeout expired.
*
* Returns ALOOPER_POLL_ERROR if an error occurred.
*
* Returns a value >= 0 containing an identifier if its file descriptor has data
* and it has no callback function (requiring the caller here to handle it).
* In this (and only this) case outFd, outEvents and outData will contain the poll
* events and data associated with the fd, otherwise they will be set to NULL.
*
* This method does not return until it has finished invoking the appropriate callbacks
* for all file descriptors that were signalled.
*/
int ALooper_pollOnce ( int timeoutMillis , int * outFd , int * outEvents , void ** outData );
/**
* Like ALooper_pollOnce(), but performs all pending callbacks until all
* data has been consumed or a file descriptor is available with no callback.
* This function will never return ALOOPER_POLL_CALLBACK.
*/
int ALooper_pollAll ( int timeoutMillis , int * outFd , int * outEvents , void ** outData );
/**
* Wakes the poll asynchronously.
*
* This method can be called on any thread.
* This method returns immediately.
*/
void ALooper_wake ( ALooper * looper );
/**
* Adds a new file descriptor to be polled by the looper.
* If the same file descriptor was previously added, it is replaced.
*
* "fd" is the file descriptor to be added.
* "ident" is an identifier for this event, which is returned from ALooper_pollOnce().
* The identifier must be >= 0, or ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
* "events" are the poll events to wake up on. Typically this is ALOOPER_EVENT_INPUT.
* "callback" is the function to call when there is an event on the file descriptor.
* "data" is a private data pointer to supply to the callback.
*
* There are two main uses of this function:
*
* (1) If "callback" is non-NULL, then this function will be called when there is
* data on the file descriptor. It should execute any events it has pending,
* appropriately reading from the file descriptor. The 'ident' is ignored in this case.
*
* (2) If "callback" is NULL, the 'ident' will be returned by ALooper_pollOnce
* when its file descriptor has data available, requiring the caller to take
* care of processing it.
*
* Returns 1 if the file descriptor was added or -1 if an error occurred.
*
* This method can be called on any thread.
* This method may block briefly if it needs to wake the poll.
*/
int ALooper_addFd ( ALooper * looper , int fd , int ident , int events , ALooper_callbackFunc callback , void * data );
/**
* Removes a previously added file descriptor from the looper.
*
* When this method returns, it is safe to close the file descriptor since the looper
* will no longer have a reference to it. However, it is possible for the callback to
* already be running or for it to run one last time if the file descriptor was already
* signalled. Calling code is responsible for ensuring that this case is safely handled.
* For example, if the callback takes care of removing itself during its own execution either
* by returning 0 or by calling this method, then it can be guaranteed to not be invoked
* again at any later time unless registered anew.
*
* Returns 1 if the file descriptor was removed, 0 if none was previously registered
* or -1 if an error occurred.
*
* This method can be called on any thread.
* This method may block briefly if it needs to wake the poll.
*/
int ALooper_removeFd ( ALooper * looper , int fd );
view raw looper.h This Gist brought to you by GitHub.

Window management

The API for handling android.view.Surface instances from native code, or "window management functions", are summarized by the below interfaces from rect.h, native_window.h andnative_window_jni.h:

     
     
/**
* Return the ANativeWindow associated with a Java Surface object,
* for interacting with it through native code. This acquires a reference
* on the ANativeWindow that is returned; be sure to use ANativeWindow_release()
* when done with it so that it doesn't leak.
*/
ANativeWindow * ANativeWindow_fromSurface ( JNIEnv * env , jobject surface );
typedef struct ARect {
int32_t left ;
int32_t top ;
int32_t right ;
int32_t bottom ;
} ARect ;
/* Pixel formats that a window can use. */
enum {
     WINDOW_FORMAT_RGBA_8888 = 1 ,
     WINDOW_FORMAT_RGBX_8888 = 2 ,
     WINDOW_FORMAT_RGB_565 = 4 ,
};
struct ANativeWindow ;
typedef struct ANativeWindow ANativeWindow ;
typedef struct ANativeWindow_Buffer {
     // The number of pixels that are show horizontally.
     int32_t width ;
     // The number of pixels that are shown vertically.
     int32_t height ;
     // The number of *pixels* that a line in the buffer takes in
     // memory. This may be >= width.
     int32_t stride ;
     // The format of the buffer. One of WINDOW_FORMAT_*
     int32_t format ;
     // The actual bits.
     void * bits ;
    
     // Do not touch.
     uint32_t reserved [ 6 ];
} ANativeWindow_Buffer ;
/**
* Acquire a reference on the given ANativeWindow object. This prevents the object
* from being deleted until the reference is removed.
*/
void ANativeWindow_acquire ( ANativeWindow * window );
/**
* Remove a reference that was previously acquired with ANativeWindow_acquire().
*/
void ANativeWindow_release ( ANativeWindow * window );
/*
* Return the current width in pixels of the window surface. Returns a
* negative value on error.
*/
int32_t ANativeWindow_getWidth ( ANativeWindow * window );
/*
* Return the current height in pixels of the window surface. Returns a
* negative value on error.
*/
int32_t ANativeWindow_getHeight ( ANativeWindow * window );
/*
* Return the current pixel format of the window surface. Returns a
* negative value on error.
*/
int32_t ANativeWindow_getFormat ( ANativeWindow * window );
/*
* Change the format and size of the window buffers.
*
* The width and height control the number of pixels in the buffers, not the
* dimensions of the window on screen. If these are different than the
* window's physical size, then it buffer will be scaled to match that size
* when compositing it to the screen.
*
* For all of these parameters, if 0 is supplied then the window's base
* value will come back in force.
*/
int32_t ANativeWindow_setBuffersGeometry ( ANativeWindow * window , int32_t width , int32_t height , int32_t format );
/**
* Lock the window's next drawing surface for writing.
*/
int32_t ANativeWindow_lock ( ANativeWindow * window , ANativeWindow_Buffer * outBuffer , ARect * inOutDirtyBounds );
/**
* Unlock the window's drawing surface after previously locking it,
* posting the new buffer to the display.
*/
int32_t ANativeWindow_unlockAndPost ( ANativeWindow * window );
view raw window*.h This Gist brought to you by GitHub.

Native activity

Using android.app.NativeActivity one may avoid having to write java glue code. See $NDK/docs/NATIVE-ACTIVITY.HTML. Note the bundled static library defined by $NDK/sources/android/native_app_glue/android_native_app_glue.h and implemented in $NDK/sources/android/native_app_glue/android_native_app_glue.c which implements an event loop in a separate thread.

   
   
/**
* This structure defines the native side of an 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.
*/
     JavaVM * vm ;
     /**
* JNI context for the main thread of the app. Note that this field
* can ONLY be used from the main thread of the process; that is, the
* thread that calls into the ANativeActivityCallbacks.
*/
     JNIEnv * env ;
     /**
* The NativeActivity Java class.
*/
     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.
*/
     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.
*/
     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.
*/
     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; 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 ;
/**
* 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 ;
/**
* Finish the given activity. Its finish() method will be called, causing it
* to be stopped and destroyed. 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.
*/
void ANativeActivity_finish ( ANativeActivity * activity );
/**
* Change the window format of the given activity. Calls getWindow().setFormat()
* of the given activity. 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.
*/
void ANativeActivity_setWindowFormat ( ANativeActivity * activity , int32_t format );
/**
* Change the window flags of the given activity. Calls getWindow().setFlags()
* of the given activity. 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. See window.h for flag constants.
*/
void ANativeActivity_setWindowFlags ( ANativeActivity * activity ,
         uint32_t addFlags , uint32_t removeFlags );
/**
* Flags for ANativeActivity_showSoftInput; see the Java InputMethodManager
* API for documentation.
*/
enum {
     ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT = 0x0001 ,
     ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED = 0x0002 ,
};
/**
* Show the IME while in the given activity. Calls InputMethodManager.showSoftInput()
* for the given activity. 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.
*/
void ANativeActivity_showSoftInput ( ANativeActivity * activity , uint32_t flags );
/**
* Flags for ANativeActivity_hideSoftInput; see the Java InputMethodManager
* API for documentation.
*/
enum {
     ANATIVEACTIVITY_HIDE_SOFT_INPUT_IMPLICIT_ONLY = 0x0001 ,
     ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS = 0x0002 ,
};
/**
* Hide the IME while in the given activity. Calls InputMethodManager.hideSoftInput()
* for the given activity. 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.
*/
void ANativeActivity_hideSoftInput ( ANativeActivity * activity , uint32_t flags );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值