webView浅析

webView类是View类的一个扩展,用来显示网页,它不包含任何的网页浏览器的特征,像没有导航控制和地址栏,使用起来也很方便。

哪种情况下需要使用webView:当你想提供一个需要更新的信息时候,另一种情况是你想提供的数据总是需要网络返回。

在app中加入webView:

        在activity的布局中加入元素标签,

        在WebView中加载网页,使用loadUrl() :
        WebView webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl("http://www.example.com");

  当然要想要能够访问网页必须要在manifest文件中声明网络访问权限:
  

在WebView中使用JavaScript:

        如果想要用JavaScript加载网页,必须确保JavaScript可用,当JavaScript可用时,
        就能够创建应用程序代码和JavaScript代码间相互调用的接口。

        JavaScript默认不可用在webView中,如下代码设置JavaScript可用:
        WebView myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings提供了一系列其他的设置可能有用的为你。
        你能够定义一个自定义用户代理字符串通过setUserAgentString();网页查询这定义用户
        代理字符串确保客户端请求网页是来自你自己的安卓应用程序。

JavaScript代码和android代码之间建立关联:创建JavaScript code和客户端android code之间的interface

                 创建一个新的接口在通过addJavaScriptInterface(),下面这个类被创建在
                 客户端,
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或者更高,你必须添加@JavaScriptInterface
 WebAppInterface允许网页去创建一个toast消息。

 如下WebView webView = (WebView) findViewById(R.id.webview);
 webView.addJavascriptInterface(new WebAppInterface(this), "Android");
 创建了一个接口为JavaScript叫做Android,你的网页应用能够访问WebAppInterface类
 例如::
 

  
 JavaScript不需要初始化Android接口,这个webView自动使他可用为你的网页
 注意:使用addJavaScriptInterface(),允许JavaScript控制你的Android程序,这样就会带来安全隐患

处理页面导航:

    当用户点击一个连接在WebView中的网页,默认的行为是Android去启动一个应用来处理urls,通常
    默认浏览器打开并加载url,当然你能够为你的WebView覆盖这种行为,让链接打开在你的webView中,
    允许用户前进或者回退被webView所保持的网页历史记录

    用户打开链接的点击事件是被定义在WebViewClient为WebView,使用setWebViewClient()去设置
    WebView的WebViewClient();
    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient());
    现在所有的用户点击的链接都在WebView中;
    如果你想要更多的控制,为一个点击链接的加载;可以自己定义WebViewClient;重写      shouldOverrideUrlLoading();
    例如:
    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;
}

}

 然后为WebView设置自定义的WebViewClient
 webView.setWebViewClient(new MyWebViewClient());

网页历史记录的导航:
当你的webView重写了url loading,它自动的累积网页所访问的历史记录。你能够导航当前的网页前几
或后退通过历史记录,使用goBack();goForward()

下面是一个activity如何通过历史记录导航的:
@Override
public 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);

}

canGoBack();去检查一个网页是否能够goBack();canGoForward()去检查是否能够goForward();

你可能感兴趣的:(android)