JNIEXPORT jstring JNICALL Java_com_imobile_thermalprinterwifiswitch_imobileJNI_WriteProc(JNIEnv *env, jclass arg, jstring Write_Path_from_JNI, jstring Write_value_from_JNI) { char* Write_Path = Jstring2CStr(env,Write_Path_from_JNI); char* Write_value = Jstring2CStr(env,Write_value_from_JNI); char* status = ""; //LOGI("[Po add] WriteProc() Write_Path=%s", Write_Path); //LOGI("[Po add] WriteProc() Write_value=%s", Write_value); //LOGI("[Po add] WriteProc() status=%s", status); FILE *f = fopen(Write_Path, "w"); if (f == NULL) { LOGI("[Po add] WriteProc() error!"); status = "xx"; exit(1); } /* print some text */ const char *text = Write_value; fprintf(f, "%s\n", text); fclose(f); status = "oo"; //LOGI("[Po add] TransportStringToC() %s", p); //Po log start LOGI("[Po add] WriteProc() %s", status); //Po log end return (*env)->NewStringUTF(env, status); }
JNIEXPORT jstring JNICALL Java_com_example_webdemo_util_H5Decode_decode( JNIEnv* env, jobject obj, jstring in, jstring account, jstring device) { char* cin = Jstring2CStr(env, in); char* cAccount = Jstring2CStr(env, account); char* cDeviceId = Jstring2CStr(env, device); //输入///////////////// LOGI("输入内容=%s", cin); LOGI("输入帐号=%s", cAccount); LOGI("输入deviceid=%s", cDeviceId); char hello[7] = { ',', 'h', 'e', 'l', 'l', 'o', '\0' }; strcat(cin, hello); LOGI("处理后内容=%s", cin); MD5_CTX context = { 0 }; MD5Init(&context); MD5Update(&context, "123456", 6); unsigned char d[16] = { 0 }; MD5Final(d, &context); int i; char buffer[80]; char destination[25]; for(i=0;i<16;i++) { sprintf(buffer, "%s%02x", buffer, d[i]); // LOGI("%02x",d[i]); } //输出///////////////// return (*env)->NewStringUTF(env, buffer); }
JNIEXPORT jstring JNICALL Java_com_util_dns_DnsUtil_getPtrRecord(JNIEnv *env, jobject obj, jstring str, jstring server) { char* p = Jstring2CStr(env, str); char* s = Jstring2CStr(env, server); LOGI("getPtrRecord:%s", p); my_dns_query(DNS_T_PTR,p,"send-query", s); return (*env)->NewStringUTF(env, dns_txt_record); }
JNIEXPORT jstring JNICALL Java_com_util_dns_DnsUtil_getTxtRecord(JNIEnv *env, jobject obj, jstring str) { char* p = Jstring2CStr(env, str); LOGI("getTxtRecord:%s", p); my_dns_query(DNS_T_TXT,"all","send-query", p); return (*env)->NewStringUTF(env, dns_txt_record); }
//com.yaozu.listener.playlist.provider JNIEXPORT void JNICALL Java_com_yaozu_listener_playlist_provider_NativeMediaScanner_processFile (JNIEnv *env, jobject obj, jstring path,jobject client) { if (!mMediaScanner) { mMediaScanner = new MediaScanner(); } char *cpath = Jstring2CStr(env, path); MyMediaScannerClient myClient(env,client); mMediaScanner->processFile(cpath, myClient); }
JNIEXPORT jstring JNICALL Java_com_yaozu_listener_HomeMainActivity_fromC (JNIEnv *env, jobject obj, jstring path) { if (!mMediaScanner) { mMediaScanner = new MediaScanner(); } char *cpath = Jstring2CStr(env, path); //MyMediaScannerClient myClient(env); //mMediaScanner->processFile(cpath, myClient); const char *cstr = ": c from char"; return (env)->NewStringUTF(strcat(cpath, cstr)); }
JNIEXPORT jstring JNICALL Java_com_ooieuioo_potestjni_PoJNITtest_TransportStringToC(JNIEnv *env, jclass arg, jstring str) { char* p = Jstring2CStr(env,str); char* Po_str = " from PoJNI!!"; char* Po_marge = strcat(p,Po_str); //Po log start //LOGI("[Po add] TransportStringToC() %s", Po_marge); //Po log end return (*env)->NewStringUTF(env, Po_marge); }
/** * 返回数据str加上特定字符串后的字符串值。 (字符串拼接) */ JNIEXPORT jstring JNICALL Java_com_example_jnidemo2_DataProvider_returnStr (JNIEnv * env, jobject obj, jstring str) { char * source = "Hello"; char * destination = Jstring2CStr(env,str);//将Java中String类型的参数转为char *。 strcat(destination,source); return (*env)->NewStringUTF(env, destination);//将C语言中的char *转为String。 }
jstring AddString(JNIEnv *env, jstring a, jstring b) { char *str_a = Jstring2CStr(env, a); char *str_b = Jstring2CStr(env, b); int len_a = strlen(str_a); int len_b = strlen(str_b); //concat string char *str_result = (char *) malloc(len_a + len_b + 1); strcpy(str_result, str_a); strcat(str_result, str_b); jstring jstr_result = (*env)->NewStringUTF(env, str_result); //创建string对象 //delete[] str_result; (*env)->ReleaseStringUTFChars(env, a, str_a); (*env)->ReleaseStringUTFChars(env, b, str_b); (*env)->ReleaseStringUTFChars(env, jstr_result, str_result); return jstr_result; //返回ab字符串连接之后的结果 }
JNIEXPORT jstring JNICALL Java_com_imobile_thermalprinterwifiswitch_imobileJNI_ReadProc(JNIEnv *env, jclass arg, jstring str) { char* origin_path = Jstring2CStr(env,str); char* p = ""; #define CHUNK 1024 /* read 1024 bytes at a time */ char buf[CHUNK]; FILE *file; size_t nread; file = fopen(origin_path, "r"); if (file) { while ((nread = fread(buf, 1, sizeof buf, file)) > 0) fwrite(buf, 1, nread, stdout); if (ferror(file)) { /* deal with error */ } fclose(file); } LOGI("[Po add] ReadProc() %s", buf); char *test = strtok(buf, "\n"); // while (test != NULL) { // LOGI("[Po add] TransportStringToC() test=%s", test); // } if(strcmp(test, "1") == 0){ p = test; }else if(strcmp(test, "0") == 0){ p = test; }else{ p = "xx"; } //LOGI("[Po add] TransportStringToC() %s", p); //Po log start //LOGI("[Po add] TransportStringToC() %s", buf); //Po log end return (*env)->NewStringUTF(env, p); }
JNIEXPORT jstring JNICALL Java_com_rockgarden_sign_jni_JniSignHolder_getInfoMD5( JNIEnv *env, jobject thiz, jstring jInfo) { char *jstr = Jstring2CStr(env, jInfo); MD5_CTX context = {0}; MD5Init(&context); MD5Update(&context, jstr, strlen(jstr)); unsigned char dest[16] = {0}; MD5Final(dest, &context); int i; char destination[32] = {0}; for (i = 0; i < 16; i++) { sprintf(destination, "%s%02x", destination, dest[i]); } LOGI("%s", destination); return (*env)->NewStringUTF(env, destination); }
JNIEXPORT void JNICALL Java_com_example_administrator_topactivity_service_DaemonService_initDaemon( JNIEnv * env, jobject obj, jstring packageDir, jint sdkVersion) { // 1,将传递过来的java的包名转为c的字符串 char * pd = Jstring2CStr(env, packageDir); // 2,创建当前进程的克隆进程 pid_t pid = fork(); // 3,根据返回值的不同做不同的操作,<0,>0,=0 if (pid < 0) { // 说明克隆进程失败 LOGD("current crate process failure"); } else if (pid > 0) { // 说明克隆进程成功,而且该代码运行在父进程中 LOGD("crate process success,current parent pid = %d", pid); } else { // 说明克隆进程成功,而且代码运行在子进程中 LOGD("crate process success,current child pid = %d", pid); // 4,在子进程中监视/data/data/包名这个目录 while (JNI_TRUE) { FILE* file = fopen(pd, "rt"); if (file == NULL) { // 应用被卸载了,通知系统打开用户反馈的网页 LOGD("app uninstall,current sdkversion = %d", sdkVersion); if (sdkVersion >= 17) { // Android4.2系统之后支持多用户操作,所以得指定用户 execlp("am", "am", "start", "--user", "0", "-a","android.intent.action.VIEW", "-d","http://www.baidu.com", (char*) NULL); } else { // Android4.2以前的版本无需指定用户 execlp("am", "am", "start", "-a","android.intent.action.VIEW", "-d","http://www.baidu.com", (char*) NULL); } } else { // 应用没有被卸载 LOGD("app run normal"); } sleep(1); } } }
JNICALL Java_com_charonchui_daemonservice_service_DaemonService_startDaemon (JNIEnv *env, jobject thiz, jstring serviceName, jint sdkVersion) { char *name = Jstring2CStr(env, serviceName); //fork子进程,以执行轮询任务 pid_t pid = fork(); LOGI("fork=%d", pid); if (pid < 0) { // fork失败了 } else if (pid == 0) { // 可以一直采用一直判断文件是否存在的方式去判断,但是这样效率稍低,下面使用监听的方式,死循环,每个一秒判断一次,这样太浪费资源了。 int check = 1; while (check) { pid_t ppid = getppid(); LOGI("pid=%d", getpid()); LOGI("ppid=%d", ppid); if (ppid == 1) { LOGI("ppid == 1"); if (sdkVersion >= 17) { LOGI("> 17"); int ret = execlp("am", "am", "startservice", "--user", "0", "-n", name, (char *) NULL); } else { execlp("am", "am", "startservice", "-n", name, (char *) NULL); LOGI("else"); } check = 0; } else { } sleep(1); } } else { LOGI("parent .. pid=%d", pid); //父进程直接退出,使子进程被init进程领养,以避免子进程僵死 } }
// JNIEnv *env, jobject thiz 默认传入 // 被调用的方法要放在前面,否则报错:conflicting types for "方法名" JNIEXPORT jstring JNICALL Java_com_rockgarden_sign_jni_JniSignHolder_getCustomInfoMD5( JNIEnv *env, jobject thiz, jobject ctx, jstring jInfo) { jstring pkg_name = Java_com_rockgarden_sign_jni_Jni_GetPackageName(env, thiz, ctx); char *c1 = Jstring2CStr(env, pkg_name); char *c2 = "com.eastcom.mobile112"; int result = strcmp(c1, c2); if (result == 0) { char *str = "AD161C0F-D5B5-86F2-6DCF-7D9ADCBC1AC7"; jstring js = (*env)->NewStringUTF(env, str); //(*env)->ReleaseStringUTFChars(env, js, str); jstring newjs = AddString(env, jInfo, js); return Java_com_rockgarden_sign_jni_JniSignHolder_getInfoMD5(env, thiz, newjs); } else { char *c = ""; jstring js = (*env)->NewStringUTF(env, c); //(*env)->ReleaseStringUTFChars(env, js, c); return js; } }