我正在尝试获取设备可以找到的所有可用单元格的列表。但我被卡住了,因为我的CellInfo总是null,我不明白为什么。有人可以给我一个提示吗?在google上关于onCellInfoChanged()的信息很少。

主要活动:

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

N.逻辑学:

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, ,这可能意味着根本没有电话

对我来说(股票android6.0,nexus6),有之间的选择 MAX_INT (一个带有null的调用), 01000.

我不是100%确定这些值意味着什么,但大概0代表即时(因此非常多)呼叫,1000代表呼叫之间至少一秒钟的东西。但请记住,这纯粹是猜测。

我会编辑这个答案,因为我发现更多,例如通过查看所述测试应用程序的实现。

其他提示

@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