문제

장치가 찾을 수 있는 모든 사용 가능한 셀 목록을 얻으려고 합니다.하지만 내 CellInfo가 항상 null이고 이유를 알 수 없기 때문에 막혔습니다.누군가 나에게 힌트를 줄 수 있습니까?Google에는 onCellInfoChanged()에 대한 정보가 거의 없습니다.

주요 활동:

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

셀리스너:

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

로그캣:

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) 

보시다시피 두 이벤트(onCellInfoChanged 및 onCellLocationChanged)가 한 번 트리거되고 후자는 장치가 사용 중인 현재 셀을 올바르게 반환합니다.

도움이 되었습니까?

해결책

정확히 한 번만 호출되고 null을 인수로 사용하는 실제 이유는 다음과 같습니다.전화를 걸어 액세스할 수 있는 "테스트" 앱에서 볼 수 있듯이 기본적으로 속도 제한 설정이 있는 것으로 보입니다. *#*#INFO#*#* (즉. *#*#4636#*#*).

테스트 앱에서 "전화 정보"를 선택하고 버튼까지 아래로 스크롤합니다. CELLINFOLISTRATE xxxx, 아마도 귀하의 경우에는 CELLINFOLISTRATE 2147483647.처럼 2147483647 == MAX_INT, 이는 아마도 통화가 전혀 없음을 의미할 것입니다.

나에게는 (스톡 안드로이드 6.0, 넥서스 6) 다음 중 하나를 선택할 수 있습니다. MAX_INT (null이 있는 한 번의 호출) 0 그리고 1000.

이 값이 무엇을 의미하는지 100% 확신할 수는 없지만 아마도 0은 즉각적인(따라서 매우 많은) 호출을 나타내고 1000은 호출 사이에 최소 1초 정도를 의미합니다.하지만 이것은 순수한 추측이라는 점을 명심하세요.

예를 들어 해당 테스트 앱의 구현을 살펴보는 등 자세한 내용을 알아보면 이 답변을 편집하겠습니다.

다른 팁

@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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top