Js調(diào)用Java方法并互相傳參的簡單實例
Js通過PhoneGap調(diào)用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數(shù)據(jù) if (action.equals("hello")) { String str1= args.getString(0); //獲取第一個參數(shù) String str2= args.getString(1); //獲取第二個參數(shù) jsonObj.put("str1", str1+"1"); //把參數(shù)放到JSONObject對象中 jsonObj.put("str2", str2+"2"); //把參數(shù)放到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的參數(shù) SimplePlugin.prototype.hello = function(successCallback, failureCallback, str1, str2) { // exec 內(nèi)的參數(shù)分別是: Success Callback, Failure Callback, Registered Plugin name:就是在XML文件配置的那個所對應(yīng)的name, // 'hello'是傳入Java文件的execute方法中的參數(shù)String action // name (從 HTML 傳進來的參數(shù)) 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文件中調(diào)用方法
在html文件中引入phonegap和插件的js文件,調(diào)用方法
<!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("返回的第一個參數(shù):"+result.str1+"返回的第二個參數(shù)"+result.str2); }, function(error) { }, "第一個參數(shù)", "第二個參數(shù)" ); }); }); </script> </head> <body> <button type="button" id="btn_test">Click Me!</button> </body> </html>
以上這篇Js調(diào)用Java方法并互相傳參的簡單實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中判斷數(shù)據(jù)類型的方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了一些JavaScript中判斷數(shù)據(jù)類型的方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,需要的小伙伴可以了解一下2023-07-07前端實現(xiàn)文件的斷點續(xù)傳(前端文件提交+后端PHP文件接收)
本文通過斷點續(xù)傳的簡單例子(前端文件提交+后端PHP文件接收),本文以圖片為實例給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,感興趣的朋友一起看看吧2016-11-11驗證控件與Button的OnClientClick事件詳細(xì)解析
以下就是被我已知忽略的問題和解決方案,當(dāng)我發(fā)覺這個問題的時候,冒出了一身冷汗,幸虧做了嚴(yán)格的服務(wù)器端驗證,不然可就慘了2013-12-12基于JS實現(xiàn)Android,iOS一個手勢動畫效果
這篇文章主要介紹了基于JS實現(xiàn)Android,iOS一個手勢動畫效果 的相關(guān)資料,需要的朋友可以參考下2016-04-04