Android JNI/NDK Basic Posture < two >

Source: Internet
Author: User

Then the previous article on the Android JNI/NDK development of the basic posture <, today we talk about how to invoke native the method, as well as the native call java layer method, the previous article we have completed the compilation work, today we will learn how to call, Before we change the following JniDemo.c C++ syntax to implement, to meet the tastes of different people, change the following:

//// Created by Q.Jay on 2016/5/8.//#include <com_jay_ndkdemo_JniDemo.h>/* * Class:     com_jay_ndkdemo_JniDemo * Method:    getHelloWordText * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_jay_ndkdemo_JniDemo_getHelloWordText  return env->NewStringUTF("Hello Word From Jni <C++>");}
Calling the native code

1. Before calling, we need to load the *.so file first

System.loadLibrary("jnidemo");

"jnidemo"Is the last time we compile the name of a good, this code is usually called only once, when the call? How do you call it okay?

When do I call? You just have to make sure you native call the method before calling it.
How do you call it okay? Typically used in static code blocks, such as the demo project here:

publicclass MainActivity extends AppCompatActivity {    static {        System.loadLibrary("jnidemo");    }}

Let's take a look at the calling code

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    finalnew JniDemo();    textView = (TextView) findViewById(R.id.textView);    findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {        @Override        publicvoidonClick(View v) {            textView.setText(jniDemo.getHelloWordText());        }    });}

At this point, the call ends, can be run directly to see the effect, is not very simple?

Implement native Call Java layer method

We want to native implement java the calling method, first we need to prepare a java method, as follows:

publicstaticvoidshowToast() {        "此方法由Native方法调用", Toast.LENGTH_SHORT).show();    }

I am here in the MainActivity preparation of a static method, and then implemented toast , the following we look at the key code, native how to implement to call this method, the code is as follows:

Jniexport jstring jnicall java_com_jay_ndkdemo_jnidemo_gethellowordtext (JNIENV * env, J object obj){//Find the method we want to call, note the package name + class nameJ class clazz = env-findclass("Com/jay/ndkdemo/mainactivity" ); //Get the ID of a static method //clazz is the bytecode file for the class we found above //showtoast is a method name in the Clazz class //"() v" The signature of the method, () is the parameter list of the method; v denotes the return type of the method;Jmethodid id = env->getstaticmethodid (clazz,"Showtoast","() V");//Last Call this method, Callstaticvoidmethod (Clazz, method ID)Env->callstaticvoidmethod (Clazz,id);returnEnv->newstringutf ("Hello Word from Jni <C++>");}

At this point, the native call java layer code is implemented, and this series finally realizes that the text returned by clicking on a button call method is displayed on the native TextView, and native java a method of the layer is called before the method returns the text toast .

Finally we look at the effect:

Demo Code Address

Github:https://github.com/jaysong/ndkdemo

Android JNI/NDK Basic Posture < two >

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.