【问题标题】:Check whether connected to the internet or not before loading WebView? [duplicate]在加载 WebView 之前检查是否连接到互联网? [复制]
【发布时间】:2015-03-27 06:52:01
【问题描述】:

我在 android 中创建了一个 webview 应用程序,我需要实现条件,即如果互联网或 wifi 可用意味着它将继续打开 web 链接。如果互联网或 WIFI 不可用,则意味着它将加载到资产中存在的我的 HTML 页面中。我们怎么能做到呢?

包 com.example.webview;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;


public class Dadhboard<Bitmap> extends ActionBarActivity {

    WebView web;
    ProgressDialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_dadhboard);


        web = (WebView) findViewById(R.id.webview);
        web.setWebViewClient(new WebViewClient() {

            // This method will be triggered when the Page Started Loading

            public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
                dialog = ProgressDialog.show(Dadhboard.this, null,
                        "Please Wait...Page is Loading...");
                dialog.setCancelable(true);
                super.onPageStarted(view, url, favicon);
            }

            public void onPageFinished(WebView view, String url) 
            {
                            dialog.dismiss();
                            super.onPageFinished(view, url);
            }

                        // This method will be triggered when error page appear

            public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) 
            {
                            dialog.dismiss();
                            // You can redirect to your own page instead getting the default
                            // error page
                            Toast.makeText(Dadhboard.this,
                                    "The Requested Page Does Not Exist", Toast.LENGTH_LONG).show();
                            web.loadUrl("http://www.google.com/");
                            super.onReceivedError(view, errorCode, description, failingUrl);
            }
                    });

                    web.loadUrl("http://www.google.com/");
                    web.getSettings().setLoadWithOverviewMode(true);
                    web.getSettings().setUseWideViewPort(true);
                }

}

【问题讨论】:

  • 您可以使用 Timer 设置时间以检查 Internet,然后显示消息或一些图像。

标签: android


【解决方案1】:

试试这个

连接检测器

package com.likith.conectiondetector;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class connectiondetector 
{
    private Context _context;

/********************************************************************************/

public connectiondetector(Context context)
{
    this._context = context;
}

/********************************************************************************/

public boolean isConnectingToInternet()
{
    ConnectivityManager connectivity = (ConnectivityManager) 
            _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) 
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) 
              for (int i = 0; i < info.length; i++) 
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }
      }
      return false;
}
}

使用

connectiondetector cd= new connectiondetector(getApplicationContext());
isInternetPresent = cd.isConnectingToInternet();

if(isInternetPresent)
{
    //Internet is connected
}
else
{
    // Internet is not connected
}

【讨论】:

    【解决方案2】:

    在您的活动中创建此方法:

    public boolean isConnected(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnected(); // isConnectedOrConnecting()
        return isConnected;
    }
    
    public boolean isWifi(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
        return isWiFi;
    }
    

    获取是否连接状态:

        // check
        if (isConnected(this)) {
            if (isWifi(this)) {
                // connected to wifi
            }else{
                // connected to mobile network
            }
        }else{
            // no network available
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      相关资源
      最近更新 更多