Skip to content

Instantly share code, notes, and snippets.

@goodzsq
Created August 27, 2014 09:29
Show Gist options
  • Save goodzsq/b006531cf41236ae8235 to your computer and use it in GitHub Desktop.
Save goodzsq/b006531cf41236ae8235 to your computer and use it in GitHub Desktop.
android获取网络状态
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