WebView缓存未保存缓存

时间:2019-03-10 23:13:21

标签: java android webview android-webview android-websettings

我正在尝试缓存WebView,但是它不起作用。具体来说,当我使用WebView退出片段时,关闭WiFi并返回到片段,当我检查Network和LOAD_CACHE_ONLY时,它给了我错误:net :: ERR_CACHE_MISS。当我在不检查网络连接的情况下将其设置为LOAD_CACHE_ELSE_NETWORK并在加载后关闭WiFi时,即使WiFi开启,我也会得到空白的白屏。

在链接到其他线程之前,我已经使用过this和其他一些

这是我的代码

使用WebView控制片段的类:

import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.net.ConnectivityManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static android.content.Context.CONNECTIVITY_SERVICE;

public class CalendarFragment extends Fragment {
    private WebView webView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.calendar_layout, container, false);
        webView = (WebView) view.findViewById(R.id.calendarWebView);
        webView.getSettings().setAppCacheMaxSize( 8 * 1024 * 1024 );
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCachePath(getContext().getCacheDir().getPath());
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.setWebViewClient(new WebViewClient());
        if ( !isNetworkAvailable() )  //offline
            //webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY );
        webView.loadUrl("https://calendar.google.com/calendar/htmlembed?src=wlmacci%40gmail.com&ctz=America%2FToronto");
        //webView.loadUrl("https://sites.google.com/view/wlmac/textfile?");
        //webView.loadUrl("https://google.ca");
        //webView.getSettings().setLoadWithOverviewMode(true);
        //webView.getSettings().setUseWideViewPort(true);
        return view;
    }
    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getContext().getSystemService( CONNECTIVITY_SERVICE );
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.starenkysoftware.macapp">
    <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"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">

        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

其中包含WebView的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/holo_red_light">


    <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Calendar Fragment"
    android:textSize="30sp"
    android:gravity="center"
    android:layout_centerInParent="true"/>

    <WebView
        android:id="@+id/calendarWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </WebView>

</RelativeLayout>

我将根据要求提供其他必要的资源/代码。任何帮助表示赞赏。

0 个答案:

没有答案
相关问题