WebView 相关知识点

时间:2021-05-07 00:31:03
//webview 的常用设置

final
WebView webView = (WebView) findViewById(R.id.hot_app); WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(
true);//设置js 可用
webSettings.setSupportZoom(
true);
webSettings.setBuiltInZoomControls(
true);//支持控件缩放
webSettings.setAllowFileAccess(
true);
// webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.requestFocus();//可聚焦
final String sUrl = Config.URL_HOT_APP;

webView.setWebChromeClient(
new WebChromeClient());
webView.setWebViewClient(
new WebViewClient()//采用内嵌的方式打开网页
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});

    //支持文件下载 webView.setDownloadListener(
new DownloadListener()
{
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
{
Uri uri
= Uri.parse(url);
Intent intent
= new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});

webView.loadUrl(sUrl);
//session 缓存的处理
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context); CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie();//移除sessino缓存 ,因为该操作另开线程执行,故当前线程可能需要等待2s 直到session被清除,再重新设置。
try
{
Thread.sleep(
2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}

cookieManager.setAcceptCookie(
true);
String mSessionValue
= getSessionValue(sUrl);//获取session值
String cookieString
= "JSESSIONID=" + mSessionValue + " ; domain=oa.myemsp.cn";
Trace.Log(
"cookieString : "+cookieString);

cookieManager.setCookie(
"oa.myemsp.cn", cookieString);//设置session值
cookieSyncManager.sync();