2012-07-16 77 views
0

如果手機上沒有數據連接,如何顯示我的自定義HTML代碼?Android Webview - 自定義HTML

自定義HTML代碼:

String customHtml = "<html><body>Hello, WebView</body></html>"; 
webView.loadData(customHtml, "text/html", "UTF-8"); 

這裏是我的全碼:

package com.webbrowser.android; 

import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class MainActivity extends Activity { 

private WebView webView; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 

    webView = (WebView) findViewById(R.id.webView1); 
    webView.setWebViewClient(new WebViewClient()); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl("http://domain.com"); 
} 

}

編輯:

這樣對嗎?它不適用於模擬器。 你能再看一遍嗎?

EDIT2:

加括號。

public class MainActivity extends Activity { 

    private WebView webView; 

    public boolean isConnected() { 
     ConnectivityManager cm = 
      (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

     return (cm.getActiveNetworkInfo() != null && 
      cm.getActiveNetworkInfo().isConnectedOrConnecting()); 
    } 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.webview); 

     webView = (WebView) findViewById(R.id.webView1); 
     webView.setWebViewClient(new WebViewClient()); 
     webView.getSettings().setJavaScriptEnabled(true); 

     if(!isConnected()) { 
      String customHtml = "<html><body>Hello, WebView</body></html>"; 
      webView.loadData(customHtml, "text/html", "UTF-8"); 
     } else { 
      webView.loadUrl("http://domain.com"); 
      } 

    } 
} 

回答

0

由於您加載特定HTML代碼(基本型)沒有數據連接它會加載...

+0

自定義HTML代碼,我不加入主要的代碼呢。 – user1273409 2012-07-16 16:06:45

1

首先檢查使用連接:

public boolean isConnected() { 
    ConnectivityManager cm = 
     (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

    return (cm.getActiveNetworkInfo() != null && 
     cm.getActiveNetworkInfo().isConnectedOrConnecting()) 
} 

然後視結果調用相應的方法:

if(!isConnected()) { 
    String customHtml = "<html><body>Hello, WebView</body></html>"; 
    webView.loadData(customHtml, "text/html", "UTF-8"); 
} else { 
    webView.loadUrl("http://domain.com"); 
} 
+0

請看我的編輯。 – user1273409 2012-07-16 16:47:31

+0

請檢查我編輯的代碼,你需要把這些行放在方括號(cm.getActiveNetworkInfo()!= null && cm.getActiveNetworkInfo()。isConnectedOrConnecting())中。 – Waqas 2012-07-17 04:14:54

+0

這是同樣的問題。已添加括號。 – user1273409 2012-07-17 13:31:25

0

請使用webviewclients函數onReceivedError。 這將工作方面的連接類型。

public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) { 
     // Load custom data into webview depending on errorcode 
    } 
0

嘿u能試試這個代碼,它的工作原理非常漂亮

public class SudhirAwesome extends Activity { 
WebView webView; 
String html = "<!DOCTYPE html>\n" + 
     "<html>\n" + 
     "<head>\n" + 
     "</head>\n" + 
     "<body>\n" + 
     "\n" + 
     "<h1>Can you please check your Internet Connection</h1>\n" + 
     "\n" + 
     "</body>\n" + 
     "</html>\n"; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sportee); 

    webView = (WebView) findViewById(R.id.webview_sportee); 
    webView.getSettings().setLoadsImagesAutomatically(true); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
    webView.setWebViewClient(new MyBrowser()); 
    if(haveNetworkConnection()){ 
     webView.loadUrl("http://blah.blah.com"); 
    } 
    else{ 
     webView.loadData(html,"text/html", "UTF-8"); 

     Toast.makeText(getApplicationContext(),"Please Check your Internet Connection",Toast.LENGTH_LONG).show(); 
    } 

    //This acts like browser only 
} 

private class MyBrowser extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     try { 
      view.loadUrl(url); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return true; 
    } 
} 
private boolean haveNetworkConnection() { 
    boolean haveConnectedWifi = false; 
    boolean haveConnectedMobile = false; 
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
    for (NetworkInfo ni : netInfo) { 
     if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
      if (ni.isConnected()) 
       haveConnectedWifi = true; 
     if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
      if (ni.isConnected()) 
       haveConnectedMobile = true; 
    } 
    return haveConnectedWifi || haveConnectedMobile; 
} 

和U必須給這些權限也

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