Code Download: http://dl.dbank.com/c0c0xs3b24
One, JNI implementation callback
The method of invoking the Java layer at the native level through JNI enables the native layer to deliver messages to the Java layer.
Jnicallback.java
public class Jnicallback extends activity {
static {
system.loadlibrary ("callback");
}
public void callback (int notify_id, int param)
{
log.e ("Jnicallback", "notify_id=" +notify_id+ ";p aram=" +param );
}
public native void Setcallback ();
/** called the activity is a. * *
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Setcallback ();
}
Com_jnicallback_jnicallback.cpp
void Jnicall java_com_jnicallback_jnicallback_setcallback
(jnienv *env, jobject obj)
{
LOGE ("Jni Callback (0) ");
Jclass cls = Env->getobjectclass (obj);
Jmethodid callback = Env->getmethodid (CLS, "Callback", "(II) V");
Env->callvoidmethod (obj,callback,5,10);
LOGE ("JNI callback (1)");
}
javah-classpath.-jni com. Jnicallback.jnicallback (package name + class name)
Ii. JNI invoke Java functions
Gets the class definition of the instance jclass CLS = Env->getobjectclass (obj);
Gets the method definition Jmethodid callback = Env->getmethodid (CLS, "Callback", "(II) V");
Call Method Env->callvoidmethod (obj,callback,5,10);
iii. format of parameters and return values
Reference http://java.sun.com/docs/books/jni/html/jniTOC.html