JNI_OnLoadJNI中HelloWorld的C和C++实现

Android移植---JNI中HelloWorld的C和C++实现

分类: Android移植   314人阅读  评论(0)  收藏  举报

MainActivity.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.jniload;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.widget.TextView;  
  7.   
  8. public class MainActivity extends Activity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         TextView  tv = new TextView(this);  
  14.         tv.setText( stringFromJNI() );  
  15.         setContentView(tv);  
  16.         //setContentView(R.layout.activity_main);  
  17.     }  
  18.   
  19.     @Override  
  20.     public boolean onCreateOptionsMenu(Menu menu) {  
  21.         // Inflate the menu; this adds items to the action bar if it is present.  
  22.         getMenuInflater().inflate(R.menu.main, menu);  
  23.         return true;  
  24.     }  
  25.     native String stringFromJNI();  
  26.     static{  
  27.         System.loadLibrary("jniload");  
  28.     }  
  29. }  
test-jni.c

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <jni.h>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <assert.h>  
  5.   
  6. # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))  
  7. #define JNIREG_CLASS "com/example/test/Test"  
  8.   
  9. JNIEXPORT jstring JNICALL stringFromJNI(JNIEnv *env, jclass clazzi)  
  10. {  
  11.     return (*env)->NewStringUTF(env, "hello Android3.");  
  12. }  
  13.   
  14. static JNINativeMethod method_table[] = {  
  15.     { "stringFromJNI""()Ljava/lang/String;", (void*)stringFromJNI },//绑定  
  16. };  
  17.   
  18. static int registerNativeMethods(JNIEnv* env, const char* className,  
  19.         JNINativeMethod* gMethods, int numMethods)  
  20. {  
  21.     jclass clazz;  
  22.     clazz = (*env)->FindClass(env, className);  
  23.     if (clazz == NULL) {  
  24.         return JNI_FALSE;  
  25.     }  
  26.     if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) {  
  27.         return JNI_FALSE;  
  28.     }  
  29.   
  30.     return JNI_TRUE;  
  31. }  
  32.   
  33. int register_ndk_load(JNIEnv *env)  
  34. {  
  35.     return registerNativeMethods(env, JNIREG_CLASS,  
  36.             method_table, NELEM(method_table));  
  37. }  
  38.   
  39. JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)  
  40. {  
  41.     JNIEnv* env = NULL;  
  42.     jint result = -1;  
  43.   
  44.     if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) {  
  45.         return result;  
  46.     }  
  47.   
  48.     register_ndk_load(env);  
  49.   
  50.     return JNI_VERSION_1_4;  
  51. }  
jni-load.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <jni.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <assert.h>  
  6. #define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))  
  7. #define JNIREG_CLASS "com/example/jniload/MainActivity"  
  8.   
  9. extern "C"  
  10.   
  11. /*jstring Java_com_example_jniload_MainActivity_stringFromJNI( JNIEnv* env,jobject thiz ) 
  12. { 
  13.     char* str="Hello from JNI."; 
  14.     return (env)->NewStringUTF(str); 
  15. }*/  
  16. JNIEXPORT jstring JNICALL stringFromJNI( JNIEnv* env,jobject thiz )  
  17. {  
  18.     char* str="Hello from JNI.";  
  19.     return (env)->NewStringUTF(str);  
  20. }  
  21.   
  22. static JNINativeMethod method_table[] = {  
  23.     { "stringFromJNI""()Ljava/lang/String;", (void*)stringFromJNI },  
  24. };  
  25.   
  26. static int registerNativeMethods(JNIEnv* env, const char* className,  
  27.         JNINativeMethod* gMethods, int numMethods)  
  28. {  
  29.     jclass clazz;  
  30.     clazz = env->FindClass(className);  
  31.     if (clazz == NULL) {  
  32.         return JNI_FALSE;  
  33.     }  
  34.     if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {  
  35.         return JNI_FALSE;  
  36.     }  
  37.   
  38.     return JNI_TRUE;  
  39. }  
  40.   
  41. int register_ndk_load(JNIEnv *env)  
  42. {  
  43.    return registerNativeMethods(env, JNIREG_CLASS, method_table, NELEM(method_table));  
  44. }  
  45.   
  46. jint JNI_OnLoad(JavaVM* vm, void* reserved)  
  47. {  
  48.     JNIEnv* env = NULL;  
  49.     jint result = -1;  
  50.   
  51.     if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {  
  52.         return result;  
  53.     }  
  54.     //result=register_ndk_load(env);  
  55.   
  56.     if ( register_ndk_load(env) < 0){  
  57.         goto bail;  
  58.     }  
  59.     result = JNI_VERSION_1_4;  
  60.   
  61. bail:  
  62.     return result;  
  63. }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值