Js调用Java方法并互相传参

来源:互联网 发布:网站模板源码免费下载 编辑:程序博客网 时间:2024/05/20 22:39

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(); } }}
复制代码

 

二、在plugins.xml中配置插件

在plugins.xml文件中添加对新插件的配置信息

复制代码
<?xml version="1.0" encoding="utf-8"?><plugins>    <plugin name="App" value="com.phonegap.App"/>    <plugin name="Geolocation" value="com.phonegap.GeoBroker"/>    <plugin name="Device" value="com.phonegap.Device"/>    <plugin name="Accelerometer" value="com.phonegap.AccelListener"/>    <plugin name="Compass" value="com.phonegap.CompassListener"/>    <plugin name="Media" value="com.phonegap.AudioHandler"/>    <plugin name="Camera" value="com.phonegap.CameraLauncher"/>    <plugin name="Contacts" value="com.phonegap.ContactManager"/>    <plugin name="Crypto" value="com.phonegap.CryptoHandler"/>    <plugin name="File" value="com.phonegap.FileUtils"/>    <plugin name="Network Status" value="com.phonegap.NetworkManager"/>    <plugin name="Notification" value="com.phonegap.Notification"/>    <plugin name="Storage" value="com.phonegap.Storage"/>    <plugin name="Temperature" value="com.phonegap.TempListener"/>    <plugin name="FileTransfer" value="com.phonegap.FileTransfer"/>    <plugin name="Capture" value="com.phonegap.Capture"/>    <!-- 新增的插件类配置 name 写你的类名,value写 包名.类名-->    <plugin name="PluginTest" value="com.easyway.barcode.PluginTest "/>  </plugins>
复制代码

 

三、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>
复制代码

 原文:http://www.cnblogs.com/liluping860122/p/3937724.html

0 0
原创粉丝点击