WebView代码交互
来源:互联网 发布:软件需求分析常见问题 编辑:程序博客网 时间:2024/06/05 09:22
WebView代码交互
作者: 郭嘉
邮箱: guoxiaoxingv@163.com
博客: https://guoxiaoxing.github.io/
知乎: https://www.zhihu.com/people/allen-wells
关于作者
Android Coder一枚, 目前就职于杭州大搜车汽车汽车服务有限公司。爱技术、爱烹饪、爱小提琴、爱一切新鲜有趣的事物。
人生格言: 不想当程序员的歌手不是好厨师。
关于文章
作者的每一篇文章都会同时发布在Github、CSDN和知乎上, 文章顶部也会附上Github上的文章链接和代码链接。如果文章中有什么疑问欢迎发邮件与
我交流, 对于交流的问题, 请描述清楚并附上代码与日志, 我一般都会给予回复。如果文章中有什么错误, 也欢迎斧正。如果你觉得本文章对你
有所帮助, 也欢迎去star文章, 关注文章的最新的动态。
本系列文章讨论WebView的各种用法以及使用技巧, 一共包含三篇文章:
WebView基本用法
WebView代码交互
WebView性能优化
一 Android原生方案
关于WebView中Java代码和JS代码的交互实现, Android给了一套原生的方案, 我们先来看看原生的用法。后面我们还会讲到其他的开源方法。
JavaScript代码和Android代码是通过addJavascriptInterface()来建立连接的, 我们来看下具体的用法。
1 设置WebView支持JavaScript
webView.getSettings().setJavaScriptEnabled(true);
2 在Android工程里定义一个接口
public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mContext = c; } /** Show a toast from the web page */ @JavascriptInterface public void showToast(String toast) { Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); }}
注意: API >= 17时, 必须在被JavaScript调用的Android方法前添加@JavascriptInterface注解, 否则将无法识别。
3 在Android代码中将该接口添加到WebView
WebView webView = (WebView) findViewById(R.id.webview);webView.addJavascriptInterface(new WebAppInterface(this), "Android");
这个”Android”就是我们为这个接口取的别名, 在JavaScript就可以通过Android.showToast(toast)这种方式来调用此方法。
4 在JavaScript中调用Android方法
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /><script type="text/javascript"> function showAndroidToast(toast) { Android.showToast(toast); }</script>
在JavaScript中我们不用再去实例化WebAppInterface接口, WebView会自动帮我们完成这一工作, 使它能够为WebPage所用。
注意:
由于addJavascriptInterface()给予了JS代码控制应用的能力, 这是一项非常有用的特性, 但同时也带来了安全上的隐患,
Using addJavascriptInterface() allows JavaScript to control your Android application. This can be a very useful feature or a dangerous
security issue. When the HTML in the WebView is untrustworthy (for example, part or all of the HTML is provided by an unknown person or
process), then an attacker can include HTML that executes your client-side code and possibly any code of the attacker’s choosing. As such,
you should not use addJavascriptInterface() unless you wrote all of the HTML and JavaScript that appears in your WebView. You should also
not allow the user to navigate to other web pages that are not your own, within your WebView (instead, allow the user’s default browser
application to open foreign links—by default, the user’s web browser opens all URL links, so be careful only if you handle page navigation
as described in the following section).
下面正式引入我们在项目中常用的两套开源的替代方案
二 jockeyjs开源方案
jockeyjs是一套IOS/Android双平台的Native和JS交互方法, 比较适合用在项目中。
Library to facilitate communication between iOS apps and JS apps running inside a UIWebView
jockeyjs对Native和JS的交互做了优美的封装, 事件的发送与接收都可以通过send()和on()来完成。我们先简单的看一下Event的发送与接收。
Sending events from app to JavaScript
// Send an event to JavaScript, passing a payloadjockey.send("event-name", webView, payload);//With a callback to execute after all listeners have finishedjockey.send("event-name", webView, payload, new JockeyCallback() { @Override public void call() { //Your execution code }});
Receiving events from app in JavaScript
// Listen for an event from iOS, but don't notify iOS we've completed processing// until an asynchronous function has finished (in this case a timeout).Jockey.on("event-name", function(payload, complete) { // Example of event'ed handler. setTimeout(function() { alert("Timeout over!"); complete(); }, 1000);});
Sending events from JavaScript to app
// Send an event to iOS.Jockey.send("event-name");// Send an event to iOS, passing an optional payload.Jockey.send("event-name", { key: "value"});// Send an event to iOS, pass an optional payload, and catch the callback when all the// iOS listeners have finished processing.Jockey.send("event-name", { key: "value"}, function() { alert("iOS has finished processing!");});
Receiving events from JavaScript in app
//Listen for an event from JavaScript and log a message when we have receied it.jockey.on("event-name", new JockeyHandler() { @Override protected void doPerform(Map<Object, Object> payload) { Log.d("jockey", "Things are happening"); }});//Listen for an event from JavaScript, but don't notify the JavaScript that the listener has completed//until an asynchronous function has finished//Note: Because this method is executed in the background, if you want the method to interact with the UI thread//it will need to use something like a android.os.Handler to post to the UI thread.jockey.on("event-name", new JockeyAsyncHandler() { @Override protected void doPerform(Map<Object, Object> payload) { //Do something asynchronously //No need to called completed(), Jockey will take care of that for you! }});//We can even chain together several handlers so that they get processed in sequence.//Here we also see an example of the NativeOS interface which allows us to chain some common//system handlers to simulate native UI interactions.jockey.on("event-name", nativeOS(this) .toast("Event occurred!") .vibrate(100), //Don't forget to grant permission new JockeyHandler() { @Override protected void doPerform(Map<Object, Object> payload) { } });//...More Handlers//If you would like to stop listening for a specific eventjockey.off("event-name");//If you would like to stop listening to ALL eventsjockey.clear();
通过上面的代码, 我们对jockeyjs的使用有了大致的理解, 下面我们具体来看一下在项目中的使用。
1 依赖配置
下载代码: https://github.com/tcoulter/jockeyjs, 将JockeyJS.Android导入到工程中。
2 jockeyjs配置
jockeyjs有两种使用方式
方式一:
只在一个Activity中使用jockey或者多Activity共享一个jockey实例
//Declare an instance of JockeyJockey jockey;//The WebView that we will be using, assumed to be instantiated either through findViewById or some method of injection.WebView webView;WebViewClient myWebViewClient;@Overrideprotected void onStart() { super.onStart(); //Get the default JockeyImpl jockey = JockeyImpl.getDefault(); //Configure your webView to be used with Jockey jockey.configure(webView); //Pass Jockey your custom WebViewClient //Notice we can do this even after our webView has been configured. jockey.setWebViewClient(myWebViewClient) //Set some event handlers setJockeyEvents(); //Load your webPage webView.loadUrl("file:///your.url.com");}
方式二:
另一种就是把jockey当成一种全局的Service来用, 这种方式下我们可以在多个Activity之间甚至整个应用内共享handler. 当然我们同样需要
把jockey的生命周期和应用的生命周期绑定在一起。
//First we declare the members involved in using Jockey//A WebView to interact withprivate WebView webView;//Our instance of the Jockey interfaceprivate Jockey jockey;//A helper for binding servicesprivate boolean _bound;//A service connection for making use of the JockeyServiceprivate ServiceConnection _connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { _bound = false; } @Override public void onServiceConnected(ComponentName name, IBinder service) { JockeyBinder binder = (JockeyBinder) service; //Retrieves the instance of the JockeyService from the binder jockey = binder.getService(); //This will setup the WebView to enable JavaScript execution and provide a custom JockeyWebViewClient jockey.configure(webView); //Make Jockey start listening for events setJockeyEvents(); _bound = true; //Redirect the WebView to your webpage. webView.loadUrl("file:///android_assets/index.html"); }}///....Other member variables....//////Then we bind the JockeyService to our activity through a helper function in our onStart method@Overrideprotected void onStart() { super.onStart(); JockeyService.bind(this, _connection);}//In order to bind this with the Android lifecycle we need to make sure that the service also shuts down at the appropriate time.@Overrideprotected void onStop() { super.onStop(); if (_bound) { JockeyService.unbind(this, _connection); }}
以上便是jockeyjs的大致用法.
- WebView代码交互
- Android代码与webview控件的交互
- ios中webview与js交互代码
- Android WebView与JavaScript代码交互
- (二)WebView实践之代码交互
- webview交互
- webview交互
- Android中WebView载入本地HTML代码并实现交互
- Android WebView中的JavaScript和java代码的交互
- 使用WebView中的Javascript和本地代码交互
- Android中WebView载入本地HTML代码并实现交互
- webview 中js交互 代码混淆后失效
- iOS原生代码通过webView与js脚本交互
- WebView的JavaScript与本地代码三种交互方式
- WebView的JavaScript与本地代码三种交互方式
- WebView的JavaScript与本地代码三种交互方式
- WebView使用与JavaScript交互详解(附完整Demo代码)
- Webview与js交互漏洞及解决方法(有注释代码)
- iphone数据存储之-- Core Data的使用(一)
- 设计模式GOF23——工厂模式
- 回味经典——uboot1.1.6 之 第二阶段 第三阶段
- listView实现圆角显示(第一个是左上角和右下角有圆角 中间没有圆角 最后一个是右下角和左下角圆角)
- Android ADB命令 浅试
- WebView代码交互
- count和capacity的区别
- 顺序表逆置法换元素位置
- 最小二乘法Java实现
- QT打包程序
- WebView性能优化
- 初步建模 2016.07.28回顾
- xUtils框架中关于注解的使用
- c# 调用webApi