详见:

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.

android webview web里面的数据透传到java以及java的数据透传到web的更多相关文章

  1. Android WebView选择本地文件上传

    This sample demonstrate android webview choose file to upload. I just implement the client code ,the ...

  2. Android WebView 加载超长 JS 数据

    在之前的文章里面,我总结过WebView如何与网页交互,也就是Java如何和JS交互 —— Android WebView 总结 —— Java和JavaScript交互. 基于这篇文章,我们基本上能 ...

  3. ESA2GJK1DH1K升级篇: STM32远程乒乓升级,基于Wi-Fi模块AT指令TCP透传方式,MQTT通信控制升级(含有数据校验)-APP用户程序制作过程

    前言 这一节和上一节是搭配的 给大家鱼,也必须给鱼竿! 我期望自己封装的代码,无论过了多少年都有应用的价值! 这节说明一下制作APP用户程序的过程 咱是用MQTT通信控制模块实现升级,所以首先自己的程 ...

  4. Android WebView 开发详解(二)

    转载请注明出处  http://blog.csdn.net/typename/article/details/39495409 powered by miechal zhao   概览: Androi ...

  5. Android WebView基本使用

    转载请注明出处: http://blog.csdn.net/lowprofile_coding/article/details/77928614 WebView介绍 Android WebView在A ...

  6. webview之如何设计一个优雅健壮的Android WebView?(下)(转)

    转载:https://iluhcm.com/2018/02/27/design-an-elegant-and-powerful-android-webview-part-two/ (这篇文章写得有点晚 ...

  7. android WebView详解,常见漏洞详解和安全源码

    这篇博客主要来介绍 WebView 的相关使用方法,常见的几个漏洞,开发中可能遇到的坑和最后解决相应漏洞的源码,以及针对该源码的解析.  转载请注明出处:http://blog.csdn.net/se ...

  8. 如何设计一个优雅健壮的Android WebView?(下)

    转:如何设计一个优雅健壮的Android WebView?(下) 前言 在上文<如何设计一个优雅健壮的Android WebView?(上)>中,笔者分析了国内WebView的现状,以及在 ...

  9. Android之网络----使用HttpClient发送HTTP请求(通过get方法获取数据)

    [正文] 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层 ...

  10. Android WebView 开发教程

    声明在先:必须在AndroidMainfest.xml 里面声明权限,否则在Java里面编写的所有WebView浏览网页的代码都无法正常使用 <uses-permission android:n ...

随机推荐

  1. mysql概要(六)连接

    内连接 [join on / from 表1,表二 ]效果一样 区别是:可以理解为首先取得笛卡儿积后,再 内连接:取俩表的匹配数据: 左连接:取的俩表匹配数据,并且保留未匹配数据中左表的数据,右表数据 ...

  2. js中cookie操作

    js中操作Cookie的几种常用方法 * cookie中存在域的概念,使用path和domain区分: * 在同一域中的set和del可以操作同一名称的cookie,但不在同一域中的情况下,则set无 ...

  3. [转载] Google数据中心网络技术漫谈

    原文: http://www.sdnlab.com/12700.html?from=timeline&isappinstalled=0#10006-weixin-1-52626-6b3bffd ...

  4. Python学习(21)python操作mysql数据库_操作

    目录 数据库连接 创建数据库表 数据库插入操作 数据库查询操作 数据库更新操作 删除操作 执行事务 错误处理 数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TEST. 在TEST数 ...

  5. jQuery插件开发全解析,类级别与对象级别开发

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  6. 关于level DB的相关资料

    可以参考: http://blog.csdn.net/houzengjiang/article/details/7718548 http://www.cnblogs.com/haippy/archiv ...

  7. 关于php的一些小知识

    浏览目录: 一.PHP的背景和优势: 二.PHP原理简介: 三.PHP运行环境配置: 四.编写简单的PHP代码以及测试. 一.PHP的背景和优势 1.1   什么是PHP? PHP是能让你生成动态网页 ...

  8. count-the-repetitions

    https://leetcode.com/problems/count-the-repetitions/ 下面是我的方法,结果对的,超时了... package com.company; class ...

  9. [css] 垂直居中方法

    原文链接:http://www.cnblogs.com/2050/p/3392803.html 一.text-algin:center; 适用于行内元素水平居中,如图片.按钮.文字, 但是在IE67下 ...

  10. 【服务器防护】VPN的ip变更,导致无法连接服务器,解决方法【阿里云ECS】

    在阿里云的管理控制台,云服务器ECS - 对应服务器 - 选“管理” - “连接管理终端” 通过这个入口,可以进入Linux云服务器,修改防火墙限制的IP即可