![](/img/trans.png)
[英]How to load url in webview in background while splashscreen is showing?
[英]Saving WebView and showing it while offline?
我需要以某种方式缓存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
错误
添加这些权限以显示
<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();}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.