网络判断

packagetext.bawei.com.internet;

importandroid.content.Context;

importandroid.net.ConnectivityManager;

importandroid.net.NetworkInfo;

importandroid.os.Bundle;

importandroid.support.v7.app.AppCompatActivity;

importandroid.telephony.TelephonyManager;

importandroid.widget.TextView;

publicclassMainActivityextendsAppCompatActivity {

privateTextViewbo1;

privateTextViewbo2;

privateTextViewbo3;

privateTextViewbo4;

privateTextViewbo5;

privateTextViewbo6;

privateTextViewbo7;

privateTextViewbo8;

privateTextViewbo9;

@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

//判断是否有网络连接

booleannetworkConnected = isNetworkConnected(MainActivity.this);

bo1.setText("判断是否有网络连接networkConnected:  "+ networkConnected);

//判断WIFI网络是否可用

booleanwifiConnected = isWifiConnected(MainActivity.this);

bo2.setText("判断WIFI网络是否可用wifiConnected:  "+ wifiConnected);

//判断MOBILE网络是否可用

booleanmobileConnected = isMobileConnected(MainActivity.this);

bo3.setText("判断MOBILE网络是否可用mobileConnected:  "+ mobileConnected);

//-----------------------------------------------

//获取当前网络连接的类型信息

//获取当前的网络状态-1:没有网络1:WIFI网络2:wap网络3:net网络

intconnectedType =getConnectedType(MainActivity.this);

bo4.setText("获取当前网络连接的类型信息connectedType:  "+ connectedType);

ConnectivityManager connMgr = (ConnectivityManager) MainActivity.this

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

intsubtype = networkInfo.getSubtype();

String subtypeName = networkInfo.getSubtypeName();

bo5.setText("网络的类型"+ subtype +"    "+"网络的名字"+ subtypeName);

intnetworkState = IntenetUtil.getNetworkState(MainActivity.this);

switch(networkState) {

case0:

bo6.setText("当前网络类型networkState:  "+"没有网络连接");

break;

case1:

bo6.setText("当前网络类型networkState:  "+"wifi连接");

break;

case2:

bo6.setText("当前网络类型networkState:  "+"2G");

break;

case3:

bo6.setText("当前网络类型networkState:  "+"3G");

break;

case4:

bo6.setText("当前网络类型networkState:  "+"4G");

break;

case5:

bo6.setText("当前网络类型networkState:  "+"NETWORN_MOBILE");

break;

}

}

//判断是否有网络连接

publicbooleanisNetworkConnected(Context context) {

if(context !=null) {

ConnectivityManager mConnectivityManager = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();

if(mNetworkInfo !=null) {

returnmNetworkInfo.isAvailable();

}

}

returnfalse;

}

//判断WIFI网络是否可用

publicbooleanisWifiConnected(Context context) {

if(context !=null) {

ConnectivityManager mConnectivityManager = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo mWiFiNetworkInfo = mConnectivityManager

.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if(mWiFiNetworkInfo !=null) {

returnmWiFiNetworkInfo.isAvailable();

}

}

returnfalse;

}

//判断MOBILE网络是否可用

publicbooleanisMobileConnected(Context context) {

if(context !=null) {

ConnectivityManager mConnectivityManager = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo mMobileNetworkInfo = mConnectivityManager

.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if(mMobileNetworkInfo !=null) {

returnmMobileNetworkInfo.isAvailable();

}

}

returnfalse;

}

//获取当前网络连接的类型信息

publicstaticintgetConnectedType(Context context) {

if(context !=null) {

ConnectivityManager mConnectivityManager = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();

if(mNetworkInfo !=null&& mNetworkInfo.isAvailable()) {

returnmNetworkInfo.getType();

}

}

return-1;

}

privatevoidinitView() {

bo1= (TextView) findViewById(R.id.bo1);

bo2= (TextView) findViewById(R.id.bo2);

bo3= (TextView) findViewById(R.id.bo3);

bo4= (TextView) findViewById(R.id.bo4);

bo5= (TextView) findViewById(R.id.bo5);

bo6= (TextView) findViewById(R.id.bo6);

bo7= (TextView) findViewById(R.id.bo7);

bo8= (TextView) findViewById(R.id.bo8);

bo9= (TextView) findViewById(R.id.bo9);

}

publicstaticclassIntenetUtil {

//没有网络连接

publicstaticfinalintNETWORN_NONE=0;

//wifi连接

publicstaticfinalintNETWORN_WIFI=1;

//手机网络数据连接类型

publicstaticfinalintNETWORN_2G=2;

publicstaticfinalintNETWORN_3G=3;

publicstaticfinalintNETWORN_4G=4;

publicstaticfinalintNETWORN_MOBILE=5;

/**

*获取当前网络连接类型

*

*@paramcontext

*@return

*/

publicstaticintgetNetworkState(Context context) {

//获取系统的网络服务

ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

//如果当前没有网络

if(null== connManager)

returnNETWORN_NONE;

//获取当前网络类型,如果为空,返回无网络

NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();

if(activeNetInfo ==null|| !activeNetInfo.isAvailable()) {

returnNETWORN_NONE;

}

//判断是不是连接的是不是wifi

NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if(null!= wifiInfo) {

NetworkInfo.State state = wifiInfo.getState();

if(null!= state)

if(state == NetworkInfo.State.CONNECTED|| state == NetworkInfo.State.CONNECTING) {

returnNETWORN_WIFI;

}

}

//如果不是wifi,则判断当前连接的是运营商的哪种网络2g、3g、4g等

NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if(null!= networkInfo) {

NetworkInfo.State state = networkInfo.getState();

String strSubTypeName = networkInfo.getSubtypeName();

if(null!= state)

if(state == NetworkInfo.State.CONNECTED|| state == NetworkInfo.State.CONNECTING) {

switch(activeNetInfo.getSubtype()) {

//如果是2g类型

caseTelephonyManager.NETWORK_TYPE_GPRS://联通2g

caseTelephonyManager.NETWORK_TYPE_CDMA://电信2g

caseTelephonyManager.NETWORK_TYPE_EDGE://移动2g

caseTelephonyManager.NETWORK_TYPE_1xRTT:

caseTelephonyManager.NETWORK_TYPE_IDEN:

returnNETWORN_2G;

//如果是3g类型

caseTelephonyManager.NETWORK_TYPE_EVDO_A://电信3g

caseTelephonyManager.NETWORK_TYPE_UMTS:

caseTelephonyManager.NETWORK_TYPE_EVDO_0:

caseTelephonyManager.NETWORK_TYPE_HSDPA:

caseTelephonyManager.NETWORK_TYPE_HSUPA:

caseTelephonyManager.NETWORK_TYPE_HSPA:

caseTelephonyManager.NETWORK_TYPE_EVDO_B:

caseTelephonyManager.NETWORK_TYPE_EHRPD:

caseTelephonyManager.NETWORK_TYPE_HSPAP:

returnNETWORN_3G;

//如果是4g类型

caseTelephonyManager.NETWORK_TYPE_LTE:

returnNETWORN_4G;

default:

//中国移动 联通 电信 三种3G制式

if(strSubTypeName.equalsIgnoreCase("TD-SCDMA") || strSubTypeName.equalsIgnoreCase("WCDMA") || strSubTypeName.equalsIgnoreCase("CDMA2000")) {

returnNETWORN_3G;

}else{

returnNETWORN_MOBILE;

}

}

}

}

returnNETWORN_NONE;

}

}

}

你可能感兴趣的:(网络判断)