Learning about JNI in Android (iii) Access Java-side objects in the JNI layer

Source: Internet
Author: User

The previous two articles introduce some corresponding relationships between the JNI layer and the Java layer, including the method name, data type and method name, which are believed to be at the theoretical level. Can very well help us to understand the role of JNI in local development of native, and have a preliminary understanding of some of the concepts of JNI, because the ability to express or understand is limited, and some places are not very clear. Suppose all friends think foggy, welcome to leave a message to study together.

Conceptual understanding helps us to better understand JNI. Some examples of actual points can better help us to master and apply JNI from the code.

In the first article, we learned from a sample, in which we returned a string through the JNI layer function. For example, the following:

Jniexport jstring jnicall Java_com_lms_jni_hwdemo_printhello  (jnienv *e, Jobject j) {return (**e). Newstringutf (E, "Hello from T");}

This is one of the simplest cases. But many other times, we need to get Java objects in the JNI layer and manipulate them. Finally, the results are returned to the Java side. So at this point we're going to use the second parameter defined by the JNI function Jobject.

As we said in the previous article, JNIEnv * and jobejct are all the parameters added by the JNI layer method, and we have introduced the previous article about jnienv*. and jobject the number of references. Then we have to work on the number of references to this article.

For local methods, the native method defined in Java, there are static (static) and non-static methods. And we know that the static method is a method that belongs to this class, and the object cannot manipulate it. The non-static method is just the opposite, so in the method parameters of the JNI layer:

1) for static methods, the Jobject parameter represents a reference to the corresponding Java class.

2) for non-static methods, Jobject represents a reference to the corresponding Java object.

This should not be difficult to understand.

Next. We learned how to manipulate Java-side objects in the JNI layer and change the values in a small demo.

First, we set a static variable TestVal in the Java class, and another method, Changetestval (), is used to change the value of the testval, such as the following:

public class Paramtransfertest {public static int testval = 1;public native void Changetestval (); ...}

Of course. First, the first step is to implement its corresponding function in C, such as the following:

jniexport void Jnicall java_com_lms_jni_paramtransfertest_changetestval  (jnienv * env, Jobject obj) {Jclass Clazz = ( *env)->getobjectclass (env,obj); Jint val = (*env)->getstaticintfield (env, Clazz, (*env)->getstaticfieldid ( Env, Clazz, "TestVal", "I")); Logi ("before change testval =%d", val); val = val + 1; Logi ("After change testval =%d", val);(*env)->setstaticintfield (env, Clazz, (*env)->getstaticfieldid (env, Clazz , "TestVal", "I"), Val);

We implement this native method in the corresponding C file, because the implementation of the non-static method, so Jobject passed is the reference to the object, so we need to use the Getobjectclass method to obtain the corresponding class of the object.

Generally in JNI, we use the Findclass and Getobjectclass two methods to obtain the corresponding classes and put them into variables of the Jclass type. Just take a little note here. Code implemented in C and in C + + is not the same for JNI invocation methods.

said in the previous article. C + + has a wrapper over the method defined by Jninativeinterface, so its parameters no longer need to pass env in, and C is required, for example, the method above *env call, assuming that it is implemented in C + +. Then it is no longer necessary to pass the Env to enter, that is Getobjectclass (Jobject) can be.

1) Using Getobjectclass method to obtain Jclass.

2) Call Getstaticintfieldid to get the corresponding class variable, which is the static (static) variable testval of type I (that is, int) in Jclass.

3) Call Getstaticintfield to get the value of the corresponding variable val.

4) Change the Val value, here we do the plus 1 operation.

5) Call Setstaticintfield to set the value of the corresponding variable.

So. Here we find. Env In fact provides a lot of methods, for the access to the object variable value, is divided into static non-static, basically is Get<type>field and Getstatic<type>field,

And the corresponding, there are Set<type>field and Setstatic<type>field methods.

And assuming the call method, is the use of Call<type>method and Callstatic<type>method method, these people can go to jni.h files themselves to see. You probably know how to do it.

After the JNI layer has been implemented, we use the Ndk-build tool to generate a so library again. Loaded into Android, the method is called directly in the activity, for example the following:

TextView tvchangetestval = (TextView) Findviewbyid (r.id.tvchangetestval); Paramtransfertest PTT = new Paramtransfertest ();p tt.changetestval (); Tvchangetestval.settext ("" + ptt.testval);

After we call the method, the value after the method is called on the screen. Shown, the result should be 1+1=2. Well, look at the following results:

Indeed, as we have imagined, its value has changed to 2, right, which means that we did change the value of the JNI layer by the native method.

We have also added log to the JNI to show the values before and after the change, such as the following:

Through such a simple sample, I believe that you should know how to operate in the JNI layer to manipulate the Java side of the data. Right.

End!

Learning about JNI in Android (iii) Access Java-side objects in the JNI layer

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.