/*添加权限*/<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>/*在需要判断网络的地方添加和调用以下方法*/
public boolean isNetworkAvilable(Context context){ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if(connectivityManager == null){ return false; }else{ NetworkInfo networkInfo =connectivityManager.getActiveNetworkInfo(); if(networkInfo != null ){ /*判断网络是否已连接*/ if(networkInfo.getState() == NetworkInfo.State.CONNECTED) { /*判断是否为wifi连接*/ if(networkInfo.getType() == connectivityManager.TYPE_WIFI){ Toast.makeText(getApplicationContext(),"wifi",Toast.LENGTH_SHORT).show(); } /*判断是否为数据连接*/ else if(networkInfo.getType() == connectivityManager.TYPE_MOBILE){ Toast.makeText(getApplicationContext(),"gprs",Toast.LENGTH_SHORT).show(); } return true; } } } return false; }
如果是数据连网时可通过 TelephonyManager来取得数据是3G/4G,联通或者移动电信等信息
/*取得手机的供应商等服务信息*/ TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
获取手机制式:int getPhoneType()
int PHONE_TYPE_CDMA 手机制式为CDMA,电信
int
PHONE_TYPE_GSM 手机制式为GSM,移动和联通
int PHONE_TYPE_NONE 手机制式未知
时间: 2024-10-06 00:29:03