简体   繁体   English

如何从Android中的本机层文件中回调应用程序层文件?

[英]How to callback the Application Layer file from Native Layer file in a Android?

I have below three files in my Android project (one java file in Application layer, one cpp file in JNI layer & one cpp file in Native layer). 我的Android项目中有以下三个文件(“应用程序”层中的一个Java文件,“ JNI”层中的一个cpp文件和“本机”层中的一个cpp文件)。

  1. MyActivity.java (Application Layer) MyActivity.java(应用程序层)
  2. MyJNIInterface.cpp (JNI Interface) MyJNIInterface.cpp(JNI接口)
  3. MyNativeFile.cpp. MyNativeFile.cpp。 (Native implementation) (本机实施)

I am able to call a api in Native Layer from Application Layer using JNI interface. 我可以使用JNI接口从应用程序层在本机层中调用api。

Is there a way through which I can have a callback to the Application Layer file (MyActivity.java) from Native Layer file (MyNativeFile.cpp)? 有没有一种方法可以使我从本机层文件(MyNativeFile.cpp)回调到应用程序层文件(MyActivity.java)?

Please share some sample code. 请分享一些示例代码。

Yes, it is possible. 对的,这是可能的。

Check this sample app provided by Google 检查由Google提供的示例应用

Android fully implements standard JNI (except JNI_CreateJavaVM() ), so callbacks are supported. Android完全实现了标准JNI( JNI_CreateJavaVM()除外),因此支持回调。 Note that you must attach your thread to JVM if you want to call Java back from a thread that was not started in Java (eg UI thread does not need AttachCurrentThread() ). 请注意,如果要从不是在Java中启动的线程回叫Java(例如,UI线程不需要AttachCurrentThread() ),则必须将线程附加到JVM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Native:从本机层返回后如何取消分配对象 - Android Native: How to deallocate object after returning from native layer 如何本地化从图层文件创建的 Netbeans MenuBar? - How to localize a Netbeans MenuBar created from the layer file? 如何使用内层解析此json文件? - How to parse this json file with an inner layer? 如何在SpringBoot CrudRepository中将本机查询从Service层传递到Repository层 - how to pass native query from Service layer to Repository layer in SpringBoot CrudRepository Java File Storege抽象层? - Java File Storege abstraction layer? 在Spring应用程序中将服务层与Web层分离 - Separating service layer from web layer in a Spring application Spring Web Application-如何从页面控制器到业务层 - Spring Web Application - How to get from page controllers to business layer REST-应用程序层还是服务层? - REST - Application layer or Service layer? 如何从 NetCDF 文件中获取恒定时间和深度的单层纬度和经度? - How do I get single layer Latitude and Longitude for constant Time and Depth from a NetCDF file? 具有不同数据访问层后端的Android应用程序 - Android application with different data access layer backends
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM