如何沟通与JavaScript code,反之亦然在PhoneGap的机器人的机器人活动? [英] how to communicate an android activity with javascript code and vise versa in phonegap android?

查看:124
本文介绍了如何沟通与JavaScript code,反之亦然在PhoneGap的机器人的机器人活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的PhoneGap开发。我想的要懂得我们的PhoneGap的网页传送到Android原生的活动,反之亦然,并给我的PhoneGap learning.Please任何教程任何一个可以帮助我。

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.Please can any one help me.

感谢在前进。

推荐答案

您需要使用cordova.exec API中的JavaScript能够code和Android的活动之间进行通信。也许这个链接可以帮助你。

you need to use cordova.exec API in able to communicate between javascript code and android activity. Maybe this link can help you.

<一个href=\"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\" rel=\"nofollow\">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

您需要声明您的自定义插件在config.xml文件中的第一件事

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

<feature name="CustomPlugin">
      <param name="android-package" value="com.AndroidApacheCordovaPlugin.CustomPlugin" />
</feature>

通过使用Java code实现插件

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;
    }
}

从JavaScript调用插件

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);
}

这篇关于如何沟通与JavaScript code,反之亦然在PhoneGap的机器人的机器人活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆