首先是JAVA层:
直接使用Android自带的TelephonyManager 和 ConnectivityManager 都可以获取到 NetworksInfo 对象,可以通过 getType() 获取类型,判断是 “WIFI”还是 “MOBILE”,如果是MOBILE,可以通过 NetworksInfo 对象的 getSubType() 和 getSubTypeName() 可以获取到对于的网络类型和名字。
通过简单地查看Android API即可调用。安卓SDK19暂无对4G网络的强度判断方法。
先声明一些常量:
//网络状态强度
public static final int NETLEVEL_STRENGTH_NONE_OR_UNKNOWN = 0;
public static final int NETLEVEL_STRENGTH_POOR = 1;
public static final int NETLEVEL_STRENGTH_MODERATE = 2;
public static final int NETLEVEL_STRENGTH_GOOD = 3;
public static final int NETLEVEL_STRENGTH_GREAT = 4;
//定义数组用于返回
static int[] netInfo = new int[2];
//信号监听
TelephonyManager Tel;
MyPhoneStateListener MyListener;
public static int singnalLevel;
//Wifi监听
private static WifiInfo wifiInfo = null;
private static WifiManager wifiManager = null;
public static int wifiLevel;
在onCreate()里添加注册监听:
//添加信号强度监听
MyListener = new MyPhoneStateListener();
Tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
//添加wifi强度监听
wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
}