一个完整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>
原创粉丝点击