那什么样收获CDMA大概GSM的手机时限信号呢,这上边就来探望大家所要求的CDMA与GSM时域信号是何许获得的呢

正文转自:http://software.intel.com/zh-cn/blogs/2011/12/16/android-gsmcdma/

 

正文转自:http://software.intel.com/zh-cn/blogs/2011/12/16/android-gsmcdma/

 

在Android中我们常用的轻巧获取WIFI连续信号列表,那怎么获得CDMA大概GSM的手提式无线电话机信号呢?
系统提供了TelephonyManager类,此类极其丰裕,基本你所须要的手提式有线电话机音信都能获得到,那下边就来看看大家所急需的CDMA与GSM实信号是怎么获取的吗。

private TelephonyManager telephonyManager;
private PhoneStateListener phoneStateListener;
先是注脚三个变量

在onCreate()方法中初阶化变量
InitPhoneStateListener();
telephonyManager = (TelephonyManager)
this.ctx.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CELL_LOCATION);
if(telephonyManager.getCellLocation()!=null){
//获取当前基站音讯
phoneStateListener.onCellLocationChanged(telephonyManager.getCellLocation());
}

/** 初始化PhoneStateListener */
private void InitPhoneStateListener() {
phoneStateListener = new PhoneStateListener() {
@Override
public void onCellLocationChanged(CellLocation location) {
// TODO Auto-generated method stub
if (location instanceof GsmCellLocation) {// gsm网络
phoneType = 1;
GsmCell gsmCell = new GsmCell();
gsmCell.lac = ((GsmCellLocation) location).getLac();
gsmCell.cid = ((GsmCellLocation) location).getCid();
/** 获取mcc,mnc */
String mccMnc=telephonyManager.getNetworkOperator();
if(mccMnc!=null&&mccMnc.length()>=5){
gsmCell.mcc = mccMnc.substring(0, 3);
gsmCell.mnc = mccMnc.substring(3, 5);
}
gsmCell.signal=lastSignal;
gsmCell.time = System.currentTimeMillis();
if (gsmCell.lac != -1 && gsmCell.cid != -1) {
gsmCells.add(0, gsmCell);
// Collections.sort(gsmCells);
// /**最多保留近3个基站新闻*/
// if (gsmCells.size() >3)
// gsmCells.remove(3);
}
//获取相邻基站新闻
List neighboringList=telephonyManager.getNeighboringCellInfo();
for(NeighboringCellInfo ni:neighboringList){
GsmCell gb = new GsmCell();
gb.mnc=mccMnc.substring(3, 5);
gb.lac=ni.getLac();
gb.cid=ni.getCid();
gb.signal=-133+2*ni.getRssi();
gb.time=System.currentTimeMillis();
gsmCells.add(gb);
}
} else {// 其他CDMA等网络
try {
Class cdmaClass =
Class.forName(“android.telephony.cdma.CdmaCellLocation”);
phoneType = 2;
CdmaCellLocation cdma = (CdmaCellLocation) location;
CdmaCell cdmaCell = new CdmaCell();
cdmaCell.stationId = cdma.getBaseStationId() >= 0 ?
cdma.getBaseStationId(): cdmaCell.stationId;
cdmaCell.networkId = cdma.getNetworkId() >= 0 ? cdma
.getNetworkId() : cdmaCell.networkId;
cdmaCell.systemId = cdma.getSystemId() >= 0 ? cdma
.getSystemId() : cdmaCell.systemId;
/** 获取mcc,mnc */
String mccMnc=telephonyManager.getNetworkOperator();
if(mccMnc!=null&&mccMnc.length()>=5){
cdmaCell.mcc = mccMnc.substring(0, 3);
cdmaCell.mnc = mccMnc.substring(3, 5);
}
cdmaCell.signal=lastSignal;
cdmaCell.time = System.currentTimeMillis();
int lat = cdma.getBaseStationLatitude();
int lon = cdma.getBaseStationLongitude();
if (lat <Integer.MAX_VALUE&& lon <Integer.MAX_VALUE) {
cdmaCell.lat = lat;
cdmaCell.lon = lon;
}
if (cdmaCell.stationId != -1&& cdmaCell.networkId != -1&&
cdmaCell.systemId != -1) {
cdmaCells.add(0, cdmaCell);
}
List neighboringList=telephonyManager.getNeighboringCellInfo();
for(NeighboringCellInfo ni:neighboringList){
CdmaCell cdmaBean = new CdmaCell();
cdmaBean.systemId=cdmaCell.systemId;
cdmaBean.lac=ni.getLac();
cdmaBean.cellid=ni.getCid();
cdmaBean.signal=-113+2*ni.getRssi();
cdmaCells.add(cdmaBean);
}
} catch (ClassNotFoundException classnotfoundexception) {
}
}// end CDMA网络
super.onCellLocationChanged(location);
}// end onCellLocationChanged

@Override
public void onServiceStateChanged(ServiceState serviceState) {
// TODO Auto-generated method stub
super.onServiceStateChanged(serviceState);
}
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
// TODO Auto-generated method stub
int asu=signalStrength.getGsmSignalStrength();
lastSignal=-113+2*asu; //时限信号强度
super.onSignalStrengthsChanged(signalStrength);
}
};
}// end InitPhoneStateListener

 

在Android中大家常用的轻易获得WIFI时限信号列表,那怎么赢得CDMA恐怕GSM的手提式有线电话机频域信号呢?
系统提供了TelephonyManager类,此类特别丰富,基本你所必要的手提式有线电话机音信都能得到到,那下边就来看看大家所急需的CDMA与GSM非信号是哪些获取的吗。

private TelephonyManager telephonyManager;
private PhoneStateListener phoneStateListener;
第一注解三个变量

在onCreate()方法中开始化变量
InitPhoneStateListener();
telephonyManager = (TelephonyManager)
this.ctx.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CELL_LOCATION);
if(telephonyManager.getCellLocation()!=null){
//获取当前基站新闻
phoneStateListener.onCellLocationChanged(telephonyManager.getCellLocation());
}

/** 初始化PhoneStateListener */
private void InitPhoneStateListener() {
phoneStateListener = new PhoneStateListener() {
@Override
public void onCellLocationChanged(CellLocation location) {
// TODO Auto-generated method stub
if (location instanceof GsmCellLocation) {// gsm网络
phoneType = 1;
GsmCell gsmCell = new GsmCell();
gsmCell.lac = ((GsmCellLocation) location).getLac();
gsmCell.cid = ((GsmCellLocation) location).getCid();
/** 获取mcc,mnc */
String mccMnc=telephonyManager.getNetworkOperator();
if(mccMnc!=null&&mccMnc.length()>=5){
gsmCell.mcc = mccMnc.substring(0, 3);
gsmCell.mnc = mccMnc.substring(3, 5);
}
gsmCell.signal=lastSignal;
gsmCell.time = System.currentTimeMillis();
if (gsmCell.lac != -1 && gsmCell.cid != -1) {
gsmCells.add(0, gsmCell);
// Collections.sort(gsmCells);
// /**最多保留近3个基站新闻*/
// if (gsmCells.size() >3)
// gsmCells.remove(3);
}
//获取相邻基站音信
List neighboringList=telephonyManager.getNeighboringCellInfo();
for(NeighboringCellInfo ni:neighboringList){
GsmCell gb = new GsmCell();
gb.mnc=mccMnc.substring(3, 5);
gb.lac=ni.getLac();
gb.cid=ni.getCid();
gb.signal=-133+2*ni.getRssi();
gb.time=System.currentTimeMillis();
gsmCells.add(gb);
}
} else {// 其他CDMA等网络
try {
Class cdmaClass =
Class.forName(“android.telephony.cdma.CdmaCellLocation”);
phoneType = 2;
CdmaCellLocation cdma = (CdmaCellLocation) location;
CdmaCell cdmaCell = new CdmaCell();
cdmaCell.stationId = cdma.getBaseStationId() >= 0 ?
cdma.getBaseStationId(): cdmaCell.stationId;
cdmaCell.networkId = cdma.getNetworkId() >= 0 ? cdma
.getNetworkId() : cdmaCell.networkId;
cdmaCell.systemId = cdma.getSystemId() >= 0 ? cdma
.getSystemId() : cdmaCell.systemId;
/** 获取mcc,mnc */
String mccMnc=telephonyManager.getNetworkOperator();
if(mccMnc!=null&&mccMnc.length()>=5){
cdmaCell.mcc = mccMnc.substring(0, 3);
cdmaCell.mnc = mccMnc.substring(3, 5);
}
cdmaCell.signal=lastSignal;
cdmaCell.time = System.currentTimeMillis();
int lat = cdma.getBaseStationLatitude();
int lon = cdma.getBaseStationLongitude();
if (lat <Integer.MAX_VALUE&& lon <Integer.MAX_VALUE) {
cdmaCell.lat = lat;
cdmaCell.lon = lon;
}
if (cdmaCell.stationId != -1&& cdmaCell.networkId != -1&&
cdmaCell.systemId != -1) {
cdmaCells.add(0, cdmaCell);
}
List neighboringList=telephonyManager.getNeighboringCellInfo();
for(NeighboringCellInfo ni:neighboringList){
CdmaCell cdmaBean = new CdmaCell();
cdmaBean.systemId=cdmaCell.systemId;
cdmaBean.lac=ni.getLac();
cdmaBean.cellid=ni.getCid();
cdmaBean.signal=-113+2*ni.getRssi();
cdmaCells.add(cdmaBean);
}
} catch (ClassNotFoundException classnotfoundexception) {
}
}// end CDMA网络
super.onCellLocationChanged(location);
}// end onCellLocationChanged

@Override
public void onServiceStateChanged(ServiceState serviceState) {
// TODO Auto-generated method stub
super.onServiceStateChanged(serviceState);
}
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
// TODO Auto-generated method stub
int asu=signalStrength.getGsmSignalStrength();
lastSignal=-113+2*asu; //实信号强度
super.onSignalStrengthsChanged(signalStrength);
}
};
}// end InitPhoneStateListener
实际很简短,只要写好了监听类,一切复信号都足以赢得获得。
那般,大家很轻松的就拿走到了手提式有线电电话机数字信号及四周基站的复信号强度了,当然,须要的读取手提式有线电话机状态等的权能就在那边不说了。

作者:sojex
Email:w.sojex@gmail.com