Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android同步Javascript与棒棒糖上的WebView_Javascript_Android_Android Webview - Fatal编程技术网

Android同步Javascript与棒棒糖上的WebView

Android同步Javascript与棒棒糖上的WebView,javascript,android,android-webview,Javascript,Android,Android Webview,我们借用了一项技术,成功地在应用程序中实现了许多功能,使我们的Android应用程序能够从网络视图同步获取数据 下面是gutterbling的示例: import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import android.content.Context; import android.util.Log; import android.webkit.WebView; /*

我们借用了一项技术,成功地在应用程序中实现了许多功能,使我们的Android应用程序能够从网络视图同步获取数据

下面是gutterbling的示例:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.util.Log;
import android.webkit.WebView;


/**
 * Provides an interface for getting synchronous javascript calls
 * @author btate
 *
 */
public class SynchronousJavascriptInterface {

/** The TAG for logging. */
private static final String TAG = "SynchronousJavascriptInterface";

/** The javascript interface name for adding to web view. */
private final String interfaceName = "SynchJS";

/** Countdown latch used to wait for result. */
private CountDownLatch latch = null;

/** Return value to wait for. */
private String returnValue;

/**
 * Base Constructor.
 */
public SynchronousJavascriptInterface() {

}


/**
 * Evaluates the expression and returns the value.
 * @param webView
 * @param expression
 * @return
 */
public String getJSValue(WebView webView, String expression)
{
    latch = new CountDownLatch(1); 
    String code = "javascript:window." + interfaceName + ".setValue((function(){try{return " + expression
        + "+\"\";}catch(js_eval_err){return '';}})());";    
    webView.loadUrl(code);

    try {   
                    // Set a 1 second timeout in case there's an error
        latch.await(1, TimeUnit.SECONDS);
        return returnValue;
    } catch (InterruptedException e) {
        Log.e(TAG, "Interrupted", e);
    }
    return null;

}


/**
 * Receives the value from the javascript.
 * @param value
 */
public void setValue(String value)
{
    returnValue = value;
    try { latch.countDown(); } catch (Exception e) {} 
}

/**
 * Gets the interface name
 * @return
 */
public String getInterfaceName(){
    return this.interfaceName;
    }
}
此JS接口的使用方式如下:

WebView webView = new WebView(context);
SynchronousJavascriptInterface jsInterface = new jsInterface();
webView.addJavascriptInterface(jsInterface, jsInterface.getInterfaceName());

String jsResult = jsInterface.getJSValue(webView, "2 + 5");
尽管在安卓4.0-4.4.4中工作得很好,但在安卓5.0(棒棒糖)中却不起作用

在棒棒糖中,JS似乎是在闩锁倒计时完成后执行的,而之前它会在倒计时完成之前返回一个值

Webview中JS执行的线程是否发生了变化?有没有什么方法可以解决这个问题,而不需要重新编写依赖于能够同步调用JS的大块应用程序

 loadUrl("javascript:" + code)
不要使用API>19,而是使用:

 evaluateJavascript(code, null);

或者,您可以改进代码以使用evaluateJavascript提供的回调。

Hm,它不起作用。看,我希望它会被修复,但现在没有解决办法。有什么想法吗?@RomanZhukov感谢您提供了这个问题的链接,从变更日志来看,这个问题已经在Android 5.1(以及forthcomming Webview 40版本)中得到了解决。我还没有测试我的应用程序的旧版本来验证这个事实,但我会这样做。@MikeFosker您有没有让它正常工作过?我遇到了一个听起来非常相似的问题,我还没有解决。