/**
* 打开网络设置界面
*/
public static void openSetting (Activity activity ) {
Intent intent = new Intent ("/" );
ComponentName cm = new ComponentName ("com.android.settings" ,
"com.android.settings.WirelessSettings" );
intent .setComponent (cm );
intent .setAction ("android.intent.action.VIEW" );
activity .startActivityForResult (intent , 0 );
}
/**
* 判断是否网络连接
*/
public static boolean isOnline (Context context ) {
ConnectivityManager manager = (ConnectivityManager ) context
.getSystemService (Activity .CONNECTIVITY_SERVICE );
NetworkInfo info = manager .getActiveNetworkInfo ();
if (info != null && info .isConnected ()) {
return true ;
}
return false ;
}
/**
* 判断wifi是否连接状态
*/
public static boolean isWifi (Context context ) {
ConnectivityManager cm = (ConnectivityManager ) context
.getSystemService (Context .CONNECTIVITY_SERVICE );
return cm != null && cm .getActiveNetworkInfo ().getType () == ConnectivityManager .TYPE_WIFI ;
}
获取移动网络运营商名称,如中国联通、中国移动、中国电信
/**
* 获取移动网络运营商名称,如中国联通、中国移动、中国电信
*/
public static String getNetworkOperatorName (Context context ) {
TelephonyManager telephonyManager = (TelephonyManager ) context
.getSystemService (Context .TELEPHONY_SERVICE );
return telephonyManager .getNetworkOperatorName ();
}
// PHONE_TYPE_NONE :0 手机制式未知
// PHONE_TYPE_GSM :1 手机制式为GSM,移动和联通
// PHONE_TYPE_CDMA :2 手机制式为CDMA,电信
// PHONE_TYPE_SIP:3
/**
* 返回移动终端类型
*/
public static int getPhoneType (Context context ) {
TelephonyManager telephonyManager = (TelephonyManager ) context
.getSystemService (Context .TELEPHONY_SERVICE );
return telephonyManager .getPhoneType ();
}
// 联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EGDE,电信的2G为CDMA,电信的3G为EVDO
public class Constants {
/**
* Unknown network class
*/
public static final int NETWORK_CLASS_UNKNOWN = 0 ;
/**
* wifi net work
*/
public static final int NETWORK_WIFI = 1 ;
/**
* "2G" networks
*/
public static final int NETWORK_CLASS_2_G = 2 ;
/**
* "3G" networks
*/
public static final int NETWORK_CLASS_3_G = 3 ;
/**
* "4G" networks
*/
public static final int NETWORK_CLASS_4_G = 4 ;
}
/**
* 判断手机连接的网络类型(2G,3G,4G)
*/
public static int getNetWorkClass (Context context ) {
TelephonyManager telephonyManager = (TelephonyManager ) context
.getSystemService (Context .TELEPHONY_SERVICE );
switch (telephonyManager .getNetworkType ()) {
case TelephonyManager .NETWORK_TYPE_GPRS :
case TelephonyManager .NETWORK_TYPE_EDGE :
case TelephonyManager .NETWORK_TYPE_CDMA :
case TelephonyManager .NETWORK_TYPE_1xRTT :
case TelephonyManager .NETWORK_TYPE_IDEN :
return Constants .NETWORK_CLASS_2_G ;
case TelephonyManager .NETWORK_TYPE_UMTS :
case TelephonyManager .NETWORK_TYPE_EVDO_0 :
case TelephonyManager .NETWORK_TYPE_EVDO_A :
case TelephonyManager .NETWORK_TYPE_HSDPA :
case TelephonyManager .NETWORK_TYPE_HSUPA :
case TelephonyManager .NETWORK_TYPE_HSPA :
case TelephonyManager .NETWORK_TYPE_EVDO_B :
case TelephonyManager .NETWORK_TYPE_EHRPD :
case TelephonyManager .NETWORK_TYPE_HSPAP :
return Constants .NETWORK_CLASS_3_G ;
case TelephonyManager .NETWORK_TYPE_LTE :
return Constants .NETWORK_CLASS_4_G ;
default :
return Constants .NETWORK_CLASS_UNKNOWN ;
}
}
判断当前手机的网络类型(WIFI还是2,3,4G)
/**
* 判断当前手机的网络类型(WIFI还是2,3,4G),需要用到上面的方法
*/
public static int getNetWorkStatus (Context context ) {
int netWorkType = Constants .NETWORK_CLASS_UNKNOWN ;
ConnectivityManager connectivityManager = (ConnectivityManager ) context
.getSystemService (Context .CONNECTIVITY_SERVICE );
NetworkInfo networkInfo = connectivityManager .getActiveNetworkInfo ();
if (networkInfo != null && networkInfo .isConnected ()) {
int type = networkInfo .getType ();
if (type == ConnectivityManager .TYPE_WIFI ) {
netWorkType = Constants .NETWORK_WIFI ;
} else if (type == ConnectivityManager .TYPE_MOBILE ) {
netWorkType = getNetWorkClass (context );
}
}
return netWorkType ;
}