【问题标题】:Saving WebView and showing it while offline?保存 WebView 并在离线时显示它?
【发布时间】:2019-03-09 13:56:40
【问题描述】:

我需要以一种在没有互联网时它会使用缓存的方式缓存 WebView,而当有互联网时它将使用在线页面。由于我的 WebView 类在 onCreateView 中执行 loadUrl(),它每次都会重新加载它,所以我需要缓存有两个原因:加载速度更快,但主要用于应用程序的离线使用。

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;

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().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());
                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;
    }
}

我尝试了LOAD_DEFAULT 和LOAD_CACHE_ELSE_NETWORK,但似乎都不起作用;当我在关闭 WiFi 的情况下从日历片段切换回它时,我得到 net::ERR_ADDRESS_UNREACHABLE

更新:也不适用于LOAD_CACHE_ONLY

更新:添加了ACCESS_NETWORK_STATE 和ACCESS_WIFI_STATE,现在出现net::ERR_INTERNET_DISCONNECTED 错误

我从中学到的来源: Source 1 Source 2

【问题讨论】:

标签: java android android-webview android-websettings


【解决方案1】:

将这些权限添加到清单

<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"/>

试试这个代码,

webView = (WebView) view.findViewById(R.id.calendarWebView);
webView.getSettings().setAppCacheMaxSize( 8 * 1024 * 1024 ); 
webView.getSettings().setAppCachePath( 
getApplicationContext().getCacheDir().getAbsolutePath() );
webView.getSettings().setAllowFileAccess( true );
webView.getSettings().setAppCacheEnabled( true );
webView.getSettings().setJavaScriptEnabled( true );
webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); 

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" );

网络连通性检查方法

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( 
CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();}

【讨论】:

  • 谢谢,但我已经尝试过了。我已经有权限了。 getApplicationContext() 不起作用并显示“无法解析方法 getApplicationContext()”,isNetworkAvailable() 也是如此,它也无法解析。
  • 你需要在你的类中添加那个方法,isNetworkAvailable
  • 哦,是的,我的错。 getApplicationContext() 呢?也不应该对检查网络产生影响吗?它已经设置为 LOAD_CACHE_ELSE_NETWORK 所以它应该仍然可以工作。无论如何我会添加方法,同时 getApplicationContext() 呢?
  • 你能澄清一下你的意思吗? getSystemService() 也无法解析
  • 我做了 getContext() 而不是 getApplicationContext() 你觉得这样好吗?
猜你喜欢
  • 1970-01-01
  • 2019-06-14
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 2011-04-16
相关资源
最近更新 更多