Android TelephonyManager獲取LET信息及手機基本信息

做一波獲取手機卡LET的信息操作。看了一波源碼寫出來的一些東西

首先需要的一些權限(危險權限動態獲取一下,之前的裏面有):

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

//首先獲取手機管理者類

tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

//獲取設備編號

tm.getDeviceId();

//SIM卡提供商的ISO國家代碼

tm.getSimCountryIso();

//獲取SIM卡序列號

tm.getSimSerialNumber();

//獲取網絡運營商代號(MCC+MNC)

tm.getNetworkOperator();

//獲取網絡運營商名稱

tm.getNetworkOperatorName();

//獲取設備當前所在位置

tm.getCellLocation();

//獲取cId,lac,psc,基於位置信息獲取

CellLocation cellLocation = tm.getCellLocation();
if(cellLocation instanceof GsmCellLocation) {
    GsmCellLocation location = (GsmCellLocation)cellLocation;
    int cid = location.getCid();
    int lac = location.getLac();
    int psc = location.getPsc();
}

//獲取手機類型

tm.getPhoneType();

//獲取手機號碼

tm.getLine1Number();

//獲取國際長途區號

tm.getNetworkCountryIso();

//獲取連接狀態

tm.getDataState();

//獲取網絡類型(SDK版本29,纔有5G,5G代號NR)

tm.getNetworkType();

//獲取臨近小區信息

之前的tm.getNeighboringCellInfo();這個方法在SDK29版本里刪除掉了,所以得用另一種方法獲取。

List<CellInfo> allCellInfo = tm.getAllCellInfo();
//集合返回的第一個數據,allCellInfo.get(0)就是當前小區的數據
        String ss = allCellInfo.toString();
        for(CellInfo cellInfo : allCellInfo){
            if(cellInfo instanceof CellInfoGsm) {
                CellInfoGsm infoGsm = (CellInfoGsm) cellInfo;
                CellIdentityGsm cellIdentity = infoGsm.getCellIdentity();
                int cid = cellIdentity.getCid();
                int lac = cellIdentity.getLac();
                int psc = cellIdentity.getPsc();
                int arfcn = cellIdentity.getArfcn();
                int rsrp = infoGsm.getCellSignalStrength().getDbm();
                str = "2G=====cid:"+cid+"=====lac:"+lac+"=====psc:"+psc+"=====arfcn:"+arfcn+"=====rsrp:"+rsrp+"\n";
            }else if(cellInfo instanceof CellInfoWcdma){
                CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
                int cid = cellInfoWcdma.getCellIdentity().getCid();
                int lac = cellInfoWcdma.getCellIdentity().getLac();
                int psc = cellInfoWcdma.getCellIdentity().getPsc();
                int rsrp = cellInfoWcdma.getCellSignalStrength().getDbm();
                serverCellInfo.asulevel = cellInfoWcdma.getCellSignalStrength().getAsuLevel();
                str = "3G=====cid:"+cid+"=====lac:"+lac+"=====psc:"+psc+"=====rsrp:"+rsrp+"\n";
            }else if(cellInfo instanceof CellInfoLte){
                CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
                int cid = cellInfoLte.getCellIdentity().getCi();
                int pci = cellInfoLte.getCellIdentity().getPci();
                int tac = cellInfoLte.getCellIdentity().getTac();
                int earfcn = cellInfoLte.getCellIdentity().getEarfcn();
                int rsrp = cellInfoLte.getCellSignalStrength().getRsrp();
                int rsrq = cellInfoLte.getCellSignalStrength().getRsrq();
//                Object rssi = cellInfoLte.getCellSignalStrength().getRssi();
                Object rssnr = cellInfoLte.getCellSignalStrength().getRssnr();
                Object bandwidth = cellInfoLte.getCellIdentity().getBandwidth();
                str = "4G=====cid:"+cid+"=====pci:"+pci+"=====tac:"+tac+"=====earfcn:"+earfcn+"=====rsrp:"+rsrp+"=====rsrq:"
                        +rsrq+"=====rssnr:"+rssnr+"=====bandWidth:"+bandwidth+"\n";

            }else if(cellInfo instanceof CellInfoNr){
                CellInfoNr cellInfoNr = (CellInfoNr) cellInfo;
                int asuLevel = cellInfoNr.getCellSignalStrength().getAsuLevel();
                int rsrp = cellInfoNr.getCellSignalStrength().getDbm();
                int level = cellInfoNr.getCellSignalStrength().getLevel();
                str = "5G=====asuLevel:"+asuLevel+"=====rsrp:"+rsrp+"=====level:"+level;
            }
        }

注:商用手機無法獲得5G下的小區信息,可以檢測到是NR(5G)網絡,但是即使在5G情況下獲得的仍是4G錨點小區的信息!!!想要獲取需要跟手機廠家去購買獲取Root過的手機。

 

//上面那個方法沒法獲取rssi,總是報錯,獲取sinr,cqi,rsrp,rssi等信息

//註冊一個監聽,利用反射拿到這些數據,在監聽後拿數據即可

class MyPhoneStateListener extends PhoneStateListener {
    @Override
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        super.onSignalStrengthsChanged(signalStrength);
        if (phoneGeneralInfo.ratType == TelephonyManager.NETWORK_TYPE_LTE) {
            try {
                int rssi = (Integer) signalStrength.getClass().getMethod("getLteSignalStrength").invoke(signalStrength);
                int rsrp = (Integer) signalStrength.getClass().getMethod("getLteRsrp").invoke(signalStrength);
                int rsrq = (Integer) signalStrength.getClass().getMethod("getLteRsrq").invoke(signalStrength);
                int sinr = (Integer) signalStrength.getClass().getMethod("getLteRssnr").invoke(signalStrength);
                int cqi = (Integer) signalStrength.getClass().getMethod("getLteCqi").invoke(signalStrength);
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        } else if (phoneGeneralInfo.ratType == TelephonyManager.NETWORK_TYPE_GSM) {
            try {
                serverCellInfo.rssi = signalStrength.getGsmSignalStrength();
                serverCellInfo.rsrp = (Integer) signalStrength.getClass().getMethod("getGsmDbm").invoke(signalStrength);
                serverCellInfo.asulevel = (Integer) signalStrength.getClass().getMethod("getAsuLevel").invoke(signalStrength);
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        } else if (phoneGeneralInfo.ratType == TelephonyManager.NETWORK_TYPE_TD_SCDMA) {
            try {
                int rssi = (Integer) signalStrength.getClass().getMethod("getTdScdmaLevel").invoke(signalStrength);
                int rsrp = (Integer) signalStrength.getClass().getMethod("getTdScdmaDbm").invoke(signalStrength);
                int asulevel = (Integer) signalStrength.getClass().getMethod("getAsuLevel").invoke(signalStrength);
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
    }
}

//監聽使用方法,需要調用一下

MyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();
tm.listen(myPhoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

 

發佈了58 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章