发布于 2017-03-11 08:43:26 | 241 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Javascript教程,程序狗速度看过来!

JavaScript客户端脚本语言

Javascript 是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如Perl,遗留的速度问题,为客户提供更流畅的浏览效果。


下面小编就为大家带来一篇Js调用Java方法并互相传参的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Js通过PhoneGap调用Java方法并互相传参的。

一、JAVA代码

写一个类,该类继承自Plugin并重写execute方法。


import org.json.JSONArray;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.phonegap.api.PhonegapActivity;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;

public class PluginTest extends Plugin {

   public static String ACTION = "hello";

  public PluginTest() {
  }

  /**
   * Executes the request and returns PluginResult.
   *
   * @param action     The action to execute.
   * @param args       JSONArray 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.
   */
  @Override
  public PluginResult execute(String action, JSONArray args, String callbackId) {
    try {
      JSONObject jsonObj = new JSONObject();//可以返回给JS的JSON数据
      if (action.equals("hello")) {
        String str1= args.getString(0); //获取第一个参数
        String str2= args.getString(1); //获取第二个参数
        jsonObj.put("str1", str1+"1"); //把参数放到JSONObject对象中
        jsonObj.put("str2", str2+"2");  //把参数放到JSONObject对象中
      }
      PluginResult r = new PluginResult(PluginResult.Status.OK,jsonObj);
      return r;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

三、Javascript文件中注册插件

新建一个.js文件,并把该文件和phonegap文件放在同一目录。(新建一个simplePlugin.js文件)


var SimplePlugin = function() {};

//str1和str2是传到JAVA的参数
SimplePlugin.prototype.hello = function(successCallback, failureCallback, str1, str2) {
  // exec 內的參數分別是: Success Callback, Failure Callback, Registered Plugin name:就是在XML文件配置的那个所对应的name,
  // 'hello'是传入Java文件的execute方法中的参数String action 
  // name (從 HTML 傳進來的參數)
  return PhoneGap.exec(successCallback, failureCallback, 'PluginTest', 'hello', [str1,str2]);
};

// 这里是 PhoneGap Plugin 的註冊,Plugin 的名稱還有 Native Class 的名稱別打錯了,就是我們剛剛輸入的那些
PhoneGap.addConstructor(function() {
  // Register the javascript plugin with PhoneGap
  PhoneGap.addPlugin('simpleplugin', new SimplePlugin());  //simpleplugin是插件名称, new SimplePlugin()实例化的是本Javascript的类名 

});

四、在HTML文件中调用方法

在html文件中引入phonegap和插件的js文件,调用方法


<!DOCTYPE html>
<html>
  <head>
  <meta charset="utf-8">
  <title>JAVA传参</title>
  <script src="phonegap.js"></script> <!--phonegap包-->
  <script src="js/jquery.js"></script>
  <script src="simplePlugin.js"></script><!--自定义的插件文件-->
  <script>  
  $(document).ready(function(e) {
    $("#btn_test").click(function(){
      window.plugins.simplePlugin.hello( 
        function(result) {
          alert("返回的第一个参数:"+result.str1+"返回的第二个参数"+result.str2);
        }, 
        function(error) {
        },
        "第一个参数",
        "第二个参数"
      );  
    });
  });
  </script>
  </head>
<body>
<button type="button" id="btn_test">Click Me!</button>
</body>
</html>

以上这篇Js调用Java方法并互相传参的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持phperz。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务