編輯:關於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信號信息,並獲得基站信息的方法全部內容了,希望大家多多支持本站~
在Android中復雜的圖形的繪制絕大多數是通過path來實現,比如繪制一條曲線,然後讓一個物體隨著這個曲線運動,比如搜索按鈕,比如一個簡單時鐘的實現:那麼什麼是path
Android客戶端和PHP、MySQL搭建的服務器之間的簡單交互,實現登錄功能 。實現原理圖:Handler消息機制原理:Handler機制主要包括4個關鍵對象,分別是
本文的目的是要實現左右滑動的指引效果。那麼什麼是指引效果呢?現在的應用為了有更好的用戶體驗,一般會在應用開始顯示一些指引幫助頁面,使用戶能更好的理解應用的功能,甚至是一些
應用進程保活基本就是圍繞兩個方面來展開:1 盡量保證進程不被殺死。2 進程被殺死後復活。細分如下:1)Service重啟2)進程守護3)Receiver觸發4)Alarm