Skip to content Skip to sidebar Skip to footer

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.

http://public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v610/06_05_Android_-_Adding_native_functionality_to_hybrid_application_with_Apache_Cordova_plugin.pdf

First thing you need to declare your custom plugin in config.xml

<featurename="CustomPlugin"><paramname="android-package"value="com.AndroidApacheCordovaPlugin.CustomPlugin" /></feature>

Implementing the plug-in by using Java code

publicclassCustomPluginextendsCordovaPlugin {

    @Overridepublicbooleanexecute(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");
            }
            returntrue;
        }

        returnfalse;
    }
}

Calling a plug-in from JavaScript

functioninitial(){
    var name = $("#NameInput").val();
    cordova.exec(sayHelloSuccess, sayHelloFailure, "CustomPlugin", "sayHello", [name]);
}

functionsayHelloSuccess(data){
    alert("OK: " + data);
}

functionsayHelloFailure(data){
    alert("FAIL: " + data);
}

Post a Comment for "How To Communicate An Android Activity With Javascript Code And Vise Versa In Phonegap Android?"