Reflection call Java layer method

On the basis of obtaining the value of the Java layer field by reflection in the underlying C code, we continue to learn to call the Java layer method by reflection to further improve the login function.

One: Define two methods in LoginActivity

Open the eclipse tool and find the "yijindaxue" project created in the previous lesson. In the project, find the "LoginActivity.java" file in the "src" => "com.yijindaxue" directory, and double-click to open the file.

Add two methods to the file, as shown in the following figure:

The first method: userError is used to prompt the user to enter the wrong account.

The second method: pwdError is used to prompt the user to enter the wrong password.

Reflection call Java layer method

Two: write C code

1. Find the "yijindaxue.c" file in the "jni" directory in the "yijindaxue" project, and double-click to open the file.

2. Find the Java_com_yijindaxue_LoginActivity _login function in the "yijindaxue.c" file, and improve the login function of this function, as shown in the following figure:

(1) Call the strcmp function to compare whether the account entered by the user is correct. If it is incorrect, call the member of the Java layer through reflection (userError), prompting the user to enter the account incorrectly.

Step 1: Call the GetMethodID function to obtain the ID of the Java layer member method.
Step 2: Use the CallVoidMethod function to call the Java layer member method.

(2) Call the strcmp function to compare whether the password entered by the user is correct, and if it is incorrect, call the static method of the Java layer (pwdError) through reflection, prompting the user that the password entered is incorrect.

The first step: call GetStaticMethodID function to obtain the ID of the Java layer static method.
Step 2: Use the CallStaticVoidMethod function to call the static method of the Java layer.

Reflection call Java layer method

Three: Compile the dynamic link library

Right-click the jni directory under the "yijindaxue" project and select the last property, click this option to pop up a pop-up window, copy the value corresponding to Location, which is the file path D:\Android\data \yijindaxue\jni, close the dialog box after the copy is successful, As shown below:

Reflection call Java layer method

Click the "windows" icon in the lower left corner, enter the "cmd" command to open the window, enter "D:" to switch to the D drive, and then execute the command "cd D:\Android\data\yijindaxue\jni" to enter the jni directory, and then execute " The "ndk-build" command generates a dynamic link library ".so" file, as shown in the following figure:

Reflection call Java layer method

Four: Run the application

Open the Thunderbolt Simulator to run the application: select the "yijindaxue" project => right-click and select the Run As option => click Android Application.

summary:

Mainly learned how to call the Java layer by reflection and improve the login function. The steps are as follows:

1. Two methods, userError and pwdError, are defined in LoginActivity to prompt the user whether the account and password entered are correct.

2. Write C code and call Java layer methods by reflection.

(1) Reflect to call member methods.
Step 1: Call the GetMethodID function to obtain the ID of the Java layer member method.
Step 2: Use the CallVoidMethod function to call the Java layer member method.

(2) Reflection calls static methods.
The first step: call GetStaticMethodID function to obtain the ID of the Java layer static method.
Step 2: Use the CallStaticVoidMethod function to call the static method of the Java layer.

3. Use the NDK tool to compile the .c file to generate the dynamic link library .so file.

4. Run the application.

Guess you like

Origin blog.51cto.com/15002917/2562989