Game Development Reference
In-Depth Information
* @param in
* @param out
* @throws IOException
*/
public static void writeToStream(InputStream in, OutputStream out)
throws IOException {
byte[] bytes = new byte[2048];
for (int c = in.read(bytes); c != -1; c = in.read(bytes)) {
out.write(bytes, 0, c);
}
in.close();
out.close();
}
}
In Android 1.5 and later, native libraries can be stored within the project under a folder named
libs/armeabi , then loaded using the system call System.loadLibrary(LIB_NAME) . Thus, Listing 2-1 can be
simplified by removing the library installation step:
public class MainActivity extends Activity
{
private static final String LIB = "ch02";
// Load library: Note that the prefix (lib) and the extension (.so) must be stripped.
static {
System.loadLibrary(LIB);
}
public void onCreate(Bundle savedInstanceState) {
// ...
Natives.LibMain(argv);
}
}
Native Interface
The native interface is defined in the Java class jni.Natives.java . It has two important methods that
deal with the C library (see Listing 2-2):
static native int LibMain(String[] argv) : This is the native library main
subroutine. It will be called within the Android main activity. The method also
takes a list of argument strings to be passed along. Notice the keyword native ,
which tells the Java compiler it is implemented natively.
private static void OnMessage(String text, int level) : This method is meant
to be called from the C library, with a string message and integer value (level). This
method will simply print the message to the console.
Search WWH ::




Custom Search