Android-JNI programming-graphic analysis

Source: Internet
Author: User

To read and debug the following source code, make sure that your NDK environment is OK;
Compiling environment: win7 + Eclipse + ADT + SDK + NDK; use the latest version.
Note: The following code is a simple personal jni implementation. [It's a simple implementation, but the complete implementation goes through many twists and turns and records ];
It is not a ready-made example. It is an implementation after jni has learned the ing relationship between Java and c/c ++. If there is anything wrong, please correct it.
Let's take a look at the process from start to end: Step 1 to 10]
 





Then a corresponding *. so dynamic library is generated and automatically embedded in the libs/armeabi directory;
The obj directory generates an intermediate file *. o. I do not know what the console outputs:


10. Demo after normal operation:
Log effect:

Real machine test results:

Okay, so far, the effect is almost the same, so the next step is the code ··

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: id = "@ + id/textView2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "the following result is returned from jni --"/> <TextView android: id = "@ + id/show_jni_text" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/textView2" android: layout_below = "@ + id/textView2" android: layout_marginTop = "77dp" android: text = "The message from jni will be displayed here --"/> <Button android: id = "@ + id/jni_btn" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout = "true" android: layout_alignParentRight = "true" android: layout_below = "@ + id/textView2" android: layout_marginTop = "17dp" android: text = "click this btn to call the jni method"/> </RelativeLayout>

Package com. quanjin. jni; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class QuanjinJniActivity extends Activity implements OnClickListener {private static final String TAG = "MainActivity"; private Button jniButton; private TextView showJn IText; // load the shared library static {System. loadLibrary ("quanjin_jni"); // remove the shared library prefix lib/suffix so} @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_quanjin_jni); Log. d (TAG, show (); // log to view the return value. Log. d ("info_out", show (); jniButton = (Button) findViewById (R. id. jni_btn); showJniText = (TextView) findViewById (R. id. show_jni_text); jniButton. setOnClickListener (this);} // declare that the native method is implemented in jni. Private native String show (); private void callback () {Log. d (TAG, "call back from native"); // intentionally throw an exception and leave it to jni for processing. If it is processed, the java layer will not throw it. If it is not processed, the java layer throws an exception. throw new NullPointerException () ;}@ Overridepublic void onClick (View v) {if (v. getId () = R. id. jni_btn) {// display the jni return value on a specific control. ShowJniText. setText (show ());}}}

# Include <string. h> # include <jni. h> jstringJava_com_quanjin_jni_QuanjinJniActivity_show (JNIEnv * env, jobject thiz) {// obtain the type information of the input object through this method. // The object at this time, jclass jcls = (* env)-> GetObjectClass (env, thiz); // obtain the methodIDjmethodID jmId = (* env) of the callback method based on the class information) -> GetMethodID (env, jcls, "callback", "() V"); // call the callback method (* env)-> CallVoidMethod (env, thiz, jmId ); // an exception is thrown in the callback method of the Java layer. Therefore, jni calls must encounter an exception and must be checked. Handle the exception; otherwise, the exception will be thrown to the callback method of the Java layer; // at this time, the callback of the Java layer does not capture the exception, so the process will die; if (* env)-> predictioncheck (env) {(* env)-> predictiondescribe (env); (* env)-> predictionclear (env ); // clear the exception. If you cancel this sentence, the exception will be thrown back to the Java layer, that is, the Java layer will display the exception in the log ;} // respond to the Java layer call return (* env)-> NewStringUTF (env, "show message from jni by quanjin! "" \ N ABCDEFGHIGKLMNOPQRSTUVWXYZ \ n "" when you have watch the above messages, congratulations, you got it! "); // Chinese seems to be garbled-uncle's \ n Chinese: This is the jni content written by me. When you see these words, congratulations, jni call successful}

Note: If you have never touched any of your comrades, they may seem confused.Better. [continue with the blog later ];
For more information, see code comments. It mainly uses Java reflection to obtain references of objects passed in by jni, and then obtain other information;
As for how to expand the business logic, let's talk about how to expand the business logic. Then I will write a blog post. Today's content is mainly about opening up ~~~
Well, let's talk about it as much as you can. If you have a try, the complete project will not be posted. If you need further research, please leave a message with your email address.

1. I have been taking the time to study during the project.In fact, the process not only enhances your understanding of object-oriented, but also provides a more in-depth study of the Android framework source code (Java + C ++ );
It is almost the same. It is not difficult to understand the basic content. If you really want to achieve integration, it will not be easy in a short time. Please study it slowly;
2.It's a good thing ~ No matter which version you use, it's really nice to read it when you are free. Just like you are reading a martial arts secret, you don't know how to read it, but when you get fascinated, it's amazing!

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.