{"id":288124,"date":"2018-08-16T16:35:04","date_gmt":"2018-08-16T12:35:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=288124"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=288124","title":{"rendered":"\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u201c\u041d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044c-\u041f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u201d \u0441 NDK \u0438 callback \u0432 Android"},"content":{"rendered":"\n<div data-io-article-url=\"https:\/\/habr.com\/post\/420389\/\" class=\"post__text post__text-html js-mediator-article\">\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432 Android (NDK) JNI callbacks, \u043f\u0430\u0442\u0442\u0435\u0440\u043d &#171;<a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9D%D0%B0%D0%B1%D0%BB%D1%8E%D0%B4%D0%B0%D1%82%D0%B5%D0%BB%D1%8C_(%D1%88%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD_%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F)\">\u041d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044c<\/a>-\u041f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a&#187; \u0441 NDK \u0438 callback, \u0441\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0439 EventBus \u0438\u043b\u0438 Rx <\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<pre><code>                     \u2026. \u0414\u043e\u0441\u0442\u0430\u043b\u043e \u043c\u0435\u043d\u044f \u044d\u0442\u043e \u00ab\u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0442 \u0434\u0435\u0442\u0430\u043b\u0435\u0439, \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0445                        \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c\u00bb. \u0425\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u0447\u0442\u043e \u0436\u0435 \u0442\u0430\u043c\u00a0\u0435\u0441\u0442\u044c.                         \u2013\u00a0\u0420\u0443\u0441\u0441\u043a\u0430\u044f \u043c\u0430\u0442\u0440\u0435\u0448\u043a\u0430 \u0434\u043e \u0441\u0430\u043c\u043e\u0439 \u0433\u043b\u0443\u0431\u0438\u043d\u044b. \u041f\u0440\u0430\u0432\u0434\u0430,                         \u041e\u0440\u043e\u0441\u043a\u043e? \u0425\u0443\u0430\u043d \u043d\u0435 \u0441\u0442\u0430\u043b \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0440\u0443\u0441\u0441\u043a\u0430\u044f                         \u043c\u0430\u0442\u0440\u0435\u0448\u043a\u0430.                      \u2013\u00a0\u0414\u0430 \u044d\u0442\u043e \u0436\u0435 \u043c\u0443\u0441\u043e\u0440, \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u043e\u0440 \u0413\u0443. \u041a\u043e\u043c\u0443 \u043e\u043d\u043e \u043d\u0430\u0434\u043e \u2013 \u0441 \u0442\u0430\u043a\u0438\u043c                       \u0432\u043e\u0437\u0438\u0442\u044c\u0441\u044f?                          \"\u041a\u043e\u043d\u0435\u0446 \u0440\u0430\u0434\u0443\u0433\" \u0412\u0438\u043d\u0434\u0436 \u0412\u0435\u0440\u043d\u043e\u0440<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043f\u043e\u0434 Android, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0432\u043c\u0435\u0449\u0430\u044e\u0442 C++ \u0438 Java \u043a\u043e\u0434. Java \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043e\u0431\u0435\u0440\u0442\u043a\u043e\u0439\/\u043f\u0440\u043e\u0441\u043b\u043e\u0439\u043a\u043e\u0439, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0443, \u0430 C++ \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0432\u0441\u044e \u0433\u0440\u044f\u0437\u043d\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u043f\u043e \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044e, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0430\u0443\u0434\u0438\u043e, \u0430 \u043d\u0430\u0432\u0435\u0440\u0445 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b. \u041d\u0443 \u0438 \u0442\u0430\u043a \u043a\u0430\u043a RX \u044d\u0442\u043e \u0443\u0436\u0435 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e, \u0442\u043e \u0420\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u0430 \u00ab\u041d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044c\u00bb \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445. \u041c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c ReactiveX \u0438\u043b\u0438 EventBus \u0438 \u043d\u0435 \u0437\u0430\u043c\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c\u0441\u044f, \u043d\u043e \u0432\u0441\u0435-\u0442\u0430\u043a\u0438 \u0438\u043d\u043e\u0433\u0434\u0430 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0434\u0430 \u0438 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0435 \u0442\u0443\u0434\u0430 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0447\u0435\u0440\u0435\u0437 JNI \u043e\u043d\u0438 \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u043f\u043e\u043c\u043e\u0433\u0443\u0442. \u0414\u0430 \u0438 \u043b\u0443\u0447\u0448\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0447\u0435\u043c\u0443-\u043d\u0438\u0431\u0443\u0434\u044c \u2014 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0441\u0432\u043e\u0438\u043c\u0438 \u0440\u0443\u043a\u0430\u043c\u0438.<br \/>  \u041f\u043e\u043b\u043d\u044b\u0439 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043a\u043e\u0434 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043d\u0430<a href=\"https:\/\/github.com\/NickZt\/MyJNACallbackTest\"> GitHub<\/a>. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0434\u0435\u0440\u0436\u043a\u0438 \u0438\u0437 \u043d\u0435\u0433\u043e.<\/p>\n<p>  <\/p>\n<p><strong>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043e\u0440\u0438\u0438 \u0438 \u0438\u0441\u0442\u043e\u0440\u0438\u0438<\/strong><\/p>\n<p>  <\/p>\n<p>\u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0431\u044b\u043b \u043d\u0430 \u043c\u0438\u0442\u0430\u043f\u0435 \u043f\u043e RX \u0438 \u0431\u044b\u043b \u043f\u043e\u0440\u0430\u0436\u0435\u043d \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043e\u0431: \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u043e, \u0438 \u043a\u0430\u043a \u044d\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0422\u0430\u043a \u0447\u0442\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043e\u0440\u0438\u0438: \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u00a0&#171;<a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9D%D0%B0%D0%B1%D0%BB%D1%8E%D0%B4%D0%B0%D1%82%D0%B5%D0%BB%D1%8C_(%D1%88%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD_%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F)\">\u041d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044c<\/a>-\u041f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a&#187; \u2014 \u044d\u0442\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0443 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043e\u043f\u043e\u0432\u0435\u0449\u0435\u043d\u0438\u044f \u043e\u0431 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0437\u0430 \u043d\u0438\u043c\u0438. \u0414\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u0438 \u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u044b\u043c\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u042f\u0440\u043a\u0438\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f\u0437\u044b\u043a\u043e\u0432\u0430\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u043d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0435\u043c \u2013 Smalltalk, \u0432\u0435\u0441\u044c \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0438\u0434\u0435\u0435 \u043f\u043e\u0441\u044b\u043b\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439. \u041f\u043e\u0432\u043b\u0438\u044f\u043b \u043d\u0430 Objective-C. <\/p>\n<p>  <\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u043b\u044e\u0431\u0438\u043c \u0438 \u0443\u043c\u0435\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u044b. \u0418 \u0447\u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435: <\/p>\n<p>  <\/p>\n<ul>\n<li>\u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0442\u0438\u043f\u0430 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0438 \u0438\u0437 C++ \u2014 \u043a\u043e\u0434\u0430 \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0432\u0448\u0438\u043c\u0441\u044f;<\/li>\n<li>\u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0432 \u043d\u0430\u0442\u0438\u0432\u043d\u043e\u043c \u043a\u043e\u0434\u0435, \u0442\u043e \u0435\u0441\u0442\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0435 \u0437\u0430\u0433\u043e\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u0440\u0430\u0441\u0447\u0435\u0442\u043e\u0432, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u0442 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432 \u0438 \u043d\u0435\u043a\u043e\u043c\u0443 \u0438\u0445 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c;<\/li>\n<li>\u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0438 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 JVM;<\/li>\n<li>\u0438 \u0447\u0442\u043e\u0431\u044b \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u0430\u0442\u044c, \u0437\u0430\u043e\u0434\u043d\u043e \u0438 \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0430 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<\/li>\n<\/ul>\n<p>  <\/p>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong><\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432 \u043b\u0443\u0447\u0448\u0438\u0445 \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u044f\u0445 DIY, \u0442\u0430\u043a \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u00ab\u043f\u043e\u043c\u0438\u0433\u0430\u0442\u044c \u0441\u0432\u0435\u0442\u043e\u0434\u0438\u043e\u0434\u043e\u043c\u00bb. \u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 JNI, \u0432 \u043c\u0438\u0440\u0435 Android NDK \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 Java \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e, \u0432 \u043b\u044e\u0431\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u042d\u0442\u043e \u043c\u044b \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u00ab\u041d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044f\u00bb. <\/p>\n<p>  <\/p>\n<p>\u0414\u0435\u043c\u043e\u043f\u0440\u043e\u0435\u043a\u0442 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d \u043d\u0430 \u0448\u0430\u0431\u043b\u043e\u043d\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438\u0437 Android Studio. <\/p>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0430\u0432\u0442\u043e\u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434. \u041e\u043d \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">\/\/ Used to load the 'native-lib' library on application startup. static {     System.loadLibrary(\"native-lib\"); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c\u0438. \u041f\u0435\u0440\u0432\u044b\u0439 \u0448\u0430\u0433 \u2014 \u043c\u0435\u0442\u043e\u0434 <code>nsubscribeListener<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">private native void nsubscribeListener(JNIListener JNIListener);<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 C ++-\u043a\u043e\u0434\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 java-\u043a\u043e\u0434 \u0434\u043b\u044f \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043a \u043e\u0431\u044a\u0435\u043a\u0442\u0443, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 <code>JNIListener.<\/code> <\/p>\n<p>  <\/p>\n<pre><code class=\"java\">public interface JNIListener {     void onAcceptMessage(String string);      void onAcceptMessageVal(int messVal); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0443\u044e \u043c\u0430\u0448\u0438\u043d\u0443 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0438 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">Java_ua_zt_mezon_myjnacallbacktest_MainActivity_nsubscribeListener(JNIEnv *env, jobject instance,                                                                    jobject listener) {      env-&gt;GetJavaVM(&amp;jvm); \/\/store jvm reference for later call      store_env = env;      jweak store_Wlistener = env-&gt;NewWeakGlobalRef(listener);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u0440\u0430\u0437\u0443 \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u044b. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442- \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430. <\/p>\n<p>  <\/p>\n<pre><code class=\"java\">jclass clazz = env-&gt;GetObjectClass(store_Wlistener); jmethodID store_method = env-&gt;GetMethodID(clazz, \"onAcceptMessage\", \"(Ljava\/lang\/String;)V\"); jmethodID store_methodVAL = env-&gt;GetMethodID(clazz, \"onAcceptMessageVal\", \"(I)V\");<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430\u043d\u043d\u044b\u0435 \u043e \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043a\u0430\u043a \u0437\u0430\u043f\u0438\u0441\u0438 \u043a\u043b\u0430\u0441\u0441\u0430 <code>ObserverChain<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">class ObserverChain { public:     ObserverChain(jweak pJobject, jmethodID pID, jmethodID pJmethodID);      jweak store_Wlistener=NULL;     jmethodID store_method = NULL;     jmethodID store_methodVAL = NULL;  };<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0430 \u0432 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 store_Wlistener_vector.<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">ObserverChain *tmpt = new ObserverChain(store_Wlistener, store_method, store_methodVAL);  store_Wlistener_vector.push_back(tmpt);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0431\u0443\u0434\u0443\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438\u0437 Java-\u043a\u043e\u0434\u0430.<\/p>\n<p>  <\/p>\n<p>\u0421\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0435 \u0432 \u043c\u0435\u0442\u043e\u0434 nonNext, \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u043e\u0441\u043b\u0430\u043d\u043e \u0432\u0441\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0432\u0448\u0438\u043c\u0441\u044f.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">private native void nonNext(String message);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">Java_ua_zt_mezon_myjnacallbacktest_MainActivity_nonNext(JNIEnv *env, jobject instance,                                                                 jstring message_) {     txtCallback(env, message_); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f txtCallback(env, message_); \u0440\u0430\u0441\u0441\u044b\u043b\u0430\u0435\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0432\u0448\u0438\u043c\u0441\u044f.<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">void txtCallback(JNIEnv *env, const _jstring *message_) {     if (!store_Wlistener_vector.empty()) {         for (int i = 0; i &lt; store_Wlistener_vector.size(); i++) {             env-&gt;CallVoidMethod(store_Wlistener_vector[i]-&gt;store_Wlistener,                                 store_Wlistener_vector[i]-&gt;store_method, message_);         }     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0441\u044b\u043b\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438\u0437 \u0421++ \u0438\u043b\u0438 \u0421 \u043a\u043e\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e test_string_callback_fom_c<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">void test_string_callback_fom_c(char *val)<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u043d\u0430 \u043f\u0440\u044f\u043c\u043e \u0441\u043e \u0441\u0442\u0430\u0440\u0442\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0435\u0441\u0442\u044c \u043b\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0438 \u0432\u043e\u043e\u0431\u0449\u0435.<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">if (store_Wlistener_vector.empty())     return;<\/code><\/pre>\n<p>  <\/p>\n<p>\u041b\u0435\u0433\u043a\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u0447\u0442\u043e \u0434\u043b\u044f \u043f\u043e\u0441\u044b\u043b\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u0441\u0435 \u0442\u0430 \u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f txtCallback:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">void test_string_callback_fom_c(char *val) {     if (store_Wlistener_vector.empty())         return;     __android_log_print(ANDROID_LOG_VERBOSE, \"GetEnv:\", \" start Callback  to JNL [%d]  \\n\", val);     JNIEnv *g_env;     if (NULL == jvm) {         __android_log_print(ANDROID_LOG_ERROR, \"GetEnv:\", \"  No VM  \\n\");         return;     }     \/\/  double check it's all ok     JavaVMAttachArgs args;     args.version = JNI_VERSION_1_6; \/\/ set your JNI version     args.name = NULL; \/\/ you might want to give the java thread a name     args.group = NULL; \/\/ you might want to assign the java thread to a ThreadGroup      int getEnvStat = jvm-&gt;GetEnv((void **) &amp;g_env, JNI_VERSION_1_6);      if (getEnvStat == JNI_EDETACHED) {         __android_log_print(ANDROID_LOG_ERROR, \"GetEnv:\", \" not attached\\n\");         if (jvm-&gt;AttachCurrentThread(&amp;g_env, &amp;args) != 0) {             __android_log_print(ANDROID_LOG_ERROR, \"GetEnv:\", \" Failed to attach\\n\");         }     } else if (getEnvStat == JNI_OK) {         __android_log_print(ANDROID_LOG_VERBOSE, \"GetEnv:\", \" JNI_OK\\n\");     } else if (getEnvStat == JNI_EVERSION) {         __android_log_print(ANDROID_LOG_ERROR, \"GetEnv:\", \" version not supported\\n\");     }      jstring message = g_env-&gt;NewStringUTF(val);\/\/      txtCallback(g_env, message);      if (g_env-&gt;ExceptionCheck()) {         g_env-&gt;ExceptionDescribe();     }      if (getEnvStat == JNI_EDETACHED) {         jvm-&gt;DetachCurrentThread();     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 MainActivity \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0434\u0432\u0430 textview \u0438 \u043e\u0434\u043d\u043e EditView.<\/p>\n<p>  <\/p>\n<pre><code class=\"xml\">&lt;TextView     android:id=\"@+id\/sample_text_from_Presenter\"     android:layout_width=\"wrap_content\"     android:layout_height=\"wrap_content\"     android:layout_weight=\"1\"     android:padding=\"25dp\"     android:text=\"Hello World!from_Presenter\"     app:layout_constraintBottom_toTopOf=\"parent\"     app:layout_constraintEnd_toStartOf=\"parent\"     app:layout_constraintStart_toStartOf=\"parent\"     app:layout_constraintTop_toTopOf=\"parent\" \/&gt;  &lt;TextView     android:id=\"@+id\/sample_text_from_act\"     android:layout_width=\"wrap_content\"     android:layout_height=\"wrap_content\"     android:layout_weight=\"1\"     android:text=\"Hello World from_ac!\"     app:layout_constraintBottom_toTopOf=\"parent\"     app:layout_constraintStart_toStartOf=\"@+id\/sample_text_from_Presenter\"     app:layout_constraintTop_toTopOf=\"parent\" \/&gt; &lt;EditText     android:id=\"@+id\/edit_text\"     android:layout_width=\"wrap_content\"     android:layout_height=\"wrap_content\"      android:text=\"Hello World!\"     app:layout_constraintBottom_toTopOf=\"parent\"     app:layout_constraintStart_toStartOf=\"@+id\/sample_text_from_act\"     app:layout_constraintTop_toTopOf=\"parent\" \/&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 OnCreate \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c View \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c, \u0447\u0442\u043e \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 EditText, \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u0430\u043c.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">mEditText = findViewById(R.id.edit_text); mEditText.addTextChangedListener(new TextWatcher() {     @Override     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {      }      @Override     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {         nonNext(charSequence.toString());     }      @Override     public void afterTextChanged(Editable editable) {      } }); tvPresenter = (TextView) findViewById(R.id.sample_text_from_Presenter);  tvAct = (TextView) findViewById(R.id.sample_text_from_act);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0430\u0432\u043e\u0434\u0438\u043c \u0438 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a\u043e\u0432:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">mPresenter = new MainActivityPresenterImpl(this); nsubscribeListener((MainActivityPresenterImpl) mPresenter);  nlistener = new JNIListener() {     @Override     public void onAcceptMessage(String string) {         printTextfrActObj(string);     }      @Override     public void onAcceptMessageVal(int messVal) {      } }; nsubscribeListener(nlistener);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u044d\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/n2\/yk\/9a\/n2yk9aykepfz92zblbxd_lej4xy.gif\"><\/p>\n<p>  <\/p>\n<p>\u0412 \u043e\u0431\u0449\u0435\u043c \u043f\u043e\u043a\u0430 \u0432\u0441\u0435, sapienti sat.<\/p>\n<\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/post\/420389\/\"> https:\/\/habr.com\/post\/420389\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div data-io-article-url=\"https:\/\/habr.com\/post\/420389\/\" class=\"post__text post__text-html js-mediator-article\">\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432 Android (NDK) JNI callbacks, \u043f\u0430\u0442\u0442\u0435\u0440\u043d &#171;<a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%9D%D0%B0%D0%B1%D0%BB%D1%8E%D0%B4%D0%B0%D1%82%D0%B5%D0%BB%D1%8C_(%D1%88%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD_%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F)\">\u041d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044c<\/a>-\u041f\u043e\u0434\u043f\u0438\u0441\u0447\u0438\u043a&#187; \u0441 NDK \u0438 callback, \u0441\u0430\u043c\u043e\u043f\u0438\u0441\u043d\u044b\u0439 EventBus \u0438\u043b\u0438 Rx <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-288124","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/288124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=288124"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/288124\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=288124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=288124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=288124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}