一个完整webview的写法
来源:互联网 发布:mysql front 编辑:程序博客网 时间:2024/06/13 01:06
webView的java文件代码:
public class SportMe_MyOrderWebView extends AbstractBaseActivity{ private static final String TAG = "YunHuWebViewActivity"; private String APP_CACAHE_DIRNAME = "/webcache"; private String webUrl; private WebView webView; private LinearLayout loading_layout; private int index; @Override public void initIntentParam(Intent intent) { // TODO Auto-generated method stub title = getResources().getString(R.string.sports_myorder_me); if (intent != null) { webUrl = intent.getStringExtra("webUrl"); index = intent.getIntExtra("index",0); } } @Override public void initView() { // TODO Auto-generated method stub showContentView(R.layout.tran_webview); mSportsApp = (SportsApp) getApplication(); Log.d(TAG, "WebViewActivity inited"); webView = (WebView) findViewById(R.id.web_train); loading_layout = (LinearLayout) findViewById(R.id.loading_layout); webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); loadWebView(); } @Override public void setViewStatus() { // TODO Auto-generated method stub } @Override public void onPageResume() { MobclickAgent.onPageStart("Train_webViewClass"); } @Override public void onPagePause() { MobclickAgent.onPageEnd("Train_webViewClass"); } @Override public void onPageDestroy() { // TODO Auto-generated method stub clearWebViewCache(); CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeAllCookie(); webView.removeAllViews(); webView.destroy(); webView = null; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // 返回键退回 webView.goBack(); return true; } return super.onKeyDown(keyCode, event); } private void loadWebView() { WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); // 设置 缓存模式 // 开启 DOM storage API 功能 webSettings.setDomStorageEnabled(true); // 开启 database storage API 功能 webSettings.setDatabaseEnabled(true); String cacheDirPath = getFilesDir().getAbsolutePath() + APP_CACAHE_DIRNAME; Log.i(TAG, "cacheDirPath=" + cacheDirPath); // 设置数据库缓存路径 webSettings.setDatabasePath(cacheDirPath); // 设置 Application Caches 缓存目录 webSettings.setAppCachePath(cacheDirPath); // 开启 Application Caches 功能 webSettings.setAppCacheEnabled(true); webSettings.setUserAgentString("mfox"); webView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } }); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { if (progress == 100) loading_layout.setVisibility(View.GONE); } }); webView.loadUrl(webUrl); } /** * 清除WebView缓存 */ public void 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(TAG, "webviewCacheDir path=" + webviewCacheDir.getAbsolutePath()); // 删除webview 缓存目录 if (webviewCacheDir.exists()) { deleteFile(webviewCacheDir); } // 删除webview 缓存 缓存目录 if (appCacheDir.exists()) { deleteFile(appCacheDir); } } /** * 递归删除 文件/文件夹 */ public void 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(TAG, "delete file no exists " + file.getAbsolutePath()); } }}
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/mls_pay" > <WebView android:id="@+id/web_train" android:layout_width="fill_parent" android:layout_height="fill_parent" > </WebView> <LinearLayout android:id="@+id/loading_layout" android:layout_width="wrap_content" android:layout_centerInParent="true" android:layout_height="wrap_content" android:orientation="horizontal" > <ProgressBar android:id="@+id/progressBarItemImageLoading" style="@android:style/Widget.ProgressBar.Small" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginRight="6dp" android:indeterminateDrawable="@anim/record_loading" android:maxHeight="24dip" android:maxWidth="24dip" android:minHeight="24dip" android:minWidth="24dip" /> <TextView android:id="@+id/ItemText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:singleLine="true" android:text="@string/loading" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> </RelativeLayout></RelativeLayout>
阅读全文
1 0
- 一个完整webview的写法
- 一个较完整的连接数据库写法!(请大家参考)
- 单例的完整写法
- js动画效果(贴一个完整的代码,附js文件写法)
- WebView 的基本写法与配置
- ajax 的最完整的写法
- ajax 的最完整的写法
- 比较完整的原始ajax写法
- 完整单例模式的写法
- php $_SERVER当前完整url的写法
- iOS单例的完整写法
- recv和send的完整写法
- 一个视图的写法
- webview的一个例子
- 一个WebView的例子
- WebView的一个方法
- 一个基础的webview
- 一个完整的Java文件的代码分几个部分,每部分都起什么作用,大致的写法
- JS基础进阶1 数据类型
- Jedis示例
- 17-Letter Combinations of a Phone Number
- 阿里百川即时通讯的介绍
- Web 前端知识体系精简
- 一个完整webview的写法
- 31-NextPermutation
- HDU --- 4549 M斐波那契数列 【费马小定理+矩阵快速幂】
- 使用 libev 构建 TCP 响应服务器(echo server)的简单流程
- CodeForces
- c++2017-6-22语句
- java main方法参数传递
- 31-NextPermutation
- Java集合类详解