2011-08-22 55 views
3

我有這個應用程序,它使用webview在脫機模式下呈現緩存內容並顯示服務器的網頁連接到互聯網...在未連接到互聯網的情況下在Android Web視圖中加載緩存否則在連接到互聯網時顯示服務器的內容

代碼片段如下所示。它在我運行應用程序時崩潰。

我無法弄清楚哪裏出了問題。我還在清單文件中設置了使用權限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/"); 
      } 











    } 
} 

回答

1

也許NetworkInfo對象爲null,您將得到一個NullPointerException。找出代碼行的最好方法是使用logcat並查看堆棧跟蹤。

順便說一句,我用類似的方式在網頁視圖,但與我的設備(慾望+ CM7),這是行不通的,因爲從來沒有使用的高速緩存使用緩存..

隨着親切的問候, jayearn

1
webview1 = (WebView) findViewById(R.id.webView1); 

    WebSettings webSettings = webview1.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webSettings.setCacheMode(WebSettings.LOAD_NORMAL); <--- CHANGE THIS 
    webSettings.setAppCacheMaxSize(1024*1024*8); 
    webview1.addJavascriptInterface(new JavaScriptInterface(this), 
      "Android"); 
    webview1.setWebViewClient(new MyWebViewClient()); 
    webview1.setBackgroundColor(Color.BLACK); 


    try { 
     current_url = "http://........link"; 
     webview1.loadUrl(current_url); 
    } catch (Exception e) { 
     Log.v("Loading Webview Failed", "Loading the webview Failed"); 
    } 

    current_fact = 0; 
1

在AndroidManifest.xml中只需添加 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 。它會像魔術一樣工作。 LogCat幫助。

0

你還需要檢查NETWORKINFO爲空 所以基本上你的if語句應該是

if(cm.getActiveNetworkInfo() == null || !cm.getActiveNetworkInfo().isConnected()){ 
//load cache 
}else{ 
//load default 
} 

不要忘了添加這些權限

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

至於AppCachePath,如果你正在爲Android 2.2+建設,然後用此代替

engine.getSettings().setAppCachePath(getApplication().getCacheDir().toString()); 

這將保證緩存將在應用程序卸載時被刪除

相關問題