nClickListener(new OnClickListener() { @Override public void onClick(View v) { mGetJni.setText(getJniDatas()); } }); } /** * A native method that is implemented by the * 'hello-jni' native library, which is packaged * with this application. * @return */ public native String getJniDatas(); /** *This is another native method declaration that is *not* * implemented by 'hello-jni'. This is simply to show that * you can declare as many native methods in your Java code * as you want, their implementation is searched in the * currently loaded native libraries only the first time * you call them. * * Trying to call this function will result in a * java.lang.UnsatisfiedLinkError exception ! * @return */ public native String unimplementedGetJniDatas(); /** * this is used to load the 'hello-jni' library on application * startup. The library has already been unpacked into * /data/data/com.example.HelloJni/lib/libhello-jni.so at * installation time by the package manager. */ static{ System.loadLibrary("hello-jni"); } }
上面有注释,其native方法为JNI需要调用与执行的方法,
在布局里就添加了一个按钮与一个文本,使用按钮单点后来获得文本显示的字符为JNI所得来的数据,其实这个过程很简单,基本就是NDK里面本身的列子般过来运行一样,等这都完了,先不要急于运行,你需要先把C文件进行编译后才能执行运行APP,在命令行进入其目录的JNI目录,使用ndk-build命令来构建编译:
你会看到:jankey@jankey-ThinkPad-Edge:~/workspace/jni_demo/jni$ ndk-build Compile thumb : hello-jni <= hello-jni.c SharedLibrary : libhello-jni.so Install : libhello-jni.so => libs/armeabi/libhello-jni.so,生成了两个文件目录:
即两个.so为后缀的文件,在这里基本就没什么问题了,这样就建立了C与JAVA通信的一个过程,OK,使用CTRI+F11开始运行其APP:
,先我们可以单击来获取JNI返回回来的数据了:

这就把数据给调出来了,如果你是初学者,你还等什么呢,赶快动手吧,希望这能给在入门里做最给里的参考,谢谢。
|