您好,登錄后才能下訂單哦!
Android中JNI如何使用,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
首先我們需要把C語言寫的功能類放入我們的項目中。這里我直接從資料中找了一個,畢竟我不會寫。路徑在src/main/jni中
find_name.cpp
#include <jni.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/select.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define send_MAXSIZE 50 #define recv_MAXSIZE 1024 struct NETBIOSNS { unsigned short int tid; //unsigned short int 占2字節 unsigned short int flags; unsigned short int questions; unsigned short int answerRRS; unsigned short int authorityRRS; unsigned short int additionalRRS; unsigned char name[34]; unsigned short int type; unsigned short int classe; }; char *getNameFromIp(const char *ip); extern "C" jstring Java_com_hao_cmake_MainActivity_cpuFromJNI(JNIEnv* env, jobject thiz, jstring ip) { const char* str_ip; str_ip = env->GetStringUTFChars(ip, 0); return env->NewStringUTF(getNameFromIp(str_ip)); } char *getNameFromIp(const char *ip) { char str_info[1024] = { 0 }; struct sockaddr_in toAddr; //sendto中使用的對方地址 struct sockaddr_in fromAddr; //在recvfrom中使用的對方主機地址 char send_buff[send_MAXSIZE]; char recv_buff[recv_MAXSIZE]; memset(send_buff, 0, sizeof(send_buff)); memset(recv_buff, 0, sizeof(recv_buff)); int sockfd; //socket unsigned int udp_port = 137; int inetat; if ((inetat = inet_aton(ip, &toAddr.sin_addr)) == 0) { sprintf(str_info, "[%s] is not a valid IP address\n", ip); return str_info; } if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { sprintf(str_info, "%s socket error sockfd=%d, inetat=%d\n", ip, sockfd, inetat); return str_info; } bzero((char*) &toAddr, sizeof(toAddr)); toAddr.sin_family = AF_INET; toAddr.sin_addr.s_addr = inet_addr(ip); toAddr.sin_port = htons(udp_port); //構造netbios結構包 struct NETBIOSNS nbns; nbns.tid = 0x0000; nbns.flags = 0x0000; nbns.questions = 0x0100; nbns.answerRRS = 0x0000; nbns.authorityRRS = 0x0000; nbns.additionalRRS = 0x0000; nbns.name[0] = 0x20; nbns.name[1] = 0x43; nbns.name[2] = 0x4b; int j = 0; for (j = 3; j < 34; j++) { nbns.name[j] = 0x41; } nbns.name[33] = 0x00; nbns.type = 0x2100; nbns.classe = 0x0100; memcpy(send_buff, &nbns, sizeof(nbns)); int send_num = 0; send_num = sendto(sockfd, send_buff, sizeof(send_buff), 0, (struct sockaddr *) &toAddr, sizeof(toAddr)); if (send_num != sizeof(send_buff)) { sprintf(str_info, "%s sendto() error sockfd=%d, send_num=%d, sizeof(send_buff)=%d\n", ip, sockfd, send_num, sizeof(send_buff)); shutdown(sockfd, 2); return str_info; } int recv_num = recvfrom(sockfd, recv_buff, sizeof(recv_buff), 0, (struct sockaddr *) NULL, (socklen_t*) NULL); if (recv_num < 56) { sprintf(str_info, "%s recvfrom() error sockfd=%d, recv_num=%d\n", ip, sockfd, recv_num); shutdown(sockfd, 2); return str_info; } //這里要初始化。因為發現linux和模擬器都沒問題,真機上該變量若不初始化,其值就不可預知 unsigned short int NumberOfNames = 0; memcpy(&NumberOfNames, recv_buff + 56, 1); char str_name[1024] = { 0 }; unsigned short int mac[6] = { 0 }; int i = 0; for (i = 0; i < NumberOfNames; i++) { char NetbiosName[16]; memcpy(NetbiosName, recv_buff + 57 + i * 18, 16); //依次讀取netbios name if (i == 0) { sprintf(str_name, "%s", NetbiosName); } } sprintf(str_info, "%s|%s|", ip, str_name); for (i = 0; i < 6; i++) { memcpy(&mac[i], recv_buff + 57 + NumberOfNames * 18 + i, 1); sprintf(str_info, "%s%02X", str_info, mac[i]); if (i != 5) { sprintf(str_info, "%s-", str_info); } } return str_info; }
這里要注意一點,jstring Java_com_hao_cmake_MainActivity_cpuFromJNI方法中,com_hao_cmake是我們的包名,MainActivity是調用JNI的Activity名稱,cpuFromJNI是對應方法的名字。
這個文件也是放在jni文件夾中
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # 指定so庫文件的名稱 LOCAL_MODULE := jni_mix # 指定需要編譯的源文件列表 LOCAL_SRC_FILES := find_name.cpp # 指定C++的編譯標志 LOCAL_CPPFLAGS += -fexceptions # 指定要加載的靜態庫 #LOCAL_WHOLE_STATIC_LIBRARIES += android_support # 指定需要鏈接的庫 LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY) $(call import-module, android/support)
android -> defaultConfig 下添加
externalNativeBuild{ ndkBuild{ abiFilters "arm64-v8a","armeabi-v7a" } }
android 下添加
externalNativeBuild { ndkBuild { path file('src/main/jni/Android.mk') } } packagingOptions{ pickFirst 'lib/arm64-v8a/libjni_mix.so' pickFirst 'lib/armeabi-v7a/libjni_mix.so' }
在build → intermediates → ndkBuild → debug → obj → local下,我們可以找到我們生成的相關配置平臺的so文件
這個樣子的
public class MainActivity extends AppCompatActivity { public native String cpuFromJNI(String ip); static { System.loadLibrary("jni_mix"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String str = cpuFromJNI("192.168.0.163"); Toast.makeText(this,str,Toast.LENGTH_SHORT).show(); } }
這樣我們就完成了用C語言類生成so包,并使用JNI進行調用的全流程。
注意:在使用JNI進行調用的時候,我們的環境一定要有NDK,這個我這里就不說了,大家如果沒有搭建需要上網找找搭建一下。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。