1. CreateNative Method
Public final static native int startservice ();
2. Select Project Right click on Android Tools->add native support will generate JNI file
The 3.CD user/alarmtest/bin/command enters the project's classes execution Javah Com.li.JNI package name. Class name can generate Com_yfz_jni.h file and then copy the com_yfz_jni.h file to the JNI file
4 Modify Android.mk File
Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module: = Test This is the name of the generated library file
Local_src_files: = com_li_jni.cpp Note Here The default is the generated project name. cpp Here we need to change to the package name _ Class name. cpp because there is no code inside the build, you can use the. h file
Local_ldlibs + =-l$ (sysroot)/usr/lib-llog This is the library file we want to print with the Android log information.
Include $ (build_shared_library)
5. Modify Com.li.JNI.cpp
Introduction of two required files equivalent to Java Import
#include <jni.h>
#include <android/log.h>
Then add in. cpp because the. h file has already generated an interface inside the CPP to implement {
__android_log_print (Android_log_error, "test-jni", "%s", "ERROR");
Android_log_error This is the LOG level inside Android here is the ERROR
Test-jni is the tag inside Android
'%s ' means that the input is a string
Error is what we print
}
6. Click Project->build Project to generate the so library file.
7. Load Library file System.loadlibrary ("Test");
8. Call the JNI method Jni.startservice ();
9. Here's what we've shown from the Android call
OK, it's here today!! Everyone has any questions can leave a message to study together!!
Android calls JNI and prints log