Learning Journey Based on Android NDK ----- data transmission 2 (reference data type)

Source: Internet
Author: User

Continue with the previous article. This article describes the transmission of reference data and custom objects.

 

1. Main Process
1. String transmission

A) The upper layer defines an native method and requires a String parameter to return a String

B) The JNI method corresponds to the upper layer, prints the String data transmitted from the upper layer, and returns the String data processed.

C) The upper layer receives the value returned by the native method and displays it in the UI.

 

 

2. Transmission of custom objects

A) User-Defined Object Person

B) The upper layer defines an native method. The Person parameter returns the Person value.

C) The JNI receiving object prints the relevant information data.

D) modify the Person object data in JNI and return it to the upper layer.

E) after receiving data from the upper layer, it is displayed on the UI.

 

 

2. Design and Implementation
1. The interface design is as follows:

Old look, very awkward, hey

The code is not posted here. If you need the code, you can download it directly at the end of the article.

2. Key Code Description

 

Java upper layer:

View plain
Public nativeString transferString (String mStrMSG );
Public nativeObject transferPerson (Person mPerson );
 
Person. java

View plain
Package com. duicky;
 
/**
*
*
* @ Author luxiaofeng <454162034@qq.com>
*
*/
Public class Person {
 
Private String name;
Private int age;
 
Public Person (){
Name = "";
Age = 0;
}
 
Public String getName (){
Return name;
}
 
Public void setName (String name ){
This. name = name;
}
 
Public int getAge (){
Return age;
}
 
Public void setAge (int age ){
This. age = age;
}
 
@ Override
Public String toString (){
Return "Person [name =" + name + ", age =" + age + "]";
}
 
}


 

Define two native methods. The first is used to test the transmission string, and the second is used to test the transmission of custom objects.

Android. mk File

View plain
LOCAL_PATH: = $ (call my-dir)
Include $ (CLEAR_VARS)
LOCAL_C_INCLUDES: = $ (LOCAL_PATH)/include
LOCAL_LDLIBS + =-L $ (SYSROOT)/usr/lib-llog
LOCAL_MODULE: = NDK_07
LOCAL_SRC_FILES: = \
TransmissionPerson. c \
TransmissionString. c
Include $ (BUILD_SHARED_LIBRARY)


You know it. If you don't understand it, click the introduction to the Android. mk file.

 

JNI Middle Layer

TransmissionString. c // String transmission

View plain
# Include <string. h>
# Include <jni. h>
# Include <android/log. h>
 
JNIEnv * jniEnv;
 
 
//----------------------------------------------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------
 
 
Jstring
Java_com_duicky_Transmission_transferString (JNIEnv * env, jobject thiz, jstring msg)
{
If (jniEnv = NULL ){
JniEnv = env;
}
 
Char data [128];
Memset (data, 0, sizeof (data ));
Char * c_msg = NULL;
C_msg = (char *) (* jniEnv)-> GetStringUTFChars (jniEnv, msg, 0 );
 
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "c jni ----> % s", c_msg );
 
Return (* jniEnv)-> NewStringUTF (jniEnv, "This is send by c jni ");
}

TransmissionPerson. c // custom object Transmission

 

View plain
# Include <string. h>
# Include <jni. h>
# Include <android/log. h>
 
Extern JNIEnv * jniEnv;
 
Jclass Person;
Jobject mPerson;
JmethodID getName;
JmethodID setName;
JmethodID getAge;
JmethodID setAge;
JmethodID toString;
 
Int InitPerson ();
Void ToString ();
Void GetName ();
Void GetAge ();
Void SetName ();
Void SetAge ();
//----------------------------------------------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------
 
Jobject
Java_com_duicky_Transmission_transferPerson (JNIEnv * env, jobject thiz, jobject person)
{
If (jniEnv = NULL ){
JniEnv = env;
}
 
If (Person = NULL | getName = NULL | setName = NULL | getAge = NULL
| SetAge = NULL | toString = NULL ){
If (1! = InitPerson ()){
Return NULL;
}
}
 
MPerson = person;
 
If (mPerson = NULL ){
Return NULL;
}
 
GetName ();
GetAge ();
ToString ();
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "Begin Modify mPerson ....");
SetName ();
SetAge ();
ToString ();
 
Return mPerson;
}
 
 
//----------------------------------------------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------
 
 
/**
* Initialization classes and Methods
*/
Int InitPerson (){
 
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "InitPerson Begin ");
 
If (jniEnv = NULL ){
Return 0;
}
 
If (Person = NULL ){
Person = (* jniEnv)-> FindClass (jniEnv, "com/duicky/Person ");
If (Person = NULL ){
Return-1;
}
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "InitPerson Begin 2 OK ");
}
 
If (getName = NULL ){
GetName = (* jniEnv)-> GetMethodID (jniEnv, Person, "getName", "() Ljava/lang/String ;");
If (getName = NULL ){
(* JniEnv)-> DeleteLocalRef (jniEnv, Person );
Return-2;
}
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "InitPerson Begin 4 OK ");
}
 
If (setName = NULL ){
SetName = (* jniEnv)-> GetMethodID (jniEnv, Person, "setName", "(Ljava/lang/String;) V ");
If (setName = NULL ){
(* JniEnv)-> DeleteLocalRef (jniEnv, Person );
(* JniEnv)-> DeleteLocalRef (jniEnv, getName );
Return-2;
}
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "InitPerson Begin 4 OK ");
}
 
 
If (getAge = NULL ){
GetAge = (* jniEnv)-> GetMethodID (jniEnv, Person, "getAge", "() I ");
If (getAge = NULL ){
(* JniEnv)-> DeleteLocalRef (jniEnv, Person );
(* JniEnv)-> DeleteLocalRef (jniEnv, getName );
(* JniEnv)-> DeleteLocalRef (jniEnv, setName );
Return-2;
}
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "InitPerson Begin 4 OK ");
}
 
If (setAge = NULL ){
SetAge = (* jniEnv)-> GetMethodID (jniEnv, Person, "setAge", "(I) V ");
If (setAge = NULL ){
(* JniEnv)-> DeleteLocalRef (jniEnv, Person );
(* JniEnv)-> DeleteLocalRef (jniEnv, getName );
(* JniEnv)-> DeleteLocalRef (jniEnv, setName );
(* JniEnv)-> DeleteLocalRef (jniEnv, getAge );
Return-2;
}
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "InitPerson Begin 4 OK ");
}
 
 
If (toString = NULL ){
ToString = (* jniEnv)-> GetMethodID (jniEnv, Person, "toString", "() Ljava/lang/String ;");
If (toString = NULL ){
(* JniEnv)-> DeleteLocalRef (jniEnv, Person );
(* JniEnv)-> DeleteLocalRef (jniEnv, getName );
(* JniEnv)-> DeleteLocalRef (jniEnv, setName );
(* JniEnv)-> DeleteLocalRef (jniEnv, getAge );
(* JniEnv)-> DeleteLocalRef (jniEnv, setAge );
Return-2;
}
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "InitPerson Begin 4 OK ");
}
 
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "InitPerson End ");
Return 1;
}
 
 
 
/**
* The GetName method of the Person corresponding to getName
*/
Void GetName (){
 
If (Person = NULL | getName = NULL ){
If (1! = InitPerson ()){
Return;
}
}
 
Jstring jstr = NULL;
Char * cstr = NULL;
// Call Method
Jstr = (* jniEnv)-> CallObjectMethod (jniEnv, mPerson, getName );
Cstr = (char *) (* jniEnv)-> GetStringUTFChars (jniEnv, jstr, 0 );
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "getName ----> % s", cstr );
// Release resources
(* JniEnv)-> ReleaseStringUTFChars (jniEnv, jstr, cstr );
(* JniEnv)-> DeleteLocalRef (jniEnv, jstr );
 
}
 
/**
* GetAge: getName method of Person
*/
Void GetAge (){
 
If (Person = NULL | getName = NULL ){
If (1! = InitPerson ()){
Return;
}
}
// Call Method
Jint age = (* jniEnv)-> CallIntMethod (jniEnv, mPerson, getAge );
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "getAge ----> % d", age );
 
}
 
/**
* SetName: setName Method for Person
*/
Void SetName (){
 
If (Person = NULL | setName = NULL ){
If (1! = InitPerson ()){
Return;
}
}
 
Jstring jstr = (* jniEnv)-> NewStringUTF (jniEnv, "Modify Name ");
// Call Method
(* JniEnv)-> CallVoidMethod (jniEnv, mPerson, setName, jstr );
(* JniEnv)-> DeleteLocalRef (jniEnv, jstr );
 
}
 
Int age = 20;
/**
* SetAge: setAge Method for Person
*/
Void SetAge (){
 
If (Person = NULL | setAge = NULL ){
If (1! = InitPerson ()){
Return;
}
}
// Call Method
(* JniEnv)-> CallVoidMethod (jniEnv, mPerson, setAge, age ++ );
 
}
 
 
/**
* ToString corresponds to the toString method of Person, and relevant information is printed.
*/
Void ToString (){
 
If (Person = NULL | toString = NULL ){
If (1! = InitPerson ()){
Return;
}
}
 
Jstring jstr = NULL;
Char * cstr = NULL;
// Call Method
Jstr = (* jniEnv)-> CallObjectMethod (jniEnv, mPerson, toString );
Cstr = (char *) (* jniEnv)-> GetStringUTFChars (jniEnv, jstr, 0 );
_ Android_log_print (ANDROID_LOG_INFO, "JNIMsg", "c jni toString ----> % s", cstr );
(* JniEnv)-> ReleaseStringUTFChars (jniEnv, jstr, cstr );
(* JniEnv)-> DeleteLocalRef (jniEnv, jstr );
 
}


3. Running result
A. test String transmission: Click the first button to view the UI display and LogCat printing information.

1. JNI receives Java Information


2. Java receives JNI Information


3. UI display information

 

 

 

 

B. Test custom object transmission: Click the second button to view the UI display and LogCat printing information.

1. JNI receives the data of the Person object (Java writes dead sending name: duicky, age: 10)

 

2. JNI uses setName and SetAge to change the data of a Person.


3. Data received at the upper layer of Java

4. UI display information

 

 

 

The above is a small example of transmission of Java --- JNI String and custom objects. Other reference data types and Java custom data can be transmitted in accordance with the above practice.

 

Another way is to transfer big data by saving it to a file. For example, the upper layer stores data to the file, and then the c jni layer directly reads the file or the c jni layer stores the data to the file for reading by the upper layer.

 

 

If you have any questions, please leave a message. Your personal skills are limited. If you have any mistakes, please point out that you are not comprehensive enough. Thank you,

 

Download source code data transmission 2

 

This article is from duicky's blog

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.