How To Communicate An Android Activity With Javascript Code And Vise Versa In Phonegap Android?
I am new to phonegap development. I want's to know how to communicate our phonegap webpage to native android activity and vise versa and give me any tutorial for phonegap learning.
Solution 1:
you need to use cordova.exec API in able to communicate between javascript code and android activity. Maybe this link can help you.
First thing you need to declare your custom plugin in config.xml
<feature name="CustomPlugin">
<param name="android-package" value="com.AndroidApacheCordovaPlugin.CustomPlugin" />
</feature>
Implementing the plug-in by using Java code
public class CustomPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
throws JSONException {
if (action.equals("sayHello")){
try {
String responseText = "Hello world, " + args.getString(0);
callbackContext.success(responseText);
} catch (JSONException e){
callbackContext.error("Failed to parse parameters");
}
return true;
}
return false;
}
}
Calling a plug-in from JavaScript
function initial(){
var name = $("#NameInput").val();
cordova.exec(sayHelloSuccess, sayHelloFailure, "CustomPlugin", "sayHello", [name]);
}
function sayHelloSuccess(data){
alert("OK: " + data);
}
function sayHelloFailure(data){
alert("FAIL: " + data);
}
Post a Comment for "How To Communicate An Android Activity With Javascript Code And Vise Versa In Phonegap Android?"