Android PhoneGap 1.7 调用 javascript 函数

Android PhoneGap 1.7 calling a javascript function

本文关键字:javascript 函数 调用 PhoneGap Android      更新时间:2023-09-26

好的,所以我需要提供一些背景知识。首先,我将jquery-mobile与PhoneGap 1.7一起使用。我已经编写了一个非常简单的Java服务器,它使用ServerSocket对象。在Android手机上,我连接到服务器,这样做后,服务器通过套接字发送数据。这部分正在工作。

我坚持的部分是我打算通过该套接字发送数据,这将需要在收到数据时更新jquery移动UI。


答:西蒙是一个巨大的帮助,我在他的帮助下并按照本教程找到了答案

真正打动我的部分是在PhoneGap插件本身中生成线程。一旦我意识到这一点,一切都到位了。但是,对于任何对此感兴趣的人,都是代码。请记住,我从教程中学到了很多东西。我还包含了我创建的非常简单的 Java 服务器来测试这些概念。我想也许这会在未来帮助某人。请记住,这基本上是一个概念证明。

我需要修改这个插件以满足我的实际需求:

安卓活动: import org.apache.cordova.DroidGap;

import android.os.Bundle;
public class ISSAndroidActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }
}

PhoneGap插件:

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import org.apache.cordova.api.*;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class InvokeJavaScriptPlugin extends Plugin {
    public static String PLUGIN_TAG = "InvokeJavaScriptPlugin";
    public static String PROCESS_DATA_ACTION = "processData";
    private String callBackId = "";
    @Override
    public PluginResult execute(String action, JSONArray args, String callbackId) {
        PluginResult pluginResult = null;
        Log.d(PLUGIN_TAG, "Invoking Javascript w'' NO_RESULT");
        if (action.equals(PROCESS_DATA_ACTION)) {
            this.callBackId = callbackId;
            startProcessingData();
            pluginResult = new PluginResult(Status.NO_RESULT);
            pluginResult.setKeepCallback(true);
        } else {
            pluginResult = new PluginResult(Status.INVALID_ACTION);
            Log.e(PLUGIN_TAG, "Invalid action : " + action);
        }
        return pluginResult;
    }
    /**
     * Spawns a thread that connects to a server, and receives data from it
     */
    private void startProcessingData() {
        new Thread() {
            @Override
            public void run() {
                // Socket Testing
                ObjectOutputStream out;
                ObjectInputStream in;
                Socket requestSocket = new Socket();
                Object inboundObject;
                SocketAddress ipAddr = new InetSocketAddress("192.168.1.2",
                        2012);
                try {
                    requestSocket.connect(ipAddr);
                    out = new ObjectOutputStream(
                            requestSocket.getOutputStream());
                    out.flush();
                    in = new ObjectInputStream(requestSocket.getInputStream());
                    do {
                        inboundObject = in.readObject(); // Data is received
                                                         // here
                        int processedData = ((Number) inboundObject).intValue();
                        onProcessDataReadSuccess(processedData);
                    } while (requestSocket.isConnected());
                } catch (SocketException ex) {
                    Log.d(PLUGIN_TAG, "Connection to Server lost");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }
    /**
     * Callback method for startProcessingData(). Sends the result up to
     * javascript layer via Plugin.success()<br>
     * This method is automatically called asynchronously when processing is
     * finished.
     * 
     * @param processedData
     *            the result of data processing which will be passed back to
     *            javascript.
     */
    private void onProcessDataReadSuccess(int processedData) {
        Log.d(PLUGIN_TAG, "Processing data: " + processedData);
        PluginResult result;
        try {
            JSONObject resultJSON = new JSONObject();
            resultJSON.put("processedData", processedData);
            result = new PluginResult(Status.OK, resultJSON);
        } catch (JSONException jsonEx) {
            Log.e(PLUGIN_TAG, "Got JSON Exception " + jsonEx.getMessage());
            jsonEx.printStackTrace();
            result = new PluginResult(Status.JSON_EXCEPTION);
        }
        result.setKeepCallback(true);
        this.success(result, this.callBackId);
    }
}

索引.html:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link type="text/css" href="css/jquery.mobile-1.1.0.min.css"
    rel="stylesheet" />
</head>
<script type="text/javascript" charset="utf-8"
    src="scripts/cordova-1.7.0.js"></script>
<script type="text/javascript" src="scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.mobile-1.1.0.min.js"></script>
<script type="text/javascript" src="scripts/InvokeJavaScript.js"></script>
<script type="text/javascript" charset="utf-8">
    document.addEventListener('deviceready', function() {
        window.plugins.InvokeJavaScript.processData(function(result) {
            displayProcessedData(result)
        }, function(error) {
            console.log(error)
        });
    }, true);
    function displayProcessedData(result) {
        document.getElementById("processedData").innerHTML = result.processedData;
    }
</script>
<body>
    <div data-role="page">
        <div data-role="header" data-position="fixed">
            <h1>Demo</h1>
        </div>
        <div data-role="content">
            Result:
            <div id="processedData"></div>
        </div>
        <div data-role="footer" data-position="fixed">
            <div data-role="navbar"></div>
        </div>
    </div>
</body>
</html>

服务器.java

import java.io.BufferedReader;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ISSServer {
    private static Socket androidSocket;
    private static ServerSocket serverSocket;
    static ObjectOutputStream out;
    static BufferedReader in;
    public static void main(String[] args) {
        System.out.println("Listen Thread: Waiting for connection");
        int port_number = 2012; // The default port
        try {
            serverSocket = new ServerSocket(port_number);
            androidSocket = serverSocket.accept();
            System.out.println("Connection established");
            out = new ObjectOutputStream(androidSocket.getOutputStream());
            out.flush();
            // in = new BufferedReader(new
            // InputStreamReader(androidSocket.getInputStream()));
            out.writeObject(1337);
            out.flush();
            out.reset();
            System.out.println("1337 sent");
            Thread.sleep(2000);
            out.writeObject(9999);
            out.flush();
            out.reset();
            System.out.println("9999 sent");
            Thread.sleep(2000);
            out.writeObject(1234);
            out.flush();
            out.reset();
            System.out.println("1234 sent");
            Thread.sleep(2000);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

你这样做是为了对自己苛刻。如果您使用 PhoneGap 插件,您将能够保存初始 JavaScript 调用的回调 ID,然后发回一个 PluginResult,您将其称为 result.setKeepCallback(true),状态PluginResult.Status.NO_RESULT。

然后,每当您在Java端获取并更新时,您都会创建一个新的PluginResult。将状态设置为"确定",将数据设置为要发送的任何内容,当然在返回之前,请设置KeepCallback(true)。然后调用this.success(callbackId,result),你的JS端将获得持续更新。