WIFI 연결 체크
private boolean checkWifiOnAndConnected() {
WifiManager wifiMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiMgr.isWifiEnabled()) { // Wi-Fi adapter is ON
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
if( wifiInfo.getNetworkId() == -1 ){
return false; // Not connected to an access point
}
return true; // Connected to an access point
}
else {
return false; // Wi-Fi adapter is OFF
}
}
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
출처 : https://stackoverflow.com/questions/3841317/how-do-i-see-if-wi-fi-is-connected-on-android
public static String getNetworkType(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
return info.getTypeName();
}
아래 함수는 약간만 손보면 사용하기 좋겠슴다.
public static boolean isInternetIsConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
return true;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
return true;
}
} else {
// not connected to the internet
return false;
}
return false;
}
출처 : https://stackoverflow.com/questions/48077755/how-to-check-programmatically-that-wifi-is-connected-or-data-pack-is-enabled-but
'Android' 카테고리의 다른 글
안드로이드 Calendar.getInstance API level24 (0) | 2018.07.10 |
---|---|
NFC NTAG 213 (1) | 2018.03.26 |
How to Change the Text Color of a Substring (0) | 2018.03.20 |