2017-04-10 161 views
2

我能夠檢測使用Android的互聯網連接超時

public class InternetDetector { 

    private Context _context; 

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

    /** 
    * Checking for all possible internet providers 
    **/ 
    public Boolean isConnectingToInternet() { 
     ConnectivityManager connectivityManager 
       = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
     return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting(); 
    } 
    public Boolean isOnline() { 
     try { 
      Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com"); 
      int returnVal = p1.waitFor(); 
      boolean reachable = (returnVal == 0); 
      return reachable; 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return false; 
    } 
} 

這完美的作品,如果有互聯網,但在某些情況下,我能夠連接到WIFI,但沒有互聯網連接互聯網。在這種情況下,如果互聯網不可用,我會在10秒後向用戶顯示一條消息[無INTERNET CHECK YOUR CONNECTION]

我該如何實現它。

回答

0

您可以通過時間完成的消息將顯示給用戶

new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     // This method will be executed once the timer is over 
     // Your message here 
    } 
}, 10000); //Your time here in miliseconds 

後使用處理器實現這一目標。簡單的是。

保持10秒,你可以添加10000

+0

感謝您的意見,讓我試試這個。 – Andi

+0

@ChandraMouliPoreddy肯定好友,如果它工作,那麼不要忘記接受並評價答案。 :) –

0

您可以嘗試以下操作來檢查網絡連接和therefafter顯示敬酒的情況下,沒有互聯網:

boolean isInternetAvailable = checkInternetConnection(this); 

    if(!isInternetAvailable) { 
     new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     Toast.makeText(this, "No INTERNET CHECK YOUR CONNECTION", Toast.LENGTH_SHORT).show(); 
    } 
}, 10000); 
    } 

    public static boolean checkInternetConnection(Context context) { 
     final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 

     NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo(); 

     if (activeNetworkInfo != null) { // connected to the internet 
      Toast.makeText(context, activeNetworkInfo.getTypeName(), Toast.LENGTH_SHORT).show(); 

      if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) { 
       // connected to wifi 
       return isWifiInternetAvailable(); 
      } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) { 
       // connected to the mobile provider's data plan 
       return true; 
      } 
     } 
     return false; 
    } 

public boolean isWifiInternetAvailable() { 
     try { 
      InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name 
      return !ipAddr.equals(""); 
     } catch (Exception e) { 
      return false; 
     } 
    } 
0

你是對的,你可以檢查您是否連接到wifiCellular Network,但您無法輕易檢查您是否真的連接到Internet

現在檢查您是否真正連接到互聯網的唯一方法是連接到遠程服務器!例如:

public static boolean hasInternetAccess(Context context) { 
    if (isNetworkAvailable(context)) { 
     try { 
      HttpURLConnection urlc = (HttpURLConnection) 
       (new URL("http://clients3.google.com/generate_204") 
       .openConnection()); 
      urlc.setRequestProperty("User-Agent", "Android"); 
      urlc.setRequestProperty("Connection", "close"); 
      urlc.setConnectTimeout(1500); 
      urlc.connect(); 
      return (urlc.getResponseCode() == 204 && 
         urlc.getContentLength() == 0); 
     } catch (IOException e) { 
      Log.e(TAG, "Error checking internet connection", e); 
     } 
    } else { 
     Log.d(TAG, "No network available!"); 
    } 
    return false; 
} 

您可以更改URL到任何需要的地址,但這個URL生成確定的響應,所以你不必擔心服務器的問題。當然,你可以把它改成你的服務器查詢服務器太^^

欲瞭解更多信息,你可以檢查這個答案: Detect if Android device has Internet connection