4

当 webview 中无法访问互联网并隐藏 URL 时,如何在 Android 应用程序中显示消息?如果互联网连接不可用,我不想显示 URL 和进度条。

这是我的代码。请给我正确的代码。我是安卓的初学者。

public class WebViewClientDemoActivity extends Activity {
    /** Called when the activity is first created. */

    WebView web;
    ProgressBar progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        web = (WebView) findViewById(R.id.webview01);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://www.vijaytutorials.com/mo");
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
4

3 回答 3

4

我有同样的问题。我刚刚解决了。我所做的是,如果设备处于脱机状态,则自定义 html 会显示一条消息,显示它无法连接到互联网的原因。将其复制粘贴到您的 webview 活动中。

最终活动活动=这个;

mainWebView.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                 String summary =
                 "<html><body>Could not connect to the server.</body></html>";
                 mainWebView.loadData(summary, "text/html", null);

                Toast.makeText(activity, "" + description, Toast.LENGTH_SHORT)
                        .show();
            }
        });

        webview.loadUrl("http://www.tipfortechs.com/");

然后使用以下代码检查设备是否连接到互联网,在同一活动中添加以下代码。

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }

最后不要忘记在您的清单中使用权限。

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

希望能帮助到你。您的代码已编辑:

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

/*
 * Demo of creating an application to open any URL inside the application and clicking on any link from that URl 
should not open Native browser but  that URL should open in the same screen.

- Load WebView with progress bar
 */
public class WebViewClientDemoActivity extends Activity {
    /** Called when the activity is first created. */

    WebView web;
    ProgressBar progressBar;
    final Activity activity = this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        web = (WebView) findViewById(R.id.webview01);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);



        web.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                 String summary =
                 "<html><body>Could not connect to the server.</body></html>";
                 web.loadData(summary, "text/html", null);

                Toast.makeText(activity, "" + description, Toast.LENGTH_SHORT)
                        .show();
            }
        });


        web.loadUrl("http://www.vijaytutorials.com/mo");
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        return activeNetworkInfo != null;
    }
}
于 2013-10-25T10:56:59.690 回答
0

您可以使用以下代码检查无线连接状态。

ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (mWifi.isConnected()) {
// hide url or whatever you want to do
}

不要忘记在 AndroidManifest.xml 中添加权限:

android.permission.ACCESS_NETWORK_STATE

干杯,

于 2013-07-26T14:30:48.487 回答
0

您可以覆盖 WebView 的错误处理程序并显示 Toast 或显示您创建的图像,告诉用户存在网络错误:

@Override
public void onReceivedError(WebView view, int errorCode, String description, String url)
{
    view.loadUrl("file:///[your error image or page goes here]");
}
于 2013-07-26T14:26:42.250 回答