Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Created June 13, 2012 04:59
Show Gist options
  • Save dhavaln/2921942 to your computer and use it in GitHub Desktop.
Save dhavaln/2921942 to your computer and use it in GitHub Desktop.
Android to Javascript Call in Phonegap
public class App extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
Thread t = new Thread() {
public void run() {
try {
for (int i = 0; i < 10; i++) {
sleep(5000);
sendValue("value " + i, "another vlaue " + i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
}
public void sendValue(String value1, String value2) {
JSONObject data = new JSONObject();
try {
data.put("value1", value1);
data.put("value2", value2);
} catch (JSONException e) {
Log.e("CommTest", e.getMessage());
}
String js = String.format("window.plugins.appcomm.updateValues('%s');", data.toString());
this.sendJavascript(js);
}
}
var AppComm = function(){};
AppComm.prototype.updateValues = function(json) {
var data = window.JSON.parse(json);
$("#pluginResult").html(data.value1 + ", " + data.value2);
};
cordova.addConstructor(function() {
cordova.addPlugin("appcomm", new AppComm());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment