android 获取手机GSM/CDMA信号信息,并获得基站信息的方法
在Android中我们常用的轻松获取WIFI信号列表,那如何获取CDMA或者GSM的手机信号呢?
系统提供了TelephonyManager类,此类非常丰富,基本你所需要的手机信息都能获取到,那下面就来看看我们所需要的CDMA与GSM信号是如何获取的吧。
privateTelephonyManagertelephonyManager;
privatePhoneStateListenerphoneStateListener;
首先声明两个变量
在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*/ privatevoidInitPhoneStateListener(){ phoneStateListener=newPhoneStateListener(){ @Override publicvoidonCellLocationChanged(CellLocationlocation){ //TODOAuto-generatedmethodstub if(locationinstanceofGsmCellLocation){//gsm网络 phoneType=1; GsmCellgsmCell=newGsmCell(); gsmCell.lac=((GsmCellLocation)location).getLac(); gsmCell.cid=((GsmCellLocation)location).getCid(); /**获取mcc,mnc*/ StringmccMnc=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); } //获取相邻基站信息 ListneighboringList=telephonyManager.getNeighboringCellInfo(); for(NeighboringCellInfoni:neighboringList){ GsmCellgb=newGsmCell(); 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{ ClasscdmaClass=Class.forName("android.telephony.cdma.CdmaCellLocation"); phoneType=2; CdmaCellLocationcdma=(CdmaCellLocation)location; CdmaCellcdmaCell=newCdmaCell(); 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*/ StringmccMnc=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(); intlat=cdma.getBaseStationLatitude(); intlon=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); } ListneighboringList=telephonyManager.getNeighboringCellInfo(); for(NeighboringCellInfoni:neighboringList){ CdmaCellcdmaBean=newCdmaCell(); cdmaBean.systemId=cdmaCell.systemId; cdmaBean.lac=ni.getLac(); cdmaBean.cellid=ni.getCid(); cdmaBean.signal=-113+2*ni.getRssi(); cdmaCells.add(cdmaBean); } }catch(ClassNotFoundExceptionclassnotfoundexception){ } }//endCDMA网络 super.onCellLocationChanged(location); }//endonCellLocationChanged @Override publicvoidonServiceStateChanged(ServiceStateserviceState){ //TODOAuto-generatedmethodstub super.onServiceStateChanged(serviceState); } @Override publicvoidonSignalStrengthsChanged(SignalStrengthsignalStrength){ //TODOAuto-generatedmethodstub intasu=signalStrength.getGsmSignalStrength(); lastSignal=-113+2*asu;//信号强度 super.onSignalStrengthsChanged(signalStrength); } }; }//endInitPhoneStateListener
以上就是小编为大家带来的android获取手机GSM/CDMA信号信息,并获得基站信息的方法全部内容了,希望大家多多支持毛票票~