1.android源码中封装了一个WebViewFragment:
- 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:
- /** * 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(); }
(4)网上还有文章说,WebView进入后台以后,WebViewCoreThread还在运行,这个问题在Android 4中仍然存在,参考:
http://www.anddev.org/other-coding-problems-f5/webviewcorethread-problem-t10234.html,解决办法也很简单,因为新的WebView提供了两个方法:
- /** * 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(); }
2.关于WebView和Cookie:
假如是Web页面设置的Cookie,native不用做任何处理,WebView在访问同一个站点的时候,会把cookie信息携带上去,而如果是native想设置一个cookie,还能传递到web页面中,就需要做些额外的工作了。
这是我们的webview:
- webView = (WebView) this.findViewById(R.id.webview1);webView.setWebViewClient(new WebViewClient());webView.loadUrl("http://10.73.59.112:8080/sess/login.jsp");
- <% 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>
- <% 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>
- class webViewClient extends WebViewClient { // 重写shouldOverrideUrlLoading方法,使点击链接后不使用其他的浏览器打开。 @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); // 如果不需要其他对点击链接事件的处理返回true,否则返回false return true; } }
假如我们想一打开页面的时候,就传递一个cookie到web页面,可以再打开页面之前这样传递cookie:
- synCookies(this, "http://10.73.59.112:8080/sess/login.jsp");
- 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了。
关于拦截:
- 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:
- <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%><% response.sendRedirect("second.jsp");%>
- <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%><% String dir = request.getSession().getServletContext().getRealPath("/"); response.sendRedirect("third.jsp");%>
- <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%>hello,this is third
客户端的输出:
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。