Accessing Java objects from native code
When we call a native function, the C or C++ function receives a JNIEnv
pointer to a table of JNI methods used to interact with JVM Runtime. The JNIEnv
pointer provides us with a set of primitives ready to find a Java class definition, set or get Java object field values, call static or member Java object functions, create Java objects, interact with Java monitors, or deal with exceptions.
Our next example will count the number of words on an EditText
UI Widget on a native function and update a TextView
text with count results from the native code. Therefore, we will learn how to use JNIEnv
to access a member Java object field and how to call a Java object method (TextView.setText
) using the JNIEnv
interface.
Let's start by defining our native function and invoke it every time the EditField
content changes:
public class MyNativeActivity extends Activity { protected EditText inputTextEt = null; protected TextView charCountTv = null; @Override...