Android判断网络状态

上代码:ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {

Toast.makeText(Healthier.this, “WIFI已经连接”, Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(Healthier.this, “WIFI未连接”, Toast.LENGTH_SHORT).show();

}

if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED) {

Toast.makeText(Healthier.this, “GPRS已经连接”, Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(Healthier.this, “GPRS未连接”, Toast.LENGTH_SHORT).show();

}