Android判断设备是否打开WIFI、GPRS数据连接

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//WIFI
boolean isWifiConnected = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ? true : false;
//GPRS
boolean isGprsConnected = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ? true : false;

if(isGprsConnected){

}else{
    Toast.makeText(HomeActivity.this, "请打开GPRS定位", Toast.LENGTH_LONG).show();
    return;
}

你可能感兴趣的:(安卓)