首页 \ 问答 \ 如何在JNI中从C ++调用Java方法(How to call Java methods from C++ in JNI)

如何在JNI中从C ++调用Java方法(How to call Java methods from C++ in JNI)

所以我正在编写一个使用大型c ++库的Android应用程序。 我有一切工作,以便java应用程序可以调用c ++委托方法,但我发现自己希望我可以将消息从c ++记录到Android日志。 这很容易从java中,但我不知道如何从c ++调用java方法。 我的搜索找到了从c ++中打开jvm的方法,这完全不是我想要做的。 理想情况下,我想将一个日志方法指针传递给c ++,然后可以随时使用它。 当然,java不支持方法指针。 我的java方法看起来像这样:

private void log(String s){
   Log.i(Tag, s);     // Android log
}

我只是不知道如何让c ++来访问这个方法。


So I'm writing an Android app which uses a large c++ library. I have everything working so that the java app can call the c++ delegation methods, but I'm finding myself wishing I could log messages from c++ to the Android log. This is easy from java, but I'm at a loss as to how to call a java method from c++. My searches found methods for opening a jvm from c++, which is not at all what I want to do. Ideally, I'd like to pass a log method pointer to c++, which could then be used whenever I wanted. Of course, java doesn't support method pointers. My java method would look something like:

private void log(String s){
   Log.i(Tag, s);     // Android log
}

I just don't know how to allow c++ to access this method.


原文:https://stackoverflow.com/questions/3902066
更新时间:2024-01-29 08:01

最满意答案

迭代器(在你的情况下int i )每次迭代增加1。 如果幻数无效,您可以将迭代器递减1。

if (guess > randomNumber && guess <= 100){
    System.out.println("Your guess, "+guess+", is greater than the magic number.");
    // If the value of i at this point is 1
    i--; //Decrement i by 1
    // Now the value of i is 0. The value of i in the next iteration will be 1
}
else if (guess < randomNumber && guess > 0){
    System.out.println("Your guess, "+guess+", is less than the magic number.");
    // If the value of i at this point is 1
    i--; //Decrement i by 1
    // Now the value of i is 0. The value of i in the next iteration will be 1
}

The iterator (in your case int i) increment by 1 for each iteration. You can decrement your iterator by 1 if the magic number is invalid.

if (guess > randomNumber && guess <= 100){
    System.out.println("Your guess, "+guess+", is greater than the magic number.");
    // If the value of i at this point is 1
    i--; //Decrement i by 1
    // Now the value of i is 0. The value of i in the next iteration will be 1
}
else if (guess < randomNumber && guess > 0){
    System.out.println("Your guess, "+guess+", is less than the magic number.");
    // If the value of i at this point is 1
    i--; //Decrement i by 1
    // Now the value of i is 0. The value of i in the next iteration will be 1
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 以编程方式创建视频?(Create videos programmatically?)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • javascript数组,如何改变这个数组结构(javascript arrays, how to change this array structure)
  • 在ASP.NET Web API中使用多个Get方法进行路由(Routing with multiple Get methods in ASP.NET Web API)
  • 用于backbone.js验证的自定义验证器(Custom validator for backbone.js validation)
  • const char *与其他指针有什么不同?(Is const char * different from other pointers? [duplicate])
  • 无效的列索引,使用PreparedStatement更新(Invalid column index , update using PreparedStatement)
  • watchOS WCSession'已配对'和'watchAppAvailable'不可用(watchOS WCSession 'paired' and 'watchAppAvailable' are unavailable)
  • CalledFromWrongThreadException在Android上执行JUnit测试(CalledFromWrongThreadException exercising JUnit tests on Android)
  • 如何把文件保存到你的应用程序目录中?(How to put\save files into your application directory? (adobe air))
  • 美元符号在Java方法描述符中的含义?(Meanings of dollar sign in Java method descriptor?)
  • font-size的含义是什么:1em / 2em?(What doe the meaning of font-size:1em/2em?)
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • Android - 检测与特定wifi ssid断开连接的正确方法?(Android - Correct way to detect disconnecting from a particular wifi ssid?)
  • 通过Shell脚本将文件转换为另一个文件(Convert File To Another File By Shell Script)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • 如何过滤magento废弃的购物车报告集合(How to Filter the magento abandoned cart report collection)
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • web api http post传递对象504接收失败(web api http post passing object 504 Receive Failure)
  • Rails从视图编辑模型上的多个属性的方法(Rails way to edit multiple attributes on a model from a view)
  • 总是用{}初始化对象是否是好习惯?(Is it good habit to always initialize objects with {}?)
  • 在方案中编写特殊字符到输出端口(编译器设计)(writing special characters to output port in scheme (compiler design))
  • 电脑等级考试得证有多大用处?
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • 第一次调用函数将无法按预期工作,但下一次工作正常(calling a function on the first time won't work as expected, but next time is working)
  • 如何优化使用BigInteger操作执行时间的代码(How to optimize the code that uses BigInteger operations for execution time)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何提供个人资料信息,以便Passport.js可以使用它?(how does Profile information should be provided so Passport.js can use it?)
  • 有没有办法初始化jquery数据表中的细节?(is there any way to initialize details in jquery datatable?)