Android中WebView的使用总结及Java与Javascript的相互调用

来源:互联网 发布:淘宝刷机 编辑:程序博客网 时间:2024/05/29 11:36

Webview可以提供一个web应用程序或者一个网页做为Android客户端的一部分,但它又不包含网页浏览器的很多特性,基本上只是用于显示一个网页。


将WebView添加到应用

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/webview"    android:layout_width="fill_parent"    android:layout_height="fill_parent"/>
使用 loadUrl()来载入页面

WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.loadUrl("http://www.example.com");
如果要访问网络,必须添加INTERNET权限
<?xml version="1.0" encoding="utf-8"?><pre name="code" class="html"><manifest ... >    <uses-permission android:name="android.permission.INTERNET" />    ...</manifest>
如果加载本机HTML文件,路径如下:
file:///android_asset/index.html   加载项目assets下的文件index.htmlfile:///sdcard/index.html        加载sdcard下的index.html文件

在WebView中使用JavaScript

首先必须添加以下设置
WebView myWebView = (WebView) findViewById(R.id.webview);WebSettings webSettings = myWebView.getSettings();webSettings.setJavaScriptEnabled(true);
绑定JavaScript到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();    }}
注意:如果你的targetSdkVersion大于等于17,你必须在你需要在JavaScript中调用的Android方法前面添加@JavascriptInterface注释
将以上创建的类注入到JavaScript
WebView webView = (WebView) findViewById(R.id.webview);webView.addJavascriptInterface(new WebAppInterface(this), "Android");
对应的HTML代码
<pre name="code" class="html"><input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /><script type="text/javascript">    function showAndroidToast(toast) {        Android.showToast(toast);    }</script>
注意:你绑定到JavaScript的目标是在另外一个线程运行,而不是创建它的线程。

处理页面导航

当用户在WebView里面点击了一个网页链接之后,Android默认启动一个应用来处理这些链接,通常是web浏览器。但是你可以通过重写WebView方法来实现在WebView中打开这些链接。
WebView myWebView = (WebView) findViewById(R.id.webview);myWebView.setWebViewClient(new WebViewClient());
private class MyWebViewClient extends WebViewClient {    @Override    public boolean shouldOverrideUrlLoading(WebView view, String url) {        if (Uri.parse(url).getHost().equals("www.example.com")) {            // This is my web site, so do not override; let my WebView load the page            return false;        }        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));        startActivity(intent);        return true;    }}
当用户点击back键时,应用默认是退出,通过以下代码可以将back键改成后退
@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {    // Check if the key event was the Back button and if there's history    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {        myWebView.goBack();        return true;    }    // If it wasn't the Back key or there's no web page history, bubble up to the default    // system behavior (probably exit the activity)    return super.onKeyDown(keyCode, event);}




0 0
原创粉丝点击