【问题标题】:How to check if mobile network is connected to the internet or not in android如何在android中检查移动网络是否连接到互联网
【发布时间】:2014-04-01 12:35:03
【问题描述】:

如何检查手机的数据包数据是否连接。因为当我在我的 android 应用程序上启用移动数据选项时,即使我的 simcard 中没有任何负载,它也会始终连接。我的问题是如何验证我的 android 应用程序中是否有互联网连接。

顺便说一句,这是我的代码。

    ConnectivityManager cm = (ConnectivityManager)this.GetSystemService(Context.ConnectivityService);



    NetworkInfo nf = cm.ActiveNetworkInfo;
    if (nf != null && nf.IsConnected == true)
    {
//connected
    }
    else
    {
//not connected
    }

但是在这段代码中。即使我没有负载。我的应用程序显示我仍然连接。请帮忙..

【问题讨论】:

  • 为什么要添加 C# 标签??
  • 在这种情况下,“已连接”并不意味着它正在主动发送或接收数据 - 它只是意味着有可能发送/接收数据。
  • 连接到什么?如果您的手机已连接到 Wifi 路由器,则可能仍然没有互联网连接。如果 Wifi 路由器连接到 ISP,则可能仍然没有向前连接。如果您想知道是否可以访问特定站点,请向其发送请求。

标签: c# android xamarin.android


【解决方案1】:

试试这个:

boolean internetCheck;
internetCheck = isInternetAvailable(this);
if (internetCheck) {
                //Internet available
            } else {
                //No Internet available         }  
    /**
         * 
         * Method to check internet connection is available
         */

        public static boolean isInternetAvailable(Context context) {
            boolean haveConnectedWifi = false;
            boolean haveConnectedMobile = false;
            boolean connectionavailable = false;

            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo[] netInfo = cm.getAllNetworkInfo();

            NetworkInfo informationabtnet = cm.getActiveNetworkInfo();
            for (NetworkInfo ni : netInfo) {
                try {

                    if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                        if (ni.isConnected())
                            haveConnectedWifi = true;
                    if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                        if (ni.isConnected())
                            haveConnectedMobile = true;
                    if (informationabtnet.isAvailable()
                            && informationabtnet.isConnected())
                        connectionavailable = true;

                } catch (Exception e) {
                    // TODO: handle exception
                    System.out.println("Inside utils catch clause , exception is"
                            + e.toString());
                    e.printStackTrace();

                }
            }
            return haveConnectedWifi || haveConnectedMobile;
        }

【讨论】:

    【解决方案2】:

    试试这个

    //call web service
        ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
        boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
        boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
        //check Internet connection
        if(internet||wifi)
        {
            Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); 
        }else{
                        Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show(); 
                }       
    

    同时为您的 Manifest 文件添加以下权限

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    【讨论】:

      【解决方案3】:
      Try this one.        
              ConnectivityManager connectivityManager = (ConnectivityManager) context
                      .getSystemService(Context.CONNECTIVITY_SERVICE);
              NetworkInfo activeNetworkInfo = connectivityManager
                      .getActiveNetworkInfo();
              boolean isActive=activeNetworkInfo != null && activeNetworkInfo.isConnected();
                     if(isActive)
                       {
                     System.out.print("Connection Establied");
                        }
                      else{
                          System.out.print("Connection not Establied");
                           }
      

      【讨论】:

        【解决方案4】:

        试试这个

        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(context);
        
           boolean  isInternetPresent = cd.isConnectingToInternet();
        

        如果存在互联网,它将返回 true

        【讨论】:

          【解决方案5】:

          我认为在您的情况下,此链接可能会对您有所帮助

          How can I receive a notification when the device loses network connectivity?

          检查互联网连接的最佳方法是发送请求并查看您是否收到任何回复。

          【讨论】:

            猜你喜欢
            • 2017-05-23
            • 1970-01-01
            • 2022-12-01
            • 1970-01-01
            • 2015-05-25
            • 2019-09-03
            • 2013-02-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多