简体   繁体   English

如何在Android中获取单元格ID和LAC?

[英]how to get the cell Id and LAC in android?

actually I'm looking about a method to get a cell parameters like cell id LAC RSSI etc. you can see bellow the code that I used but it's not working I dont know why. 实际上,我正在寻找一种获取像细胞ID LAC RSSI这样的细胞参数的方法。您可以在下面看到我使用的代码,但是它不起作用,我不知道为什么。

public final class MainActivity  extends AppCompatActivity  {

    PhoneStateListener phoneStateListener;
    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        this.phoneStateListener = setupPhoneStateListener();
        this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
        this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
        this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

        // This part is used to listen for properties of the neighboring cells
        List<NeighboringCellInfo> neighboringCellInfos = this.telephonyManager.getNeighboringCellInfo();
        for (NeighboringCellInfo neighboringCellInfo : neighboringCellInfos) {
            neighboringCellInfo.getCid();
            neighboringCellInfo.getLac();
            neighboringCellInfo.getPsc();
            neighboringCellInfo.getNetworkType();
            neighboringCellInfo.getRssi();

            Log.d("cellp", neighboringCellInfo.toString());
        }
    }
    public PhoneStateListener setupPhoneStateListener() {
        return new PhoneStateListener() {

            /** Callback invoked when device cell location changes. */
            @SuppressLint("NewApi")
            public void onCellLocationChanged(CellLocation location)
            {
                GsmCellLocation gsmCellLocation = (GsmCellLocation) location;
                gsmCellLocation.getCid();
                gsmCellLocation.getLac();
                gsmCellLocation.getPsc();

                Log.d("cellp", "registered: "+gsmCellLocation.toString());
            }

            /** invoked when data connection state changes (only way to get the network type) */
            public void onDataConnectionStateChanged(int state, int networkType)
            {
                Log.d("cellp", "registered: "+networkType);
            }

            /** Callback invoked when network signal strengths changes. */
            public void onSignalStrengthsChanged(SignalStrength signalStrength)
            {
                Log.d("cellp", "registered: "+signalStrength.getGsmSignalStrength());
            }

        };
    }
}

获取单元ID:

myCellId= cellLocation.getCid() % 0xffff;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM