Android-如何正确使用WebView

转载请注明出处:http://blog.csdn.net/goldenfish1919/article/details/38435799

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();
    }
(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提供了两个方法:

 /**
     * 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:

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

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>
点击submit按钮,会跳转到result.jsp:

<%
		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:

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");
%>
second.jsp:

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

<%@ 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。







  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值