android.webkit.WebView.evaluateJavascript()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 JavaScript  
字(8.2k)|赞(0)|评价(0)|浏览(715)

本文整理了Java中android.webkit.WebView.evaluateJavascript()方法的一些代码示例,展示了WebView.evaluateJavascript()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebView.evaluateJavascript()方法的具体详情如下:
包路径:android.webkit.WebView
类名称:WebView
方法名:evaluateJavascript

WebView.evaluateJavascript介绍

暂无

代码示例

代码示例来源:origin: Justson/AgentWeb

private void evaluateJs(String js, final ValueCallback<String>callback){
  mWebView.evaluateJavascript(js, new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String value) {
      if (callback != null){
        callback.onReceiveValue(value);
      }
    }
  });
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
 float lux=sensorEvent.values[0];
 jsInterface.updateLux(lux);
 String js=String.format(Locale.US, "update_lux(%f)", lux);
 if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
  wv.evaluateJavascript(js, null);
 }
 else {
  wv.loadUrl("javascript:"+js);
 }
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
 float lux=sensorEvent.values[0];
 jsInterface.updateLux(lux);
 String js=String.format(Locale.US, "update_lux(%f)", lux);
 if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
  // postLux();
 }
 else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
  wv.evaluateJavascript(js, null);
 }
 else {
  wv.loadUrl("javascript:"+js);
 }
}

代码示例来源:origin: commonsguy/cw-omnibus

@TargetApi(Build.VERSION_CODES.KITKAT)
public void onLocationChanged(Location location) {
 StringBuilder buf=new StringBuilder("whereami(");
 buf.append(String.valueOf(location.getLatitude()));
 buf.append(",");
 buf.append(String.valueOf(location.getLongitude()));
 buf.append(")");
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  browser.evaluateJavascript(buf.toString(), null);
 }
 else {
  browser.loadUrl("javascript:" + buf.toString());
 }
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
 float lux=sensorEvent.values[0];
 jsInterface.updateLux(lux);
 String js=String.format(Locale.US, "update_lux(%f)", lux);
 if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) {
  wv.postWebMessage(new WebMessage(jsInterface.getLux()),
   Uri.EMPTY);
 }
 else if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
  wv.evaluateJavascript(js, null);
 }
 else {
  wv.loadUrl("javascript:"+js);
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
@Config(minSdk = 19)
public void evaluateJavascript() {
 assertThat(shadowOf(webView).getLastEvaluatedJavascript()).isNull();
 webView.evaluateJavascript("myScript", null);
 assertThat(shadowOf(webView).getLastEvaluatedJavascript()).isEqualTo("myScript");
}

代码示例来源:origin: TheFinestArtist/FinestWebView-Android

@Override
public void onPageFinished(WebView view, String url) {
 BroadCastManager.onPageFinished(FinestWebViewActivity.this, key, url);
 if (updateTitleFromHtml) {
  title.setText(view.getTitle());
 }
 urlTv.setText(UrlParser.getHost(url));
 requestCenterLayout();
 if (view.canGoBack() || view.canGoForward()) {
  back.setVisibility(showIconBack ? View.VISIBLE : View.GONE);
  forward.setVisibility(showIconForward ? View.VISIBLE : View.GONE);
  back.setEnabled(!disableIconBack && (rtl ? view.canGoForward() : view.canGoBack()));
  forward.setEnabled(!disableIconForward && (rtl ? view.canGoBack() : view.canGoForward()));
 } else {
  back.setVisibility(View.GONE);
  forward.setVisibility(View.GONE);
 }
 if (injectJavaScript != null) {
  webView.evaluateJavascript(injectJavaScript, null);
 }
}

代码示例来源:origin: AndroidHardening/PdfViewer

private void renderPage(final boolean lazy) {
  mWebView.evaluateJavascript(lazy ? "onRenderPage(true)" : "onRenderPage(false)", null);
}

代码示例来源:origin: stackoverflow.com

@Override
  public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      view.evaluateJavascript("javascript:document.getElementById('username').value ='" + strUsername + "';javascript:document.getElementById('password').value = '" + strPassword + "';javascript:document.getElementById('loginButton').click();", null);
    } else {
      view.loadUrl("javascript:document.getElementById('username').value = '" + strUsername + "';javascript:document.getElementById('password').value = '" + strPassword + "';javascript:document.getElementById('loginButton').click();");
    }

}

代码示例来源:origin: stackoverflow.com

final String js = "javascript:document.getElementById('j_password').value='something';";
webView.setWebViewClient(new WebViewClient() {
  @Override
  public void onPageFinished(WebView view, String url) {
    if (Build.VERSION.SDK_INT >= 19) {
      view.evaluateJavascript(js, new ValueCallback<String>() {
        @Override
        public void onReceiveValue(String s) { }
      });
    } else {
      view.loadUrl(js);
    }
   }
});

代码示例来源:origin: Flipboard/GoldenGate

@TargetApi(Build.VERSION_CODES.KITKAT)
protected static void evaluateJavascript(WebView webView, String javascript) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    webView.evaluateJavascript(javascript, null);
  } else  {
    webView.loadUrl("javascript:" + javascript);
  }
}

代码示例来源:origin: zfman/hputimetable

/**
   * 调用js方法(4.4之上)
   */
  @TargetApi(Build.VERSION_CODES.KITKAT)
  @SuppressLint("SetJavaScriptEnabled")
  private void callEvaluateJavascript(String method) {
    // 调用html页面中的js函数
    webView.evaluateJavascript(method, null);
  }
}

代码示例来源:origin: Flipboard/GoldenGate

@TargetApi(Build.VERSION_CODES.KITKAT)
protected static void evaluateJavascript(WebView webView, String javascript) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    webView.evaluateJavascript(javascript, null);
  } else  {
    webView.loadUrl("javascript:" + javascript);
  }
}

代码示例来源:origin: zeleven/mua

public void loadMarkdown(String markdown) {
    if (pageFinish) {
      String content = markdown.replace("\n", "\\n").replace("\"", "\\\"")
          .replace("'", "\\'");
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        webView.evaluateJavascript("javascript:parseMarkdown(\"" + content + "\");", null);
      } else {
        webView.loadUrl("javascript:parseMarkdown(\"" + content + "\");");
      }
    }
  }
}

代码示例来源:origin: stackoverflow.com

private void test() {
  WebView webview = new WebView(this);
  webview.getSettings().setJavaScriptEnabled(true);
  webview.loadUrl("");
  webview.evaluateJavascript("(function(){return {foo:"bar"}})()", new ValueCallback<String>() {
    @Override
    public void onReceiveValue(String s) {
      Log.e(TAG, s); // {foo:"bar"}
    }
  });
}

代码示例来源:origin: AlexMofer/ProjectX

@Override
  public void evaluateJavascript(WebView webView, String script,
                  ValueCallback resultCallback) {
    webView.evaluateJavascript(script, new ValueCallbackKitKat(resultCallback));
  }
}

代码示例来源:origin: easilycoder/EasyBridge

@Override
public void evaluateJavascript(String script, @Nullable ValueCallback<String> resultCallback) {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    super.evaluateJavascript(script, resultCallback);
  } else {
    loadUrl(String.format("%s%s", JAVA_SCRIPT_PROTOCOL, script));
  }
}

代码示例来源:origin: free46000/HybridFoundation

private static void execJs(WebView webView, String jsCode) {
  if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
    webView.post(() -> execJs(webView, jsCode));
  }
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    webView.evaluateJavascript(jsCode, null);
  } else {
    webView.loadUrl(jsCode);
  }
}

代码示例来源:origin: appnexus/mobile-sdk-android

private static void injectJavaScript(String url, WebView webView) {
    try {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        webView.evaluateJavascript(url, null);
      } else {
        webView.loadUrl(url);
      }
    } catch (Exception exception) {
      // We can't do anything much here if there is an exception ignoring.
      // This is to avoid crash of users app gracefully.
      Clog.e(Clog.baseLogTag, "ANJAMImplementation.loadResult -- Caught EXCEPTION...", exception);
      Clog.e(Clog.baseLogTag, "ANJAMImplementation.loadResult -- ...Recovering with webView.loadUrl.");
    }
  }
}

代码示例来源:origin: martykan/webTube

public static void toggleVideo() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    webView.evaluateJavascript("(function() { return document.getElementsByTagName('video')[0].paused; })();",
        value -> {
          if (value.equals("true")) {
            playVideo();
          } else {
            pauseVideo();
          }
        });
  } else {
    pauseVideo();
  }
}

相关文章

WebView类方法