离线Webview

时间:2014-01-10 21:40:05

标签: android webview

我有一个应用程序,它需要一个网站,并使用JavaScript来覆盖一些CSS,使其更具移动性。我试图添加一个离线模式,但无法让它工作。我尝试了以下代码,但它没有用。

我添加了这些权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

应用代码:

package com.aquamorph.frcmanual;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Introduction extends  Fragment  {


@Override
public void onActivityCreated(Bundle savedInstanceState) {        
    super.onActivityCreated(savedInstanceState);
}

@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View myFragmentView = inflater.inflate(R.layout.introduction, container, false);

    final ProgressDialog pd = ProgressDialog.show(getActivity(), "FRC Manual", "Loading");
    final WebView webView = (WebView) myFragmentView.findViewById(R.id.webview1);
    String url = "http://frc-manual.usfirst.org/viewItem/178";

    webView.getSettings().setJavaScriptEnabled(true);
    webView.setVerticalScrollBarEnabled(false);
    webView.setHorizontalScrollBarEnabled(false);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setRenderPriority(RenderPriority.HIGH);

    webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
    webView.getSettings().setAppCachePath("/data/data/de.app/cache");
    webView.getSettings().setAllowFileAccess( true );
    webView.getSettings().setAppCacheEnabled( true );
    webView.getSettings().setJavaScriptEnabled( true );
    webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default

    if ( !isNetworkAvailable() ) { // loading offline
        webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
    }

    webView.loadUrl(url);



    webView.setWebViewClient(new MyWebViewClient() {
     @Override
    public void onPageFinished(WebView view, String url)
    {
         webView.loadUrl("javascript:var con = document.getElementById('header_container'); " +
                 "con.style.display = 'none'; javascript:var con = document.getElementById('footer'); " +
                 "con.style.display = 'none'; javascript:var con = document.getElementsByClassName('ChapterTools RightPos')[0].style.visibility='hidden';"
                 + "javascript:var con = document.getElementsByClassName('colmid')[0].style.right='100%'; "
                 + "javascript:var con = document.getElementsByClassName('colleft')[0].style.right='initial'; "
                 + "javascript:var con = document.getElementsByClassName('col1')[0].style.width='98%'; "
                 + "javascript:var con = document.getElementsByClassName('colmask threecol')[0].style.right='2%'; "
                 + "javascript:var con = document.getElementsByClassName('colmask threecol')[0].style.width=''; "
                 + "javascript:var img = document.getElementsByTagName('img'); for (var i = 0; i < img.length; ++i) {img[i].style.maxWidth='100%';} "
                 + "javascript:var img = document.getElementsByTagName('img'); for (var i = 0; i < img.length; ++i) {img[i].style.height='';} "
                 + "javascript:var img = document.getElementsByTagName('table'); for (var i = 0; i < img.length; ++i) {img[i].style.width='';} ");

         pd.dismiss();
    }
    });

    return myFragmentView;


}


private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}


}

0 个答案:

没有答案