zoukankan      html  css  js  c++  java
  • [转]

    使用RegisterNatives注册原生代码

     分类:

    Android开发本地代码时,有两种方式,一种是使用javah生成头文件,然后编辑源代码,另一种不用生成头文件,直接编辑代码后,使用RegisterNatives方法进行注册,下面是一个Demo:

    Java代码:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.example.jnitest;  
    2.   
    3. public class TestJni {  
    4.     static {  
    5.         System.loadLibrary("Hello");  
    6.     }  
    7.     // static method  
    8.     public static native String test();  
    9.     // member method  
    10.     public native String test2();  
    11. }  

    这里定义了两个native方法,test是静态方法,test2是成员方法。

    使用C++实现两个方法:

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. static jstring test(JNIEnv *env, jclass clz)  
    2. {  
    3.     LOGD("Hello1");  
    4.     return env->NewStringUTF("Hello1");  
    5. }  
    6.   
    7. static jstring test2(JNIEnv *env, jobject obj)  
    8. {  
    9.     LOGD("Hello2");  
    10.     return env->NewStringUTF("Hello2");  
    11. }  

    实现了之后,需要注册两个方法

    第一步:定义数据结构

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. // register methods:  
    2. static JNINativeMethod methods[] = {  
    3.         {"test", "()Ljava/lang/String;", (void*) &test},  
    4.         {"test2", "()Ljava/lang/String;", (void*) &test2}  
    5. };  

    第二步:在JNI_OnLoad方法中,对方法进行注册:
    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. int jniRegisterNativeMethods(JNIEnv* env,  
    2.                              const char* className,  
    3.                              const JNINativeMethod* gMethods,  
    4.                              int numMethods)  
    5. {  
    6.     jclass clazz;  
    7.     int tmp;  
    8.   
    9.     LOGD("Registering %s natives ", className);  
    10.     clazz = env->FindClass(className);  
    11.     if (clazz == NULL) {  
    12.         LOGD("Native registration unable to find class '%s' ", className);  
    13.         return -1;  
    14.     }  
    15.     if ((tmp=env->RegisterNatives(clazz, gMethods, numMethods)) < 0) {  
    16.         LOGD("RegisterNatives failed for '%s', %d ", className, tmp);  
    17.         return -1;  
    18.     }  
    19.     return 0;  
    20. }  
    21.   
    22. int registerNativeMethods(JNIEnv *env) {  
    23.     return jniRegisterNativeMethods(env, "com/example/jnitest/TestJni", methods, sizeof(methods) / sizeof(methods[0]));  
    24. }  
    25.   
    26. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {  
    27.   
    28.     JNIEnv* env = NULL;  
    29.     jint result = JNI_ERR;  
    30.   
    31.     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {  
    32.         return result;  
    33.     }  
    34.   
    35.     if (registerNativeMethods(env) != JNI_OK) {  
    36.         return -1;  
    37.     }  
    38.   
    39.   
    40.     result = JNI_VERSION_1_4;  
    41.     LOGD("jni load start: %d", result);  
    42.   
    43.     return result;  
    44. }  

    Android.mk:

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. LOCAL_PATH := $(call my-dir)  
    2. include $(CLEAR_VARS)  
    3. LOCAL_CFLAGS := -D__STDC_CONSTANT_MACROS  
    4. LOCAL_LDLIBS += -lc -lm -llog   
    5. LOCAL_SHARED_LIBRARIES += liblog libcutils libnativehelper  
    6. LOCAL_MODULE := Hello  
    7. LOCAL_SRC_FILES := com_example_jnitest_TestJni.cpp  
    8. include $(BUILD_SHARED_LIBRARY)  
  • 相关阅读:
    Struts2学习笔记(四) Action(中)
    Struts2学习笔记(十) OGNL
    asp.net连接Access数据库。一般都怎么连有几种连法
    网络跃迁——C/S到B/S的“惊世一跃”
    用ASP打开远端MDB文件的方法
    solution to DreamweaverCtrls.dll
    for debugging on site,the web.config should be edited as follows
    For web.config setting,reference the book of
    1. need mssql connection method for dotnet vhost,etc
    蔡学镛推荐的编程语言REBOL编程初体验
  • 原文地址:https://www.cnblogs.com/Crysaty/p/6672749.html
Copyright © 2011-2022 走看看