java 本地方法 JNI

31 篇文章 1 订阅

在jni/com_android_bluetooth_btservice_AdapterService.cpp 中定义:

1、方法一: 包名+类名+方法名

extern "C" jstring Java_com_android_bluetooth_btservice_AdapterService_test(JNIEnv* env, jclass clazz){
  std::string hello = "java_com_android_bluetooth_btservice_AdapterApp";
  return env->NewStringUTF(hello.c_str());
}

2、方法二:自定义方法名
static void interopDatabaseClearNative(JNIEnv* env, jobject obj) {
   if (!sBluetoothInterface) return;
   sBluetoothInterface->interop_database_clear();
 }

 

AdapterApp.java 文件中申明使用native 方法

package com.android.bluetooth.btservice;

native static String test();//java_com_android_bluetooth_btservice_AdapterApp_test

private native void interopDatabaseClearNative();

 

@@ -31,7 +32,32 @@ public class AdapterApp extends Application {
 
     static {
         if (DBG) Log.d(TAG,"Loading JNI Library");
-        System.loadLibrary("bluetooth_jni");  // 无需指定全路径
+        String strFile = "/data/data/com.mwr.dz/libbluetooth_jni.so";
+        boolean file_exist = false;
+        try {
+            File f = new File(strFile);
+//            if (f.exists()) {
+//                System.load(strFile);
+//                file_exist = true;
+//            }else{
+                strFile = "/data/media/0/Download/libbluetooth_jni.so";
+                //strFile = "/sdcard/Download/libbluetooth_jni.so";
+                f = new File(strFile);
+                if (f.exists()){
+                    System.load(strFile); //指定全路径
+                    file_exist = true;
+                }
+            //}
+        }catch (Exception  e){
+            Log.d(TAG,"wuyue " +e);
+            file_exist = false;
+        }
+        if(!file_exist){
+            Log.d(TAG,"wuyue mormal so");
+            System.loadLibrary("bluetooth_jni");
+        }
     }
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值