雪里香梅,先报春来早。——(宋)欧阳修《蝶恋花》
如今的天气是越来略冷了,每每走在凛冽的寒风中,心里就一个想法,春(假)天(期)怎么还不到!不知道大家有没有同感。前两天要做一个获取手机信号的小程序,于是在网上搜索了很多,就找到两种方法,遗憾的是都没跑通。正当本人山重水复疑无路之时,看到了第三种方法,赶紧试了一下——噢耶\(^o^)/成功啦!于是把这三种方法都整理下来和大家分享一下!
首先,获取有关手机信号的信息是需要获取权限的,这里介绍动态申请权限的方法。列出所需权限:
private String[] permissions =
{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.CHANGE_NETWORK_STATE,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.READ_PHONE_STATE,Manifest.permission.ACCESS_NETWORK_STATE,Manifest.permission.MODIFY_PHONE_STATE};
申请权限:
for (int j = 0;j < permissions.length; j++){
int i = ContextCompat.checkSelfPermission(this, permissions[j]);
// 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
if (i != PackageManager.PERMISSION_GRANTED) {
// 如果没有授予该权限,就去提示用户请求
ActivityCompat.requestPermissions(this, permissions, 321);
}
}
第一种方法:
public void getCurrentNetDBM(Context context) {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener mylistener = new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
String signalInfo = signalStrength.toString();
String[] params = signalInfo.split(" ");
if (tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE) {
//4G网络 最佳范围 >-90dBm 越大越好
int Itedbm = Integer.parseInt(params[9]);
Log.e("66666", "onSignalStrengthsChanged: " + Itedbm + "");
} else if (tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA ||
tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSPA ||
tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSUPA ||
tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) {
//3G网络最佳范围 >-90dBm 越大越好 ps:中国移动3G获取不到 返回的无效dbm值是正数(85dbm)
//在这个范围的已经确定是3G,但不同运营商的3G有不同的获取方法,故在此需做判断 判断运营商与网络类型的工具类在最下方
String yys = NetUtils.getOperatorName(getApplication());//获取当前运营商
if (yys == "中国移动") {
Log.e("66666", "onSignalStrengthsChanged: " + 0 + "");//中国移动3G不可获取,故在此返回0
} else if (yys == "中国联通") {
int cdmaDbm = signalStrength.getCdmaDbm();
Log.e("66666", "onSignalStrengthsChanged: " + cdmaDbm + "");
} else if (yys == "中国电信") {
int evdoDbm = signalStrength.getEvdoDbm();
Log.e("66666", "onSignalStrengthsChanged: " + evdoDbm + "");
}
} else {
//2G网络最佳范围>-90dBm 越大越好
int asu = signalStrength.getGsmSignalStrength();
int dbm = -113 + 2 * asu;
Log.e("66666", "onSignalStrengthsChanged: " + dbm + "");
}
}
};
//开始监听
tm.listen(mylistener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
经实验,这种方法虽然可以触发onSignalStrengthsChanged()方法,但是获取的信号强度一直是-1(实验信号为北京联通4G),显然不符合实际。
第二种方法:
public void signal() {
List infos;
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
infos = mTelephonyManager.getNeighboringCellInfo();
StringBuffer sb = new StringBuffer("总数 : " + infos.size() + "\n");
for (NeighboringCellInfo info1 : infos) { // 根据邻区总数进行循环
sb.append(" LAC : " + info1.getLac()); // 取出当前邻区的LAC
sb.append(" CID : " + info1.getCid()); // 取出当前邻区的CID
sb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 获取邻区基站信号强度
}
Log.e("66666", " 获取邻区基站信息:" + sb.toString());
}
这种方法看起来是可行的,但在实际实验过程中getNeighboringCellInfo()方法的返回值一直为null,查阅相关资料有人说是Google API的问题,期待着修复吧。
第三种方法,也是我找到的唯一可行的办法:
public void getMobileDbm()
{
int dbm = -1;
TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
List cellInfoList = tm.getAllCellInfo();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
if (null != cellInfoList)
{
for (CellInfo cellInfo : cellInfoList)
{
if (cellInfo instanceof CellInfoGsm)
{
CellSignalStrengthGsm cellSignalStrengthGsm = ((CellInfoGsm)cellInfo).getCellSignalStrength();
dbm = cellSignalStrengthGsm.getDbm();
Log.e("66666", "cellSignalStrengthGsm" + cellSignalStrengthGsm.toString());
}
else if (cellInfo instanceof CellInfoCdma)
{
CellSignalStrengthCdma cellSignalStrengthCdma = ((CellInfoCdma)cellInfo).getCellSignalStrength();
dbm = cellSignalStrengthCdma.getDbm();
Log.e("66666", "cellSignalStrengthCdma" + cellSignalStrengthCdma.toString() );
}
else if (cellInfo instanceof CellInfoWcdma)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
{
CellSignalStrengthWcdma cellSignalStrengthWcdma = ((CellInfoWcdma)cellInfo).getCellSignalStrength();
dbm = cellSignalStrengthWcdma.getDbm();
Log.e("66666", "cellSignalStrengthWcdma" + cellSignalStrengthWcdma.toString() );
}
}
else if (cellInfo instanceof CellInfoLte)
{
CellSignalStrengthLte cellSignalStrengthLte = ((CellInfoLte)cellInfo).getCellSignalStrength();
dbm = cellSignalStrengthLte.getDbm();
Log.e("66666", "cellSignalStrengthLte.getAsuLevel()\t" + cellSignalStrengthLte.getAsuLevel() );
Log.e("66666", "cellSignalStrengthLte.getCqi()\t" + cellSignalStrengthLte.getCqi() );
Log.e("66666", "cellSignalStrengthLte.getDbm()\t " + cellSignalStrengthLte.getDbm() );
Log.e("66666", "cellSignalStrengthLte.getLevel()\t " + cellSignalStrengthLte.getLevel() );
Log.e("66666", "cellSignalStrengthLte.getRsrp()\t " + cellSignalStrengthLte.getRsrp() );
Log.e("66666", "cellSignalStrengthLte.getRsrq()\t " + cellSignalStrengthLte.getRsrq() );
Log.e("66666", "cellSignalStrengthLte.getRssnr()\t " + cellSignalStrengthLte.getRssnr() );
Log.e("66666", "cellSignalStrengthLte.getTimingAdvance()\t " + cellSignalStrengthLte.getTimingAdvance() );
}
}
}
}
}
采用北京联通4G信号进行实验,打印日志如下:
12-14 09:30:06.667 25776-25776/com.example.data.rssi E/66666: cellSignalStrengthLte.getAsuLevel() 46
12-14 09:30:06.667 25776-25776/com.example.data.rssi E/66666: cellSignalStrengthLte.getCqi() 2147483647
12-14 09:30:06.667 25776-25776/com.example.data.rssi E/66666: cellSignalStrengthLte.getDbm() -94
12-14 09:30:06.667 25776-25776/com.example.data.rssi E/66666: cellSignalStrengthLte.getLevel() 4
12-14 09:30:06.667 25776-25776/com.example.data.rssi E/66666: cellSignalStrengthLte.getRsrp() -94
12-14 09:30:06.667 25776-25776/com.example.data.rssi E/66666: cellSignalStrengthLte.getRsrq() -18
12-14 09:30:06.667 25776-25776/com.example.data.rssi E/66666: cellSignalStrengthLte.getRssnr() 2147483647
12-14 09:30:06.667 25776-25776/com.example.data.rssi E/66666: cellSignalStrengthLte.getTimingAdvance() 2147483647
获取手机信号强度功能——实现!