Phonegap – call javascript method from native code

Default Phonegap way of interacting with the Native code is to initiate a plugin request from Javascript, process in the Native code and reply the result with data which can be used in Javascript again. However many times it may require to update Javascript values or HTML DOM elements without initiating any plugin call from Javascript. Below code snippet uses a Plugin method as a gateway to call a Javascript method from Native Android code.

Example with full source code for Android is available here

  • Anas Azeem

    I was searching for this solution for a long time. So much so, that its been a long time I posted my question on StackOverflow.com [http://stackoverflow.com/questions/19177991/asynchronous-communication-between-javascript-and-phonegap-plugin] and havn’t received response that suits me.

    Now I have issues with your solutions too.
    1) I’m not able to call the javascript method (but I’ve just found this solution and hopefully will be able to run it)
    2) My concern is that, why there are two (exactly same) definitions of the “sendValues” methods. There is one the “App.java” and other “AppComm.java”. So, why they are there?
    3) And finally, how come the “sendJavacript” method is called in the plugin class with “this” instance. As far as I know sendJavascript is the method of the DroidGap class.

    Anticipating a quick response. Thank You

    • Anas Azeem

      4) “[DEPRECATION NOTICE] window.addPlugin and window.plugins will be removed in version 2.0.” I’m using phonegap 3.0