Domanda

Sto cercando di ottenere un elenco di tutte le celle disponibili che il dispositivo può trovare.Ma sono bloccato, come il mio cellulare è sempre nullo e non capisco perché.Qualcuno può darmi un suggerimento?Ci sono pratiche informazioni su OncellInFochanged () su Google.

Maineattività:

 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);     
    }
}
.

logcat:

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) 
.

Come puoi vedere entrambi gli eventi (OncellInFochanged & OncellLolocationChanged) Get Trigged una volta e quest'ultimo restituisce correttamente la cella corrente, il dispositivo utilizza.

È stato utile?

Soluzione

La vera ragione per cui viene chiamato esattamente una volta e con Null come argomento è quanto segue: Sembra che ci sia un'impostazione limitante per il tasso in atto per impostazione predefinita, come si può osservare nell'app "Testing", che può Accessibile componendo *#*#INFO#*#* (IE *#*#4636#*#*).

Nell'app di test, selezionare "Informazioni sul telefono" e scorrere verso il basso fino al pulsante CELLINFOLISTRATE xxxx, nel caso in cui presumibilmente CELLINFOLISTRATE 2147483647. Come 2147483647 == MAX_INT, probabilmente significa nessuna chiamata a tutti

Per me (Stock Android 6.0, Nexus 6), c'è una scelta tra MAX_INT (una chiamata con NULL), 0 e 1000.

Non sono sicuro al 100% Ciò che questi valori significano, ma presumibilmente lo 0 rappresenta chiamate istantanee (e quindi molto), e 1000 per qualcosa come almeno un secondo tra le chiamate. Tieni presente, tuttavia, questa è pura speculazione.

Moderò questa risposta mentre scoprirò di più, ad esempio guardando l'implementazione di detta app di test.

Altri suggerimenti

@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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top