Frage

ich versuche, eine Liste aller verfügbaren Zellen zu erhalten, die das Gerät finden kann.Aber ich stecke fest, da meine CellInfo immer null ist und ich nicht verstehe warum.Kann mir jemand einen Hinweis geben?Es gibt ziemlich wenige Informationen zu onCellInfoChanged () bei Google.

Hauptaktivität:

 CellListener cellListener = new CellListener(this);
 cellListener.start();

CellListener:

public class CellListener extends PhoneStateListener {

private static final String TAG = "CellListener";
private TelephonyManager telephonyManager = null;
private PhoneStateListener listener = null;
private String newCell = null;  
private int events = PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_CELL_INFO;
private Context context = null;

public CellListener(Context context) {
    this.context = context;
}

public void start() {

    telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    CellLocation.requestLocationUpdate();

    telephonyManager.listen(this, events);
}

@Override
public void onCellInfoChanged(List<CellInfo> cellInfo) {
    Log.i("CellListener","onCellInfoChanged(List<CellInfo> cellInfo) ");
    super.onCellInfoChanged(cellInfo);

     if(cellInfo == null) return;     // this always null here

     for (CellInfo c : cellInfo) {          
        Log.i("CellListener"," c = "+c);
    }       
 }

 @Override
    public void onCellLocationChanged(CellLocation location) {
        if (!(location instanceof GsmCellLocation)) {
            return;
        }
        GsmCellLocation gsmCell = (GsmCellLocation) location;
        String operator = telephonyManager.getNetworkOperator();
        if (operator == null || operator.length() < 4) {
            return;
        }
        newCell = operator.substring(0, 3) + ':' + operator.substring(3) + ':'
                + gsmCell.getLac() + ':' + gsmCell.getCid();

        Log.i(TAG,"newCell = "+newCell);     
    }
}

Holzkatze:

11-18 14:50:23.806: I/CellListener(4953): newCell = 262:02:4311:99031735
11-18 14:50:23.814: I/CellListener(4953): onCellInfoChanged(List<CellInfo> cellInfo) 

Wie Sie sehen, werden beide Ereignisse (onCellInfoChanged & onCellLocationChanged ) einmal ausgelöst und letzteres gibt die aktuelle Zelle, die das Gerät verwendet, korrekt zurück.

War es hilfreich?

Lösung

Der wahre Grund, warum Sie nur genau einmal und mit null als Argument aufgerufen werden, ist der folgende:es scheint standardmäßig eine ratenbegrenzende Einstellung vorhanden zu sein, wie in der "Testing" -App zu sehen ist, auf die durch Wählen zugegriffen werden kann *#*#INFO#*#* (also. *#*#4636#*#*).

Wählen Sie in der Test-App "Telefoninformationen" und scrollen Sie nach unten zur Schaltfläche CELLINFOLISTRATE xxxx, in Ihrem Fall vermutlich CELLINFOLISTRATE 2147483647.Als 2147483647 == MAX_INT, das bedeutet wahrscheinlich überhaupt keine Anrufe

Für mich (Stock Android 6.0, Nexus 6) gibt es die Wahl zwischen MAX_INT (ein Anruf mit null), 0 und 1000.

Ich bin mir nicht 100% sicher, was diese Werte bedeuten, aber vermutlich steht die 0 für sofortige (und damit sehr viele) Anrufe und 1000 für so etwas wie mindestens eine Sekunde zwischen Anrufen.Denken Sie jedoch daran, dies ist reine Spekulation.

Ich werde diese Antwort bearbeiten, sobald ich mehr herausfinde, zum Beispiel indem ich mir die Implementierung dieser Test-App anschaue.

Andere Tipps

@bofredo: It is returning null because you haven't defined CellInfo yet.

Can't see from your question if you're using CDMA, GSM, or LTE. From memory, CDMA doesn't seem to return anything, but GSM (CellInfoGsm) and LTE (CellInfoLte) do. So doing something like for example:

CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;

will return an instance of CellInfoGsm or CellInfoLte which will then allow you to retrieve more information about a cell like:

CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();
CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();  

or

CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();
CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength();

Then use cellIdentityGsm, cellIdentityLte, cellSignalStrengthGsm and cellSignalStrengthLte to do what you want in onCellInfoChanged.

In addition to @bimmlerd answer. If you are updating the Phone Information via dialing *#*#INFO#*#* (i.e. *#*#4636#*#*). There are chances that the hidden menu doesn't appear. In that case, use default dialer if you are using some 3rd party call management application (Worked for me as it was not showing any hidden menu for phone information).

https://www.reddit.com/r/GooglePixel/comments/6xc40q/4636_service_menu_not_available_in_oreo/

Note: The following picture will help you with the latest Android OS. Just update the mobile info refresh rate in phone information 1/2 option (I hope multiple phone information because of multiple sim cards)

Hidden Menu enter image description here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top