WebView使用方法

来源:互联网 发布:新三板最新数据 编辑:程序博客网 时间:2024/06/04 19:05

1.android源码中封装了一个WebViewFragment:

view plaincopy to clipboardprint?
  1. public class WebViewFragment extends Fragment {    private WebView mWebView;    private boolean mIsWebViewAvailable;    public WebViewFragment() {    }    /**     * Called to instantiate the view. Creates and returns the WebView.     */    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        if (mWebView != null) {            mWebView.destroy();        }        mWebView = new WebView(getActivity());        mIsWebViewAvailable = true;        return mWebView;    }    /**     * Called when the fragment is visible to the user and actively running. Resumes the WebView.     */    @Override    public void onPause() {        super.onPause();        mWebView.onPause();    }    /**     * Called when the fragment is no longer resumed. Pauses the WebView.     */    @Override    public void onResume() {        mWebView.onResume();        super.onResume();    }    /**     * Called when the WebView has been detached from the fragment.     * The WebView is no longer available after this time.     */    @Override    public void onDestroyView() {        mIsWebViewAvailable = false;        super.onDestroyView();    }    /**     * Called when the fragment is no longer in use. Destroys the internal state of the WebView.     */    @Override    public void onDestroy() {        if (mWebView != null) {            mWebView.destroy();            mWebView = null;        }        super.onDestroy();    }    /**     * Gets the WebView.     */    public WebView getWebView() {        return mIsWebViewAvailable ? mWebView : null;    }}  
这种用法应该是最正宗的使用方式,有两点要注意:

(1)webview是用代码new出来的,而非配置在xml中的。网上有文章说写在xml中会导致内存无法释放,没验证过。

(2)webview也是有生命周期的,要注意onPause()和onResume()方法。

WebView.java:

view plaincopy to clipboardprint?
  1. /**     * Pauses any extra processing associated with this WebView and its     * associated DOM, plugins, JavaScript etc. For example, if this WebView is     * taken offscreen, this could be called to reduce unnecessary CPU or     * network traffic. When this WebView is again "active", call onResume().     * Note that this differs from pauseTimers(), which affects all WebViews.     */    public void onPause() {        checkThread();        if (DebugFlags.TRACE_API) Log.d(LOGTAG, "onPause");        mProvider.onPause();    }    /**     * Resumes a WebView after a previous call to onPause().     */    public void onResume() {        checkThread();        if (DebugFlags.TRACE_API) Log.d(LOGTAG, "onResume");        mProvider.onResume();    }  
(3)创建WebView的时候的Context应该是Activity而非Application。如果传入的是Application,那么WebView将无法弹出Dialog,而且无法播放flash。

(4)网上还有文章说,WebView进入后台以后,WebViewCoreThread还在运行,这个问题在Android 4中仍然存在,参考:

http://www.anddev.org/other-coding-problems-f5/webviewcorethread-problem-t10234.html,解决办法也很简单,因为新的WebView提供了两个方法:

view plaincopy to clipboardprint?
  1. /**     * Pauses all layout, parsing, and JavaScript timers for all WebViews. This     * is a global requests, not restricted to just this WebView. This can be     * useful if the application has been paused.     */    public void pauseTimers() {        checkThread();        if (DebugFlags.TRACE_API) Log.d(LOGTAG, "pauseTimers");        mProvider.pauseTimers();    }    /**     * Resumes all layout, parsing, and JavaScript timers for all WebViews.     * This will resume dispatching all timers.     */    public void resumeTimers() {        checkThread();        if (DebugFlags.TRACE_API) Log.d(LOGTAG, "resumeTimers");        mProvider.resumeTimers();    }  
方便起见,调用了mWebView.onPause();以后,顺带调用下mWebView.pauseTimers();就可以了,当然在调用了mWebView.onResume();以后,也要调用下resumeTimers()就ok了,可以写一个带timer的页面,在DDMS验证下。

2.关于WebView和Cookie:

假如是Web页面设置的Cookie,native不用做任何处理,WebView在访问同一个站点的时候,会把cookie信息携带上去,而如果是native想设置一个cookie,还能传递到web页面中,就需要做些额外的工作了。

这是我们的webview:

view plaincopy to clipboardprint?
  1. webView = (WebView) this.findViewById(R.id.webview1);webView.setWebViewClient(new WebViewClient());webView.loadUrl("http://10.73.59.112:8080/sess/login.jsp");  

login.jsp:

view plaincopy to clipboardprint?
  1. <%       Cookie cookies[] = request.getCookies() ;       if(cookies != null && cookies.length > 0){           out.print("<br/>");           out.print("cookie:<br/>");            for(Cookie cookie : cookies ){              out.print(cookie.getName() +"=" + cookie.getValue() +"<br/>");            }       }       %>       <br/>     <form action="result.jsp">            username:<input type="text" name="username"><br/>           password:<input type="text" name="password"><br/>           <input type="submit" value="submit"/>     </form>  
点击submit按钮,会跳转到result.jsp:

view plaincopy to clipboardprint?
  1. <%       String username = (String)request.getParameter("username");     String password = (String)request.getParameter("password");     if(username != null && username.length() > 0){           Cookie cookie = new Cookie("username",username) ;           cookie.setMaxAge(120) ;         response.addCookie(cookie);     }       if(password != null && password.length() > 0){           Cookie cookie = new Cookie("password",password) ;           cookie.setMaxAge(120) ;         response.addCookie(cookie);     }       %>       <a href="login.jsp">back</a>  

这里是有一个back连接,然后会把username和password写到cookie中,如果点击back返回到login.jsp,页面上应该会输出刚才设置的cookie。为了能在同一个webview中显示页面内容,我们要自定义一个WebViewClient:

view plaincopy to clipboardprint?
  1. class webViewClient extends WebViewClient {     // 重写shouldOverrideUrlLoading方法,使点击链接后不使用其他的浏览器打开。      @Override       public boolean shouldOverrideUrlLoading(WebView view, String url) {         view.loadUrl(url);          // 如果不需要其他对点击链接事件的处理返回true,否则返回false            return true;        }   }  

假如我们想一打开页面的时候,就传递一个cookie到web页面,可以再打开页面之前这样传递cookie:

view plaincopy to clipboardprint?
  1. synCookies(this"http://10.73.59.112:8080/sess/login.jsp");  
view plaincopy to clipboardprint?
  1. public static void synCookies(Context context, String url) {    CookieSyncManager.createInstance(context);      CookieManager cookieManager = CookieManager.getInstance();      cookieManager.setAcceptCookie(true);    cookieManager.removeSessionCookie();//移除    cookieManager.setCookie(url, "hello=world; Expires=Fri, 09-Aug-2014 02:37:42 GMT");     CookieSyncManager.getInstance().sync();  <span style="font-family: Arial, Helvetica, sans-serif;">}  </span>  

打开页面的时候,就能和看到我们写到的hello=world这个cookie了。


关于拦截:

view plaincopy to clipboardprint?
  1. webView.setWebViewClient(new WebViewClient() {              @Override   public boolean shouldOverrideUrlLoading(WebView view, String url) {     Log.e("test""shouldOverrideUrlLoading,"+url);     return false;   }   public void onPageStarted(WebView view, String url, Bitmap favicon) {       Log.e("test""onPageStarted,"+url);    }   @Override   public void onPageFinished(WebView webView, String url){        Log.e("test""onPageFinished,"+url);   }  });    

服务端代码:

first.jsp:

view plaincopy to clipboardprint?
  1. <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%><%   response.sendRedirect("second.jsp");%>  
second.jsp:

view plaincopy to clipboardprint?
  1. <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%><%   String dir = request.getSession().getServletContext().getRealPath("/"); response.sendRedirect("third.jsp");%>  
third,jsp:

view plaincopy to clipboardprint?
  1. <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%>hello,this is third  
如果访问:webView.loadUrl("http://10.73.59.166:8080/web/first.jsp");

客户端的输出:

10-15 14:17:15.558: E/test(26181): onPageStarted,http://10.73.59.166:8080/web/first.jsp
10-15 14:17:15.568: E/test(26181): onPageStarted,http://10.73.59.166:8080/web/second.jsp
10-15 14:17:15.568: E/test(26181): shouldOverrideUrlLoading,http://10.73.59.166:8080/web/second.jsp
10-15 14:17:15.578: E/test(26181): onPageStarted,http://10.73.59.166:8080/web/third.jsp
10-15 14:17:15.578: E/test(26181): shouldOverrideUrlLoading,http://10.73.59.166:8080/web/third.jsp
10-15 14:17:15.658: E/test(26181): onPageFinished,http://10.73.59.166:8080/web/third.jsp

也就是说:

(1)第一个url无法进入shouldOverrideUrlLoading。shouldOverrideUrlLoading的意思是说,在webview中打开链接的方式,前提是已经打开了webview。

(2)302响应的只进入onPageStarted,不进入onPageFinished。

0 0