1 package com.test.androidjni;
  2 
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.util.Log;
  6 import android.widget.TextView;
  7 
  8 
  9 public class MainActivity extends Activity {
 10     private static final String DEBUGTAG = "NATIVE";
 11     private String myString;
 12     private static int si=0;
 13     private static String helloString = "What's your name";
 14     
 15     //对基本数据类型的访问
 16     private native String GetNativeString(String str);//访问字串
 17     private native int SumArray(int [] ar, int length);//传一个数组的长度
 18     private native int SumArrayA(int [] ar);//不传长度
 19     private native int SumArray2D(int [][]ar2d);//传一个二维数组,返回一个长度    
 20     private native int [][] ModifyArray2d(int [][]ar2d);//传入一个二维数组,返回一个二维数组
 21     
 22     //这一部分是对成员和方法的访问
 23     private native void AccessField();//访问静态成员
 24     private native void AccessMethod();
 25     
 26     private native void signature(String str1, int i,byte b,char c,boolean bl,long l,double d,float f,short sh);
 27     
 28     /**
 29      * 对于加动.so库做一点说明
 30      * System.loadLibrary 这个方法默认去system/lib去找这个.so库,
 31      * 再到data/data/com.test.androidjin/lib/目录下找。本应数据存放的目录下
 32      * 我们可以通过eclipse上的ddms去查看。前提是把调试的机器联上,
 33      * 动态库的名称是:lib前缀+库名+.so后缀。此函数不要前缀,也不要后缀,只要包名,否则找不到
 34      * 但是我们在这个apk的libs/armeabi/目录下要用libmyjni.so这个全名.
 35      * 另外还有一个加载库的函数
 36      * System.load 这个函数是传入全路径名.这样就可以到指定的路径加载了
 37      * System.load(/system/lib/libXXX.so);注意访问目录的权限问题 ,这个与linux的目录访问权限一样的.
 38      * 有时候我们在使用第三访.so库是,找不到库,但明明库已加打包进了。这里我们要看它的本地方法是在
 39      * 那个java包下的那个类中声明的,比如这个apk的native方法是在
 40      * com.test.androidjni包的MainActivity 这个类中声明的。生成的native函数都有
 41      * Java_com_test_androidjni_MainActivity_这个前缀.我们可以根据第三访.so的函数接口的前缀
 42      * 建立相应的包和类名,然后就可以正确加载了。这里与前面讲的找库的路径是刚好相互应证了。
 43      * 
 44      */
 45     static{        
 46         System.loadLibrary("myjni");        
 47     }
 48     
 49     @Override
 50     protected void onCreate(Bundle savedInstanceState) {
 51         super.onCreate(savedInstanceState);
 52         setContentView(R.layout.activity_main);
 53         //字符串操作    
 54         String retStr = GetNativeString("Please give me A String");
 55         Log.d(DEBUGTAG, retStr);
 56         if(null != retStr){
 57             TextView t = (TextView)findViewById(R.id.nativeString);
 58             t.setText(retStr);
 59         }
 60         
 61         //传入一维数组,累计和,并修改了第0,第1个数组元素的值
 62         int arr[] =new int[]{1,2,3,4,5,6,7};
 63         Integer value = SumArrayA(arr);    
 64         TextView t = (TextView)findViewById(R.id.nativeArray);
 65         t.setText(value.toString());        
 66         Log.d(DEBUGTAG, "arr[0] = "+ arr[0]+" arr[1]"+arr[1]);
 67         
 68         //访问二维数组,累计和,
 69         int arr2d[][] = {{1,1,5,5},{2,2,2,2,}};//new int[2][4];
 70         int sum = SumArray2D(arr2d);        
 71         Log.d(DEBUGTAG, "len = " + sum);
 72         
 73         //访问二维数组,并返回一个二维数组,且修改了传入二维数组的元素
 74         int arr2dA[][] = null;
 75         int sumV = 0;        
 76         arr2dA = ModifyArray2d(arr2d);
 77         for(int i=0;i<2;i++)
 78         {
 79             for(int j=0;j<4;j++)
 80             {
 81                 sumV += arr2dA[i][j];                
 82             }            
 83         }    
 84         Log.d(DEBUGTAG, "sumV = " + sumV);
 85         
 86         
 87         //c中访问java域和方法
 88         this.myString = "Android Java String";
 89         this.AccessField();
 90         Log.d(DEBUGTAG, "after modify by native myString = " + myString);
 91         Log.d(DEBUGTAG, "after modify by native si = " + si);
 92         Log.d(DEBUGTAG, "after modify by native helloString = " + helloString);
 93         //c中调用java中的方法
 94         this.AccessMethod();
 95     }
 96     private void callback(String strFromNative,int i,byte b,char c,boolean bl,long l,double d,float f,short sh){
 97         Log.d(DEBUGTAG,"CALL FROM NATIVE:"+strFromNative +i+b+c+bl+l+d+f+sh);        
 98     }
 99     private  static String  callStaticBack(){
100         return    "What's your name ";
101     }
102     
103 }
MainActivity.java

相关文章:

  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
  • 2021-06-02
  • 2021-05-18
  • 2021-08-03
猜你喜欢
  • 2022-02-05
  • 2021-10-03
  • 2022-12-23
  • 2021-10-07
  • 2021-08-22
  • 2021-11-26
  • 2022-02-19
相关资源
相似解决方案