Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 獲取手機GSM/CDMA信號信息,並獲得基站信息的方法

android 獲取手機GSM/CDMA信號信息,並獲得基站信息的方法

編輯:關於Android編程

在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 獲取手機GSM/CDMA信號信息,並獲得基站信息的方法全部內容了,希望大家多多支持本站~

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved