Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call C function in Android

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about how to call the function of C in Android. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Activity_main.xml

The button adds the event name of onCLick to btnclick, and we select to create this button event in the corresponding Activity through Alt+Enter.

MainActivity.java

Let's go back to MainActivity, where the default stringFromJni method is to return a string directly, and then we can take a look at the source code.

Next, let's try how to implement the method with parameters.

We created a new settextgood method with one input parameter and one return parameter.

As you can see, the method just defined is displayed in red font, then we mouse over the method name, then press ALT+ENTER to select the first item in the red box below

The program automatically jumps to the folder of native-lib.cpp and creates the corresponding calling method.

Next, let's write the method of this settextgood

Extern "C" JNIEXPORT jstring JNICALLJava_dem_vac_vaccaendk_MainActivity_settextgood (JNIEnv * env, jobject instance, jstring str_) {const char * str = env- > GetStringUTFChars (str_, 0)

/ / define the appended character char * addstr= ", I am the appended character"; / / define the character to be output and set the length char * outputstr = new char [strlen (str) + strlen (addstr)]; / / start assembling the output character / / 1. The incoming characters are copied into strcpy (outputstr,str); / / 2. Concatenate the additional character strcat (outputstr,addstr) just defined

/ / release resource env- > ReleaseStringUTFChars (str_, str)

Return env- > NewStringUTF (outputstr);}

Finally, we rewrite a button event and click in to pass in a "Hello".

Let's run the program to see

When we first came in, it showed Hello from clocking cards. Now let's click the test button again and see the results.

After reading the above, have you mastered how to call the function of C in Android? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report