Android jni -- Field & amp; Method -- & gt; Accessing Fi

Source: Internet
Author: User

Now we know how to use native code to access simple data types and Reference reference types (string, array ), next we will introduce how to allow the jni code to access the member variables and member functions in java, and then callback the methods in java in jni.

Bytes -------------------------------------------------------------------------------------

Accessing fields

Java provides 2 members, static members, and non-static members. JNI supports methods for getting and set static and non-static members. The following is an example.

Access non-static members first.

First, declare a non-static member variable in the class:


[Java]
Public class MyJNI extends Activity {
/** Called when the activity is first created .*/
// Declear a instance field
Private String s = "123 ";

When you click the button, we change the textview string of the title to s,


[Java]
MyJNI mj = new MyJNI ();
 
Mj. accessField ();
TV. setText (mj. s );

Java code is very simple. We only need to implement our functions. The following describes how jni enters the member variable in the class:


[Cpp]
Java_com_android_jni_MyJNI_accessField (JNIEnv * env, jobject obj)
{
JfieldID fid;
Jstring jstr;
Const char * str;
 
// Get a reference to obj's class
Jclass cls = (* env)-> GetObjectClass (env, obj );
// Jclass cls = (* env)-> FindClass (env, "com/android/jni/Native ");
_ Android_log_print (ANDROID_LOG_INFO, "-JNI-", "here in native C! ");
// Look for the instance field in cls
Fid = (* env)-> GetFieldID (env, cls, "s ",
"Ljava/lang/String ;");
If (fid = NULL ){
_ Android_log_print (ANDROID_LOG_INFO, "-JNI-", "can not find field ");
Return;
}
// Read the instance field s
Jstr = (* env)-> GetObjectField (env, obj, fid );
Str = (* env)-> GetStringUTFChars (env, jstr, NULL );
If (str = NULL)
Return;
(* Env)-> ReleaseStringUTFChars (env, jstr, str );
// Create a new string and overwrite the instance field
Jstr = (* env)-> NewStringUTF (env, "abc ");
If (jstr = NULL)
Return; // out of memory
(* Env)-> SetObjectField (env, obj, fid, jstr );
}

To access the member variables in the target class, perform two steps: Call GetFieldID to obtain a field ID from the class, according to the member name and description:

Fid = (* env)-> GetFieldID (env, cls, "s ",
"Ljava/lang/String ;");


Then access the member based on the field ID:

Jstr = (* env)-> GetObjectField (env, obj, fid );


Because string is an object in java, the GetObjectField function is called here.

Finally, run the simulator. When you click the button, textView is changed to the "abc" modified in JNI"

 
 

 

 

OK, this part ends. Next we will look at how to access static member variables.

In the same java code:


[Java]
Public class MyJNI extends Activity {
/** Called when the activity is first created .*/
// Declear a instance field
Private static int si = 100;
Private String s = "123 ";

 

 

We define a static integer variable si initialized to 100. When we click the button, access static field through jni to change the si value, and then display it in the title textView.


[Java]
MyJNI mj = new MyJNI ();
 
Mj. accessStaticField ();
TV. setText (mj. si + "");

Let's take a look at how to enter static field:


[Cpp]
Java_com_android_jni_MyJNI_accessStaticField (JNIEnv * env, jobject obj)
{
JfieldID fid; // store the field id
Jint si;
 
// Get a reference to obj's class
Jclass cls = (* env)-> GetObjectClass (env, obj );
_ Android_log_print (ANDROID_LOG_INFO, "-JNI-", "here in native C! ");

// Look for the static field si in lcs
Fid = (* env)-> GetStaticFieldID (env, cls, "si", "I ");
If (fid = NULL)
Return; // field not found
// Access the static field si
Si = (* env)-> GetStaticIntField (env, cls, fid );
(* Env)-> SetStaticIntField (env, cls, fid, 200 );
}

 

Related Article

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.