其他分享
首页 > 其他分享> > 未连接到互联网时在Android Webview中加载缓存,否则在连接到互联网时显示来自服务器的内容

未连接到互联网时在Android Webview中加载缓存,否则在连接到互联网时显示来自服务器的内容

作者:互联网

我有这个应用程序,它在脱机模式下使用webview呈现缓存的内容,并在连接到互联网时显示来自服务器的网页…

代码片段如下所示.我运行该应用程序时会崩溃.

我无法弄清楚出了什么问题.我还在清单文件中设置了uses-permission android:name =“ android.permission.INTERNET”.

package com.html5webappcache.android;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage.QuotaUpdater;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

public class HTML5WebAppCacheTestActivity extends Activity {
    final Activity activity = this;
    private ConnectivityManager cm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Adds Progrss bar Support
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main );

        getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 

        WebView engine=(WebView)findViewById(R.id.web_engine);
        engine.getSettings().setJavaScriptEnabled(true);
        engine.getSettings().setBuiltInZoomControls(true);
        engine.getSettings().setLoadsImagesAutomatically(true);
        engine.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        engine.setInitialScale(1);

        engine.setWebChromeClient(new WebChromeClient() {
              @Override
                 public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,QuotaUpdater quotaUpdater)
                 {
                       quotaUpdater.updateQuota(spaceNeeded * 2);
                 }
              public void onProgressChanged(WebView view, int progress)
              {
                  activity.setTitle("Loading...");
                  activity.setProgress(progress * 100);



                  if(progress == 100)
                  { activity.setTitle(R.string.app_name);

                  }
              }
           });
        engine.getSettings().setDomStorageEnabled(true);
       // Set cache size to 8 mb by default. should be more than enough
        engine.getSettings().setAppCacheMaxSize(1024*1024*8);
        engine.getSettings().setAppCachePath("/data/data/com.html5webappcache.android/cache");
        engine.getSettings().setAllowFileAccess(true);
        engine.getSettings().setAppCacheEnabled(true);
        engine.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

         cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
         if(cm.getActiveNetworkInfo().isConnected())
         {
         engine.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
         engine.loadUrl("http://www.bifter.co.uk/");
         }
             else{
                engine.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
                 engine.loadUrl("http://www.bifter.co.uk/");
             }











    }
}

解决方法:

也许NetworkInfo对象为null,您会收到NullPointerException.找出代码行的最佳方法是使用logcat并查看stacktrace.

顺便说一句,我使用类似的方法在WebViews中使用缓存,但是对于我的设备(Desire CM7),由于从未使用过缓存,因此无法使用.

亲切的问候,
杰耶恩

标签:caching,android-webview,android
来源: https://codeday.me/bug/20191208/2088344.html