JNI Implementation Callback | JNI calls Java functions | The format of parameters and return values __linux

Source: Internet
Author: User
Tags class definition
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.