Created
June 13, 2012 04:59
-
-
Save dhavaln/2921942 to your computer and use it in GitHub Desktop.
Android to Javascript Call in Phonegap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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