2017-04-10 15 views
2

나는이 인터넷이지만, 어떤 경우에는 내가 WIFI하지만 인터넷에 연결할 수 있어요 경우가 완벽하게 작동안드로이드 인터넷 연결 시간 제한

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

를 사용하여 인터넷 연결을 감지 할 수 있어요. 이 경우 인터넷을 사용할 수없는 경우 10 초 후에 사용자에게 [No 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 초 동안 당신은 당신이 인터넷 연결을 확인하고 어떤 인터넷이없는 경우에 토스트 보여 therefafter 다음 시도 할 수 10000

+0

덕분에 내가 이것을 시도하자 : 추가 정보를 위해 서버도 ^^

당신이 대답을 확인할 수 있습니다. – Andi

+0

@ChandraMouliPoreddy 확실한 친구, 그리고 그때 일하는 경우에 받아들이고 응답을 평가하는 것을 잊지 말라. :) –

0

을 추가 할 수 있습니다

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

당신이 옳다을, 당신을 wifi 또는 Cellular 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