1

如果手机上没有数据连接,如何显示我的自定义 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");
}

}

编辑:

这是正确的吗?它在模拟器上不起作用。请您再看一遍好吗?

编辑2:

添加了括号。

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

    }
}
4

4 回答 4

2

首先使用以下命令检查连接性:

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");
}
于 2012-07-16T15:45:03.523 回答
0

由于您正在加载特定的 html 代码(基本),它将在没有数据连接的情况下加载...

于 2012-07-16T15:43:36.537 回答
0

请使用 webviewclients 函数 onReceivedError。这将适用于任何连接类型。

public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
        // Load custom data into webview depending on errorcode
    }
于 2013-11-07T09:38:26.623 回答
0

嘿,你可以试试这个代码,它工作得很好

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;
}

你也必须给予这些权限

 <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"/>
于 2015-10-30T09:57:12.153 回答