{"id":9379,"date":"2013-01-31T12:49:02","date_gmt":"2013-01-31T07:19:02","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=9379"},"modified":"2013-01-31T12:52:54","modified_gmt":"2013-01-31T07:22:54","slug":"using-sendjavscript-to-inject-javascript-into-android-phonegap-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-sendjavscript-to-inject-javascript-into-android-phonegap-application\/","title":{"rendered":"Using &#8216;SendJavscript&#8217; to inject javascript into Android Phonegap application"},"content":{"rendered":"<p>In one of my Android Phonegap projects, I had to pass information from the native java code of my app to the Phonegap code(written in Javascript). I found out that I can do so with \u2018sendJavascript()\u2019 method available in DroidGap class. For example, the following line of code\u00a0will inject a variable in Javscript with value of 10.<br \/>\n[sourcecode]<br \/>\nPhonegapActivity.this.sendJavascript(\u201cjavascript:var injectedVariable=10;\u201d) \/\/PhonegapActivity \u2013 Activity that extends DroidGap class<br \/>\n[\/sourcecode]<\/p>\n<p>Moreover, as various activites in an Android app communicate with each other by using intents, the Phonegap activity can be configured to handle data sent by other activities and passing it to the Javascript by over-riding the \u2018onNewIntent()\u2019 method in Phonegap Activity. For example \u2013<br \/>\n[sourcecode]<br \/>\n@Override<br \/>\npublic void onNewIntent(Intent intent) {<br \/>\nsuper.onNewIntent(intent);<br \/>\nString data=intent.getStringExtra(\u201cdata\u201d);<br \/>\nPhonegapActivity.this.sendJavascript(&quot;javascript:var data=&quot;+data+\u201d;\u201d);<br \/>\nPhonegapActivity.this.sendJavascript(&quot;javascript:doSomethingWithData(data);\u201d);<\/p>\n<p>}<br \/>\n[\/sourcecode]<br \/>\nThus, we can intercept the event to obtain the data, then pass it along to Phonegap and process it according to our requirement in Javascript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of my Android Phonegap projects, I had to pass information from the native java code of my app to the Phonegap code(written in Javascript). I found out that I can do so with \u2018sendJavascript()\u2019 method available in DroidGap class. For example, the following line of code\u00a0will inject a variable in Javscript with value [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[518],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9379"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=9379"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/9379\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=9379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=9379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=9379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}