Android NDK Practical Learning (quad)-Class cache

Source: Internet
Author: User
Tags cdata

1, why class caching is required:

A: Because it takes a lot of time and space overhead to find classes and class member variables frequently, you can refer to the following articles:

http://www.ibm.com/developerworks/cn/java/j-jni/

Http://www.28im.com/java/a2379737.html

2, the cache needs to use static in the Java class, as follows:

1  PackageCom.dasea.test.core;2  Public classTestsetdata {3     //mainly the class ID and the field ID, the method ID of the cache4     Static {5 onnative ();6     }7 8      Public native Static voidonnative ();9 Ten      Public Booleanbdata; One      Public DoubleDData; A      Public intIData; -      Public byteCData; -      PublicString SData; the      Public bytebytearr[]; -  -      Public intintarr[]; -  +      PublicTestsetdata () { -         //TODO auto-generated Constructor stub +Bdata =true; ADData = 100.11; atIData = 333; -CData = 100; -SData = "20150204"; -Bytearr =New byte[10]; -          for(inti = 0; i < bytearr.length; i++) { -Bytearr[i] = ' 2 '; in         } -  toIntarr =New int[10]; +          for(inti = 0; i < intarr.length; i++) { -Intarr[i] = i * 10; the         } *     } $}

The relevant implementation code for the 3,c++ side:

① defines a corresponding structure:

1 struct jtestsetdata{  2    jclass   jtestsetdata; 3 4     jfieldid jbdata; 5     jfieldid jidata; 6     jfieldid jddata; 7     jfieldid jcdata; 8     jfieldid jsdata; 9     jfieldid jarrdata; Ten };

② defines the variables for the corresponding class:

struct Jtestsetdata Gs_testsetdatamgr;

③ Implement the Cache function:

 1  jniexport void   Jnicall java_com_dasea_test_core_testsetdata_onnative ( 2  Jnienv*
   
     env, Jobject obj) { 
    3  debug_out (
     " 
    testsetdata native start!  
     " 
        4   Inittestsetdata (env);  6   " testsetdata native end!   "  8 } 
   
1 BOOLInittestsetdata (jnienv*env) {2     //cache classes and their fields3     //find the field inside the class and assign the value4 5     //STEP 1/3: Load the class ID6Jclass jcsetdatamgr = Env->findclass ("Com/kq/rtk/core/testsetdata");7 8     //STEP 2/3: Assign the ClassId as a Global Reference9Gs_testsetdatamgr.jtestsetdata = (jclass) env->Newglobalref (jcsetdatamgr);Ten  OneJfieldid Funb = Env->getfieldid (Jcsetdatamgr,"bdata","Z"); AGs_testsetdatamgr.jbdata =Funb; -Gs_testsetdatamgr.jidata = Env->getfieldid (Jcsetdatamgr,"IData","I"); -Gs_testsetdatamgr.jddata = Env->getfieldid (Jcsetdatamgr,"DData","D"); theGs_testsetdatamgr.jcdata = Env->getfieldid (Jcsetdatamgr,"CData","B"); -Gs_testsetdatamgr.jsdata = Env->getfieldid (Jcsetdatamgr,"SData","ljava/lang/string;"); -Gs_testsetdatamgr.jarrdata = Env->getfieldid (Jcsetdatamgr,"Intarr","[I"); -  +     //STEP 3/3: Delete the no longer needed local reference -Env->Deletelocalref (jcsetdatamgr); +  A     return true; at}

④ using cached classes and members:

Java-side interface:

 Public native void testprecachefun (Testsetdata obj);

C + + Side implementation:

1JniexportvoidJnicall Java_com_dasea_test_core_rtknativemanager_testprecachefun (2jnienv*env, Jobject obj, Jobject jobj) {3Debug_out ("Testprecache start!");4 5     if(NULL = =gs_testsetdatamgr.jtestsetdata) {6Debug_out ("No Cache class!");7         if(false==Inittestsetdata (env)) {8Debug_out ("Cache failed!");9             return ;Ten         } OneDebug_out ("Cache success!"); A}Else{ -Debug_out ("Has cache!"); -     } the  -Env->setbooleanfield (Jobj, Gs_testsetdatamgr.jbdata,false); -Env->setdoublefield (Jobj, Gs_testsetdatamgr.jddata,209.22); -Env->setintfield (Jobj, Gs_testsetdatamgr.jidata,3653); +Env->setbytefield (Jobj, Gs_testsetdatamgr.jcdata, the); -  +Debug_out ("Set Field succ!"); A  at     Chardata[Ten] ="JFKDSAJFL"; -Jstring SSS = env->Newstringutf (data); -Env->Setobjectfield (jobj, Gs_testsetdatamgr.jsdata, SSS); -Env->Deletelocalref (SSS); -  -     //gets the object of the array property arrays in Java inJintarray Jint_arr = (jintarray) env->Getobjectfield (Jobj, gs_testsetdatamgr.jarrdata); -     intarrint[Ten] = {0}; to      for(inti =0; I <Ten; ++i) { +Arrint[i] = -+i; -     } theEnv->setintarrayregion (Jint_arr,0,Ten, arrint); *  $Debug_out ("Testprecache end!");Panax Notoginseng}

⑤ constructs a class object using a cache class:

1      Debug_out ("allocobject Object! " ); 2      " <init> " " () V " ); 3      Jobject Jresult = Env->newobject (Gs_testgetdatamgr.jtestgetdata, Initid);

Android NDK Practical Learning (quad)-Class cache

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.