webview和H5 清除缓存和缓存文件删除。缓存请看: java.io.File; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.util.Log; import android.view.View; import android.webkit.JsPromptResult; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebSettings.RenderPriority; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extendsActivity { privatestatic final String APP_CACAHE_DIRNAME = "/webcache"; privateTextView tv_topbar_title; privateRelativeLayout rl_loading; privateWebView mWebView; privateString url; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); url =" initView(); } privatevoid initView() { tv_topbar_title = (TextView) findViewById(R.id.tv_topbar_title); rl_loading = (RelativeLayout) findViewById(R.id.rl_loading); mWebView = (WebView) findViewById(R.id.mWebView); initWebView(); mWebView.setWebViewClient(newWebViewClient() { @Override publicvoid onLoadResource(WebView view, String url) { super.onLoadResource(view, url); } @Override publicboolean shouldOverrideUrlLoading(WebView webview, String url) { webview.loadUrl(url); returntrue; } @Override publicvoid onPageStarted(WebView view, String url, Bitmap favicon) { rl_loading.setVisibility(View.VISIBLE);// 显示加载界面 } @Override publicvoid onPageFinished(WebView view, String url) { String title = view.getTitle(); tv_topbar_title.setText(title); tv_topbar_title.setVisibility(View.VISIBLE); rl_loading.setVisibility(View.GONE);// 隐藏加载界面 } @Override publicvoid onReceivedError(WebView view,int errorCode, String description, String failingUrl) { rl_loading.setVisibility(View.GONE);// 隐藏加载界面 Toast.makeText(getApplicationContext(),"", Toast.LENGTH_LONG).show(); } }); mWebView.setWebChromeClient(newWebChromeClient() { @Override publicboolean onJsAlert(WebView view, String url, String message, JsResult result) { Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); result.confirm(); returntrue; } @Override publicboolean onJsConfirm(WebView view, String url, String message, JsResult result) { returnsuper.onJsConfirm(view, url, message, result); } @Override publicboolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) { returnsuper.onJsPrompt(view, url, message, defaultValue, result); } }); mWebView.loadUrl(url); } privatevoid initWebView() { mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setRenderPriority(RenderPriority.HIGH); mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); //设置 缓存模式选择自己想要的模式 // 开启 DOM storage API 功能 mWebView.getSettings().setDomStorageEnabled(true); //开启 database storage API 功能 mWebView.getSettings().setDatabaseEnabled(true); String cacheDirPath = getFilesDir().getAbsolutePath()APP_CACAHE_DIRNAME; //设置数据库缓存路径 mWebView.getSettings().setDatabasePath(cacheDirPath); //设置 Application Caches 缓存目录 mWebView.getSettings().setAppCachePath(cacheDirPath); //开启 Application Caches 功能 mWebView.getSettings().setAppCacheEnabled(true); } /** * 清除WebView缓存 在onDestroy调用这个方法就可以了 */ publicvoid clearWebViewCache(){ //清理Webview缓存数据库 try{ deleteDatabase("webview.db"); deleteDatabase("webviewCache.db"); }catch (Exception e) { e.printStackTrace(); } //WebView 缓存文件 File appCacheDir =new File(getFilesDir().getAbsolutePath()APP_CACAHE_DIRNAME); Log.e(TAG,"appCacheDir path="appCacheDir.getAbsolutePath()); File webviewCacheDir =new File(getCacheDir().getAbsolutePath()"/webviewCache"); Log.e("path===","webviewCacheDir path="webviewCacheDir.getAbsolutePath()); //删除webview 缓存目录 if(webviewCacheDir.exists()){ deleteFile(webviewCacheDir); } //删除webview 缓存 缓存目录 if(appCacheDir.exists()){ deleteFile(appCacheDir); } } /** * 递归删除 文件/文件夹 * * @param file */ publicvoid deleteFile(File file) { Log.i(TAG,"delete file path=" file.getAbsolutePath()); if(file.exists()) { if(file.isFile()) { file.delete(); }else if(file.isDirectory()) { File files[] = file.listFiles(); for(int i = 0; i < files.length; i) { deleteFile(files[i]); } } file.delete(); }else { Log.e("delete===","delete file no exists " file.getAbsolutePath()); } } }
推荐整理分享webview和H5 清除缓存和缓存文件删除。(webview清除数据),希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:h5如何清缓存,webview清除缓存,webview缓存h5页面,webview清除缓存,webview清除cookie,h5怎么删除页面,webview清除缓存,webview清除数据,内容如对您有帮助,希望把文章链接给更多的朋友!
layout_gravity与gravity的区别 TextViewandroid:layout_width=match_parentandroid:layout_height=match_parentandroid:gravity=centerandroid:text=@string/began_scan_do/例如TextView中使用的android:gravity=center意思是文本将
Android屏幕适配全攻略(最权威的官方适配指导) 转载请注明出处:
Android 开源交流分享汇总 目录开源交流1群------开源交流2群------开源交流3群开源交流4群------开源交流5群------开源交流6群原创简介几群作者加速球效果4群gavin可翻转Layout(正反面)