Android Java和Phonegap Javascript之间的通信?

时间:2010-04-28 08:15:51

标签: android cordova

我相信可以从(PhoneGap)Javascript调用Java方法。

任何人都知道怎么做? (我知道如何通过更改PhoneGap的源代码来实现,但我会避免这种情况)

6 个答案:

答案 0 :(得分:125)

我终于成功了。

  • 使用您想要使用的方法创建一个类:

    public class MyClass {
      private WebView mAppView;
      private DroidGap mGap;
    
      public MyClass(DroidGap gap, WebView view)
      {
        mAppView = view;
        mGap = gap;
      }
    
      public String getTelephoneNumber(){
        TelephonyManager tm = 
          (TelephonyManager) mGap.getSystemService(Context.TELEPHONY_SERVICE);
        String number = tm.getLine1Number();
        return number;
      }
    }
    
  • 在您的主要活动中为此类添加Javascript界面​​:

    public class Main extends DroidGap
    {
        private MyClass mc;
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            super.init();
    
            mc = new MyClass(this, appView);
            appView.addJavascriptInterface(mc, "MyCls");
    
            super.loadUrl(getString(R.string.url));
        }
    }
    
  • 在Javascript调用窗口中.MyCls方法:

    <script>
      $(function(){
        $("#phone").text("My telephone number is: " + 
                window.MyCls.getTelephoneNumber());
      });
    </script>
    

注意:

正如评论中所述,对于Android版本4.2及更高版本,请将@JavascriptInterface添加到您要从HTML页面访问的方法中。 Reference

答案 1 :(得分:14)

没有Gap addJavaScriptInterface(mc, "MyCls")

init()可能会导致应用粉碎,您最好在super.init()之前添加addJavascriptInterface()

public class Main extends DroidGap
{
   private MyClass mc;

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);

       super.init();

       mc = new MyClass(this, appView);
       appView.addJavascriptInterface(mc, "MyCls");

       super.loadUrl(getString(R.string.url));
   }
}

答案 2 :(得分:9)

PhoneGap有一个不错的插件API。您可以通过实现IPlugin接口在Java中编写插件。大多数魔法都在execute()函数中。

public interface IPlugin {

    /**
     * Executes the request and returns PluginResult.
     *
     * @param action        The action to execute.
     * @param args          JSONArry of arguments for the plugin.
     * @param callbackId    The callback id used when calling back into JavaScript.
     * @return              A PluginResult object with a status and message.
     */
    PluginResult execute(String action, JSONArray args, String callbackId);

        // ... more ...
}

开始编写插件的最佳方法是首先编写javascript API。你通常会先编写一个自定义的javascript类,然后在javascript类的每个方法中,编组变量并调用你使用Phonegap.exec()方法开发的插件。以下是方法签名供您参考。

/* src/com/phonegap/api/PluginManager.java */
/**
 * Receives a request for execution and fulfills it by finding the appropriate
 * Java class and calling it's execute method.
 *
 * PluginManager.exec can be used either synchronously or async. In either case, a JSON encoded
 * string is returned that will indicate if any errors have occurred when trying to find
 * or execute the class denoted by the clazz argument.
 *
 * @param service       String containing the service to run
 * @param action        String containt the action that the class is supposed to perform. This is
 *                      passed to the plugin execute method and it is up to the plugin developer
 *                      how to deal with it.
 * @param callbackId    String containing the id of the callback that is execute in JavaScript if
 *                      this is an async plugin call.
 * @param args          An Array literal string containing any arguments needed in the
 *                      plugin execute method.
 * @param async         Boolean indicating whether the calling JavaScript code is expecting an
 *                      immediate return value. If true, either PhoneGap.callbackSuccess(...) or
 *                      PhoneGap.callbackError(...) is called once the plugin code has executed.
 *
 * @return              JSON encoded string with a response message and status.
 */
@SuppressWarnings("unchecked")
public String exec(final String service, final String action,
    final String callbackId, final String jsonArgs,
    final boolean async)

您还需要注册插件。您可以通过在自定义JavaScript库底部添加注册码来执行此操作。

在下面的示例中,作者定义了一个javascript BarcodeScanner类,并使用addConstructor方法对其进行注册。

在addConstructor中执行了两个步骤:

  1. 在javascript中创建一个新的BarcodeScanner实例并注册它。 这可以通过javascript访问window.plugins.barcodeScanner

  2. 使用服务名称注册自定义插件类。这个服务名称 作为PhoneGap.exec的第一个参数传入,以便PhoneGap 可以实例化java插件类并在其上调用execute()方法。

  3. 样本注册码:

    PhoneGap.addConstructor(function() {
        /* The following registers an instance of BarcodeScanner in window.plugins.barcodeScanner */
        PhoneGap.addPlugin('barcodeScanner', new BarcodeScanner());
    
        /* The following associates a service name BarcodeScanner with a class com.beetight.barcodescanner.BarcodeScanner */
        /* The service name is the first argument passed into PhoneGap.exec */
        PluginManager.addService("BarcodeScanner","com.beetight.barcodescanner.BarcodeScanner");
    });
    

答案 3 :(得分:6)

更简单的形式:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init(); 
    super.appView.getSettings().setJavaScriptEnabled(true);
    super.appView.addJavascriptInterface(this, "MyCls");
    super.loadUrl("file:///android_asset/www/login.html");
}

答案 4 :(得分:5)

如果有人使用上面的代码获取nullPointer异常,先执行super.oncreate(),然后执行super..init()

super.onCreate(savedInstanceState);
super.init();

我在这里找到了这个解决方案:Phonegap Google Group

非常感谢@ zorglub76的解决方案......

答案 5 :(得分:0)

通过覆盖Android本机代码中的JavaScript提示函数来实现从JavaScript到本机的通信,并且传递的消息非常类似于iOS中使用的消息。我们曾经使用WebView.addJavascriptInterface将Java对象直接添加到JavaScript沙箱,但这导致某些设备在Android 2.3中崩溃。要从本机调用JavaScript,我们当前使用WebView.loadUrl(“javascript:...”),但这有一些问题,所以我们很快就会转移到通过长期XHR连接调用本地HTTP服务器的Java消息队列。

Description by here