【问题标题】:android: how to know internet connection is available or not in device?android:如何知道设备中的互联网连接是否可用?
【发布时间】:2013-09-18 16:44:46
【问题描述】:

我想检查设备中的 Internet 连接是否可用,所以我怎么才能得到这个..请给我这个的代码..

非常感谢提前

【问题讨论】:

    标签: android


    【解决方案1】:

    嘿伙计...应用此代码..它可能对你有帮助。提前谢谢....

    布尔连接;

        private boolean checkInternetConnection()
        {
    
            ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
    
            // ARE WE CONNECTED TO THE NET
    
            if (conMgr.getActiveNetworkInfo() != null
    
                    && conMgr.getActiveNetworkInfo().isAvailable()
    
                    && conMgr.getActiveNetworkInfo().isConnected()) 
            {
    
            return true;
    
            }
            else 
            {
            return false;
    
            }
        }
    

    【讨论】:

      【解决方案2】:
              ConnectivityManager connectionService =
                  (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
              if (connectionService.getActiveNetworkInfo().isConnectedOrConnecting()) {
                  // Device is online
              }
      

      【讨论】:

        【解决方案3】:

        使用以下功能检查互联网连接:

        public boolean isOnline()
            {
        
                 ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                 NetworkInfo ni = cm.getActiveNetworkInfo();
                 boolean result = false;
                 if(ni != null )
                 {
                     if(  ni.getState() == NetworkInfo.State.CONNECTED )
                     {
                         result = true;
                     }
                 }
        
                 return result;
        
                } 
        

        检查 isOnline() 返回什么。如果 true 则 Internet 已连接,否则 Inernet 未连接。 希望这会对你有所帮助..:)

        【讨论】:

          【解决方案4】:
          // Network is connected or not 
          public boolean isConnected()
          {
              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)
                            {
                                Log.d("LOG","Network is Available");
                                return true;
                            }
          
                }
                return false;
          }
          

          【讨论】:

            【解决方案5】:

            使用这个答案很有帮助。

            public boolean isOnline() {
              ConnectivityManager cm =(ConnectivityManager)      
              getSystemService(Context.CONNECTIVITY_SERVICE);
              NetworkInfo netInfo = cm.getActiveNetworkInfo();
              if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                 return true;
              }
              return false;
            }
            
            if(temp==true){
                genHelper.showToast("Net Connection");              
            }else{              
                genHelper.showToast(" Not  Connected");
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2023-03-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多