Question

j'essaie d'obtenir une liste de toutes les cellules disponibles que l'appareil peut trouver.Mais je suis coincé, car mon CellInfo est toujours nul et je ne comprends pas pourquoi.Quelqu'un peut-il me donner un indice ?Il y a assez peu d'informations sur onCellInfoChanged() sur Google.

Activité principale:

 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) 

Comme vous pouvez le voir, les deux événements (onCellInfoChanged et onCellLocationChanged) sont déclenchés une fois et ce dernier renvoie correctement la cellule actuelle utilisée par l'appareil.

Était-ce utile?

La solution

La vraie raison pour laquelle vous n'êtes appelé qu'une seule fois et avec null comme argument est la suivante :il semble y avoir un paramètre de limitation de débit en place par défaut, comme on peut l'observer dans l'application "test", accessible en composant le numéro *#*#INFO#*#* (c'est à dire. *#*#4636#*#*).

Dans l'application de test, choisissez « Informations sur le téléphone » et faites défiler jusqu'au bouton CELLINFOLISTRATE xxxx, dans votre cas probablement CELLINFOLISTRATE 2147483647.Comme 2147483647 == MAX_INT, cela signifie probablement qu'il n'y a aucun appel

Pour moi (Android 6.0 d'origine, Nexus 6), vous avez le choix entre MAX_INT (un appel avec null), 0 et 1000.

Je ne suis pas sûr à 100% de la signification de ces valeurs, mais vraisemblablement le 0 représente les appels instantanés (et donc très nombreux), et 1000 pour quelque chose comme au moins une seconde entre les appels.Gardez cependant à l’esprit qu’il s’agit de pure spéculation.

Je modifierai cette réponse au fur et à mesure que j'en saurai plus, par exemple en examinant la mise en œuvre de ladite application de test.

Autres conseils

@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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top