2

I want to pass the 'this' parameter from javascript to the native layer in android.

function abc() {

    function callf() {
        // 'this' should refer to the instance of the 
        // abc object
        Native.call(this);
    }
 }

This is to make it possible to call a function on the 'abc' instance from the native layer.

When I do pass an object, either using 'this' or just an object directly, it gives 'null'.

Does anyone know how to solve this?

Thanks,
Rajath

4

4 回答 4

2

当我需要做这样的事情时,我会使用这样的基板层:

javascript

(function() {

  var callbacks = {};

  function getNewId() {
    return Math.round(Math.random() * 999999999);
  }

  window.__withCallback = function(func, context, callback, params) {

    var cbId = getNewId(),
        args = Array.prototype.slice.call(arguments, 2);

    while (cbId in callbacks) {
      cbId = getNewId();
    }

    args.unshift(cbId);
    callbacks[cbId] = { context: context, callback: callback };

    func.apply(Native, args);

  };

  window.__callbackFromNative = function(id, params) {

    var args,
        cbItem = callbacks[id];    

    if (cbItem && typeof(cbItem.callback) === 'function') {
      args = Array.prototype.slice.call(arguments, 1);
      cbItem.callback.apply(cbItem.context, args);
    }

    delete callbacks[id];
  };


}());

所以现在当你有 Java 代码时(假设你暴露了与“Native”相同的对象):

爪哇

class Native {
    public void test (long callbackId, String param1, String param2) {

        // do java stuff in here
        // webView is your webView
        webView.loadUrl("javascript:__callbackFromNative(" + callbackId + ", 'yay', 69)");

    }
}

并像这样使用它:

javascript

var awesome = {

  test2: function(arg1, arg2) {
    // callback
  },

  startTest: function() {
    // here's the way you pass "this" to "Native.test", 
    // this.test2 is the function you want to "callback" to
    // the rest are all params 
    __withCallback(Native.test, this, this.test2, "xxx", 'yay');
  }
};


awesome.startTest();

底层现在是可重用和标准的,所以你不必担心设置全局变量或任何类似的疯狂,而且它可以在本地层内外使用任意数量的参数......

希望这会有所帮助-ck

于 2012-10-18T22:04:03.723 回答
1

请注意,您的示例正在传递thisof callf。您必须将abc's传递thiscallf

function abc() {
    function callf() {
        Native.call(this);
    }
    // ...
    callf.call(this);
    // ...
}

关闭

function abc() {
    var that = this;
    function callf() {
        Native.call(that);
    }
    // ...
    callf();
    // ...
}

编辑:

您仍然受限于可以传递给 Java 的值类型,请参阅这个问题

于 2012-10-18T11:44:50.603 回答
1

假设 Native 被映射为具有方法“调用”的命名 java 对象,您使用 addJavascriptInterface() 绑定到 Web 视图(请参阅相关的https://stackoverflow.com/a/12832132/1367983),我认为您应该跳过尝试调用 webview 之外的 javascript 绑定对象上的任何操作,并使用动态生成的 javascript url 生成要在其上运行的逻辑,然后可以使用 webview 的 loadUrl() 方法执行。

in js of html content rendered by webview:

var myVar;

...

myVar = this;
Native.call('myVar');
...

in javascript interface class Native's implementation of call()

webView.loadUrl("javascript:myVar.doSomething('" + stringFromApp1 + "', " + numberFromApp1 + ");");
于 2012-10-12T06:41:08.733 回答
0

这是一个长镜头,但您可以将“this”对象字符串化并将其用作 Java 中的 JSON 对象

在 JavaScript 中

function abc() {

  function callf() {
    // 'this' should refer to the instance of the 
    // abc object
    Native.call(JSON.stringify(this));
  }
}

在爪哇

    class Native {
    public void call (String object)
    {
        try {
            JSONObject thisObject = new JSONObject(object);
            // do whatever you want to the thisObject
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

我不记得当您收到对象时是否会对其进行编码。如果是,您可以使用:

URLDecoder.decode(object, "UTF-8")
于 2012-10-18T14:23:53.773 回答