
نوشته شده توسط
darkenerboy
متد چک کردن وضعیت اینترنت
public static boolean isConnectingToInternet(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERV ICE);
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;
}
دسترسی مورد نیاز
<uses-permission android:name="android.permission.ACCESS_NETWORK_ST ATE" />
این کد اینترنت رو چک نمیکنه. فقط ارتباط با شبکه مثل وای فایل و 3g اینا رو چک میکنه یعنی ابزارهای موجود برای ارتباط با اینترنت. از این کد استفاده کنید
/**
* Return True if host name address is reachable in <b>timeoutMill</b> mili seceond else False
*/
public static boolean isInternetAvailable(int timeoutMill) {
try {
if (InetAddress.getByName((String) UC.getHostName()).isReachable(timeoutMill)) {
return true;
}
return false;
}
catch (Exception e) {
return false;
}
}