国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Js調用Java方法并互相傳參的簡單實例

2019-11-20 09:15:45
字體:
來源:轉載
供稿:網友

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 

主站蜘蛛池模板:
岐山县|
闽侯县|
封丘县|
正蓝旗|
中阳县|
永嘉县|
噶尔县|
老河口市|
宁化县|
交口县|
达尔|
徐水县|
长治市|
江永县|
临猗县|
县级市|
舟山市|
赤壁市|
广汉市|
蓬安县|
岳阳市|
罗江县|
云和县|
合作市|
松溪县|
娄烦县|
万盛区|
邯郸市|
甘洛县|
民丰县|
绍兴市|
开江县|
诸暨市|
方城县|
容城县|
修水县|
稻城县|
上饶市|
宁国市|
吉木乃县|
太白县|