The Design of Callback between Java and Native
来源:互联网 发布:电脑摇奖软件 编辑:程序博客网 时间:2024/06/08 19:26
The Design of Callback between Java and Native
Overall Module Stack
This article is about the design of callback between Java and native modules. There is no doubt that JNI is the key man. The sample design is referenced from Multimedia framework.
Below figure is the event path from native modules to java. Let’s see the detailed process.
Init Media JNI
First step is to initialize media JNI. In android_media_MediaPlayer_native_init, JNI getspostEventFromNavtive defined in MediaPlayer (java) and assigns it topost_event of fields_t structure. JNI module will use post_event to dispatch media events to Java layer MediaPlayer in JNIMediaPlayerListener::notify.
Create JNI MediaPlayer Listener
Since MediaPlayer (java) requires to receive media events from native module, it is kind of a listener. Android registers MediaPlayer (java) as events listener in JNI duringnative_setup. A wrapper class JNIMediaPlayerListener was designed to wrap the weak reference of MediaPlayer (java) object. Actually, JNIMediaPlayerListener is the instance registered in native MedaiPlayer (cpp). Native media events are sent to MediaPlayer (java) via JNIMediaPlayerListener. Let’s take a look of it as below UML and code snippet. The member variables ofmClass and mObjectare assigned as MediaPlayer (java) class andWeakReference of MediaPlayer (java) respectively.
Below is the code snippet of native_setup implementation.
Notification flow of native media events
Finally, we can trace how a event was sent from MediaPlayer (cpp) to MediaPlayer (Java).
- The Design of Callback between Java and Native
- The Battle Between Flat Design And Skeuomorphism
- The difference of overriding between C# and Java
- the difference between state and strategy design pattern
- What is the difference between Facade and Gateway design patterns?
- Design the mutex of java
- Design the queue of java
- The distinct interpretation of the $(),${} between make and shell
- The Differences of Dietary Cultures Between China and the West
- The Art and Science of Level Design
- Software Engineering and the Art of Design
- The Differences Between Java, C And C++
- the differences between groovy and java
- native, The Keyword Of Java
- the difference between ClientID and ID of web control
- the differences of DataRelation class between 1.1 and 2.0
- String and some of the difference between StringBuffer
- the allegedly exchange of fire between north and south
- 第八周训练2鸡兔共笼
- python学习笔记(2)网络编程
- Hadoop集群搭建示意图即相应配置文件汇总
- friend function in C++
- HUD 3279 Nth Largest Value
- The Design of Callback between Java and Native
- Django runserver 支持https
- url %E4%BD%A0%E5%A5%BD 编码问题
- Ubuntu GTK theme
- TCP/IP协议栈之五---------总结
- Session理解
- springMVC Ajax实现和原理
- 搜索算法-搜索的优化
- [UPDATED!] USING YOUR NEW RASPBERRY PI 3 AS A WIFI ACCESS POINT WITH HOSTAPD