webview清理缓存

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="webLoad"   
  11.         android:id="@+id/loadId"  
  12.         />  
  13.      <Button  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="clearCache"   
  17.           android:id="@+id/clearId"  
  18.         />  
  19.       
  20.       <WebView  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:id="@+id/webViewId"  
  24.           
  25.          />  
  26. </LinearLayout>  

  1. public class WebCacheActivity extends Activity implements OnClickListener {  
  2.     private Button clearBtn, loadBtn;  
  3.     private WebView webView;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.         clearBtn = (Button) findViewById(R.id.clearId);  
  10.         loadBtn = (Button) findViewById(R.id.loadId);  
  11.   
  12.         clearBtn.setOnClickListener(this);  
  13.         loadBtn.setOnClickListener(this);  
  14.         webView = (WebView) findViewById(R.id.webViewId);  
  15.         webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);  
  16.         WebSettings webSettings = webView.getSettings();         
  17.         webSettings.setJavaScriptEnabled(true);    
  18.           
  19.         webView.setWebViewClient(new WebViewClient() {  
  20.             public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  21.                 view.loadUrl(url);  
  22.                 return true;  
  23.             }  
  24.   
  25.         });  
  26.         webView.loadUrl("http://www.baidu.com/");  
  27.           
  28.     }  
  29.   
  30.     @Override  
  31.     public void onClick(View v) {  
  32.         switch (v.getId()) {  
  33.         case R.id.clearId://清除缓存的有效方法  
  34.             webView.loadDataWithBaseURL(null"","text/html""utf-8",null);  
  35.             break;  
  36.         case R.id.loadId:  
  37.             webView.loadUrl("http://www.baidu.com/");  
  38.             break;  
  39.         default:  
  40.             break;  
  41.         }  
  42.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值