Created
August 27, 2014 09:29
-
-
Save goodzsq/b006531cf41236ae8235 to your computer and use it in GitHub Desktop.
android获取网络状态
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
public static int isNetworkAvailable(){ | |
ConnectivityManager mConnMgr = (ConnectivityManager) appActivity.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo mWifi = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); | |
NetworkInfo mMobile = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); | |
if(mWifi != null && mWifi.isAvailable() && mWifi.isConnected()){ | |
return 1; | |
}else if(mMobile != null && mMobile.isAvailable() && mMobile.isConnected()){ | |
return 2; | |
}else{ | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment