zoukankan      html  css  js  c++  java
  • android webview web里面的数据透传到java以及java的数据透传到web

    详见:
    http://tutorials.jenkov.com/android/android-web-apps-using-android-webview.html#android-web-app-or-android-hybrid-app

    Calling From Android Web App to JavaScript

    It is also possible to call JavaScript functions inside the WebView from your Android web app. You have two possibilities to do so. Both will be covered below.

    Calling JavaScript via WebView loadUrl()

    Before API level 19 (before Android 4.4 - Kitkat) you can use the WebView loadUrl() method like this:

    webView.loadUrl("javascript:theFunction('text')");
    This has the same effect as clicking on a JavaScript link inside the page currently loaded in the WebView. It does not result in a new page being loaded. Rather it results in the JavaScript being executed within the currently loaded page.

    The disadvantage of this method is that you cannot get any return values from the called function. However, you can arrange for the called JavaScript function to call back into Java with the result (how to call Java from JavaScript is explained earlier in this tutorial).

    Calling JavaScript via WebView evaluateJavascript()

    The second option is only available from Android API level 19 (Android Kitkat) and forward, Android's WebView class contains a method called evaluateJavascript(). This method can execute JavaScript as if it was executed inside the page currently loaded into the WebView . Here is an example of executing JavaScript via WebView evaluateJavascript() :

    webView.evaluateJavascript("fromAndroid()", new ValueCallback() {
    @Override
    public void onReceiveValue(String value) {
    //store / process result received from executing Javascript.
    }
    });
    The first parameter passed to evaluateJavascript() is the JavaScript string to evaluate (execute). The second parameter is a callback object which contains a single method named onReceiveValue. When the JavaScript has been evaluated and a result obtained from it, the onReceiveValue() method of this callback object is called. The Android web app can then process the value returned from exeuting the JavaScript.

    Keeping Page Navigation Inside the WebView With a WebViewClient

    The the users clicks a link in the web page loaded into the WebView, the default behaviour is to load that URL of the link in the system Android browser. That means that the Android browser app is opened and the page for the link is shown in the Android browser, and not inside the WebView in your app. This breaks the user experience of your app's users.

    To keep page navigation within the WebView and hence within your app, you need to create a subclass of WebViewClient, and override its shouldOverrideUrlLoading(WebView webView, String url) method. Here is how such a WebViewClient subclass could look:

    private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
    return false;
    }
    }
    When the shouldOverrideUrlLoading() method returns false, the URLs passed as parameter to the method is loaded inside the WebView instead of the Android standard browser. In the above example all URls will be loaded inside the WebView.

    If you want to distinguish between that URLs are loaded inside the WebView and which are loaded in the Android browser, your implementation of shouldOverrideUrlLoading() can examine the URL passed to it as parameter. Here is an example that only loads URLs that contains jenkov.com inside the WebView and all other URLs in the Android browser:

    public class WebViewClientImpl extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if(url.indexOf("jenkov.com") > -1 ) return false;
        return true;
    }
    

    }
    Weirdly enough, returning true from shouldOverrideUrlLoading() does not cause the URL to be loaded in the external Android browser. Rather, it causes the URL not to be loaded at all. To open all other URLs in the external Android browser you will have to fire an Intent. Here is how the WebViewClient subclass looks with that added:

    public class WebViewClientImpl extends WebViewClient {

    private Activity activity = null;
    
    public WebViewClientImpl(Activity activity) {
        this.activity = activity;
    }
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {
        if(url.indexOf("jenkov.com") > -1 ) return false;
    
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
        return true;
    }
    

    }
    Notice how the WebViewClientImpl class now takes an Activity in its constructor. This activity is used to fire the Intent which opens the URL in the Android browser.

  • 相关阅读:
    delphi新语法之泛型实现的对象池模板
    java输入函数
    OC----简单的购物系统----
    关于在堆区创建字符串的疑惑
    自己在OC考试中的试题
    nonatomic, retain,weak,strong用法详解
    python 读写文件和设置文件的字符编码
    python 逐行读取文件的三种方法
    Javascript写入txt和读取txt文件示例
    Javascript的io操作
  • 原文地址:https://www.cnblogs.com/xianqingzh/p/4333295.html
Copyright © 2011-2022 走看看