【问题标题】:check internet connection on android (eclipse)检查 android (eclipse) 上的互联网连接
【发布时间】:2014-06-18 09:51:33
【问题描述】:

我需要这个算法在 android Eclipse 编程中的代码:

If internet connection = connect then open program 
else show error_activity.xml

【问题讨论】:

标签: java android eclipse


【解决方案1】:

创建一个类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;
}

// 现在无论你想在哪里检查互联网,只需创建一个此类的对象并调用它的方法,如下所示:

// 在你的活动中定义全局变量:

Boolean _isInternetPresent = false;
ConnectionDetector _cd;

//在创建时

    _cd = new ConnectionDetector(getApplicationContext());
    _isInternetPresent = _cd.isConnectingToInternet();

然后像这样检查:

         if (_isInternetPresent) {
            // do you work here
            } else {
                    // no internet,please try again.

           }

因为你是一个新手,我给你完整的代码。请试着理解每一步,这个代码是如何工作的。

【讨论】:

    【解决方案2】:

    试试这个检查互联网连接..

    public void checkNetwork() {
            ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    
            if (networkInfo != null && networkInfo.isConnected()) {
                //true : your code
            } else {
                //false : your code
                Log.v("Network Error", "No network connection available.");
            }
        }
    

    【讨论】:

      【解决方案3】:

      创建检测连接的类非常简单。

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

      }

      并检查设备是否连接到互联网。

      在使用之前初始化 cd。 isInternetPresent = cd.isConnectingToInternet();

                  // check for Internet status
              if (isInternetPresent) {
                  // Internet Connection is Present
                  // make HTTP requests
                  showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
                          "You have internet connection", true);
              } else {
                  // Internet connection is not present
                  // Ask user to connect to Internet
                  showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection",
                          "You don't have internet connection.", false);
              }
      

      【讨论】:

        【解决方案4】:

        步骤 1. 创建 ConnectionDetector.java

        package com.YOURPACKAGE.YOURAPP;
        
        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 checkInternetConn() {
            ConnectivityManager connectivity = (ConnectivityManager) _context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {
                NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        
                if(info == null){
                    connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                }
        
                if(info == null){
                    connectivity.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
                }
        
                if (info != null) {
                    if (info.isConnected()) {
                        return true;
                    }
                }
            }
            return false;
        }
        }
        

        第 2 步。在您的 Activity 中实施连接检测器

        public class YOURACTIVITY extends Activity {
        
            ConnectionDetector cd;
        
            private void methodThatNeedsTheInternet() {
        
                Boolean isConnectionExist = false;
                isConnectionExist = cd.checkInternetConn();
        
                // check for Internet status before proceeding
                if (!isConnectionExist) {
                    //no internet, show alert or something
                    return;
                }
        
                //internet works, perform method that needs it
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2012-02-21
          • 2012-06-12
          • 1970-01-01
          • 2016-08-28
          • 2016-01-16
          • 2017-11-30
          • 2013-02-02
          • 2011-02-14
          • 2012-03-23
          相关资源
          最近更新 更多