Android > ³×Æ®¿öÅ© ¿¬°á »óŸ¦ È®ÀÎÇÏ´Â ConnectivityManager
µî·ÏÀÏ : 2017-07-06 17:31
Á¶È¸¼ö : 52,879
¾îÇà »ó¿¡¼ ¿ÍÀÌÆÄÀÌ ¶Ç´Â µ¥ÀÌÅÍ ³×Æ®¿öÅ©(3G,4G)°¡ ¿¬°áµÇ¾î ÀÖ´ÂÁö¸¦ È®ÀÎÇØ¾ß ÇÒ ¶§°¡ ÀÖ½À´Ï´Ù.
ÀÌ ¶§ »ç¿ëÇÏ´Â ¸Þ¼Òµå°¡ ¹Ù·Î ConnectivityManagerÀÔ´Ï´Ù.
±× Àü¿¡ ÀÏ´Ü ¸ÕÀü AndroidManifest.xml¿¡¼ Æ۹̼ÇÀ» Á¤ÀÇÇØÁÖ¾î¾ß ÇÕ´Ï´Ù..
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
±× ´ÙÀ½ ÀÚ¹Ù Äڵ忡¼ üũ¸¦ ÇÏ´Â Äڵ带 ¾Æ·¡¿Í °°ÀÌ ÀÛ¼ºÇÕ´Ï´Ù.
¾îµð¼µç »ç¿ëÀÌ °¡´ÉÇÏÁö¸¸ ÁÖ·Î onCreate() ³»¿¡ ÀÛ¼ºÀ» Çؼ ¾îÇÃÀÌ ½ÃÀÛÇÏ´Â ¼ø°£¿¡ È®ÀÎÀ» Çϵµ·Ï ÇÕ´Ï´Ù.
ConnectivityManager manager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
// wifi ¶Ç´Â ¸ð¹ÙÀÏ ³×Æ®¿öÅ© ¾î´À Çϳª¶óµµ ¿¬°áÀÌ µÇ¾îÀÖ´Ù¸é,
if (wifi.isConnected() || mobile.isConnected()) {
Log.i("¿¬°áµÊ" , "¿¬°áÀÌ µÇ¾ú½À´Ï´Ù.);
setContentView(R.layout.activity_logo);
} else {
Log.i("¿¬°á ¾È µÊ" , "¿¬°áÀÌ ´Ù½Ã Çѹø È®ÀÎÇØÁÖ¼¼¿ä);
}
ÀÌ·± ½ÄÀ¸·Î »ç¿ëÇؼ ³×Æ®¿öÅ© ¿¬°áÀ» ¼³Á¤ÇÏ¸é µË´Ï´Ù.!!
Á¦°¡ »ç¿ëÇÏ´Â ¿¹Á¦ ÀÔ´Ï´Ù.
¿¬°áÀ» È®ÀÎÇØ¾ß ÇÒ¶§ Fucntion À» »ç¿ëÇؼ üũ ÇÕ´Ï´Ù.
private Boolean isNetWork(){
ConnectivityManager manager = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
boolean isMobileAvailable = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isAvailable();
boolean isMobileConnect = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
boolean isWifiAvailable = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isAvailable();
boolean isWifiConnect = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
if ((isWifiAvailable && isWifiConnect) || (isMobileAvailable && isMobileConnect)){
return true;
}else{
return false;
}
}
isConnected ÀÇ true/ false °ªÀ» ¹ÙÅÁÀ¸·Î ÀÎÅÍ³Ý ¿¬°á È®ÀÎÀÌ °¡´ÉÇÕ´Ï´Ù.