0
其實我正在尋找一種方法來獲取單元格參數,如單元ID LAC RSSI等,你可以看到下面的代碼,我使用,但它不工作,我不知道爲什麼。如何在android中獲取單元格ID和LAC?
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());
}
};
}
}