Understanding Android's webview addjavascriptinterface

后端 未结 3 1288
感动是毒
感动是毒 2020-12-08 15:38

I know that to interact from Javascript to Java you have to inject a Java object using the addjavascriptInterface method in webview.

Here is the problem I am facing.

相关标签:
3条回答
  • 2020-12-08 16:00

    Ensure your Javascript objects declared in your HTML / Javascript that you need to access from Java are declared global otherwise they will most likely be collected. I have code that does this (where Android is my interface added with addJavascriptInterface):

    <script>
      var cb = function(location) {
         alert('location is ' + location);
      }
      Android.getLocation('cb');
    </script>
    

    The getLocation method invokes Android's LocationManager.requestSingleUpdate which then invokes the callback when the LocationListener fires.

    Without the "var" I find that by the time the location lookup invokes the callback the callback function has been garbage collected.

    0 讨论(0)
  • 2020-12-08 16:00

    (copied from my response on a similar question)

    I've taken Jason Shah's and Mr S's implementation as the building block for my fix and improved upon it greatly.

    There's just far too much code to put into this comment I'll just link to it.

    • Details: http://twigstechtips.blogspot.com/2013/09/android-webviewaddjavascriptinterface.html
    • Source: https://github.com/twig/twigstechtips-snippets/blob/master/GingerbreadJSFixExample.java

    Key points are:

    • Applies to all versions of Gingerbread (2.3.x)
    • Calls from JS to Android are now synchronous
    • No longer have to map out interface methods manually
    • Fixed possibility of string separators breaking code
    • Much easier to change JS signature and interface names
    0 讨论(0)
  • 2020-12-08 16:01

    I want to know that if there is some time before i can access my object

    Yes, I think there is a delay, because WebView.addJavascriptInterface will run in the WebView's internal worker thread. Perhaps you've thought about this, and realized that WebView has to maintain at least one worker thread to do asynchronous network IO. Maybe you also noticed these threads in DDMS while using a WebView.

    It turns out that it also uses a thread to do work for a number of other public methods. I really wish the docs from Google made this clearer! But I hope I can help and show you how I tried to confirm this for myself.

    Follow me as I take a look at the source for WebView. It's reasonably readable, even if you can't follow exactly what's going on, it's possible to trace through answer some questions with respect to threads.

    You can download the Android framework source through the SDK manager tool, but it's also mirrored on Github, so that's what I've linked to here. I guessed and picked a tag that's close to some version of ICS. It's not hard to find WebView.addJavascriptInterface. I just Googled "WebView.java site:github.com/android".

    The method WebView.addJavascriptInterface sends a message to an instance of WebViewCore:

    mWebViewCore.sendMessage(EventHub.ADD_JS_INTERFACE, arg);
    

    In WebViewCore.java there are a bunch of overloaded methods called sendMessage, but we don't really need to know which exactly is being called, since they do pretty much the same thing. There's even a nice comment to give us a hint that we're in the right place! All of them are delegating to an instance of EventHub which is some inner class. This method turns out to be synchronized, and is sending a message to an instance of Handler, which is a good indication that this is probably running in another thread, but for completeness sake, let's find out!

    That Handler is instantiated in EventHub.transferMessages which is called from WebViewCore.initialize. There are a few more hops here, but eventually I found out that this is called from run in WebCoreThread (subclass of Runnable), which is instantiated along with a new Thread right here.

    What an adventure! So, even though I really can't say for sure what's going on with all these moving parts, I am pretty confident to say that this method is not synchronous, and sends a message to the WebView's worker thread. I hope that makes sense!

    if so, how do i get to know how much time should i wait to call my object?

    Unfortunately, I don't know the answer to this. I was researching this exact issue and found this question on StackOverflow in the course of my Googling. I think you have the following options, some of which are nicer or easier than others:

    1) Just Thread.sleep for 100 ms or something between addJavascriptInterface and loadUrl("javascript:..."). Blech, I don't like this, but it is potentially the easiest.

    2) Another possibility is that you could call WebView.loadUrl with a snippet of JavaScript that specifically tests if the interface is set, and catches the ReferenceError that is thrown if it's not set yet. However, as you might have guessed, this kind of involves adding a JavaScript interface to the WebView!

    3) Call WebView.setWebChromeClient instead, and catch JavaScript's alert() or console.log instead. From my experiments, this method is synchronous, so there is no delay. (I have confirmed this in source, but I'll leave details as an exercise for the reader) You should probably come up with some special string to call alert with and check for it inside onJsAlert, so you aren't just catching all alert()s.

    Sorry for the length of this answer, I hope that helps. Good luck!

    0 讨论(0)
提交回复
热议问题