onCellInfoChanged()回调始终为null

bof*_*edo 3 android cell cellinfo

我正在尝试获取设备可以找到的所有可用单元的列表。但是我被卡住了,因为我的CellInfo始终为null,我不知道为什么。有人可以给我提示吗?Google的onCellInfoChanged()信息很少。

主要活动:

 CellListener cellListener = new CellListener(this);
 cellListener.start();
Run Code Online (Sandbox Code Playgroud)

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);     
    }
}
Run Code Online (Sandbox Code Playgroud)

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) 
Run Code Online (Sandbox Code Playgroud)

如您所见,两个事件(onCellInfoChanged和onCellLocationChanged)都被触发一次,而后者正确返回了设备正在使用的当前单元格。

bim*_*erd 5

您只被调用一次且参数为null的真正原因如下:默认情况下,似乎存在一个限速设置,如在“测试”应用程序中可以看到的,拨号*#*#INFO#*#*(即*#*#4636#*#*)。

在测试应用程序中,选择“电话信息”,然后向下滚动到该按钮CELLINFOLISTRATE xxxx(可能是您想使用的按钮)CELLINFOLISTRATE 2147483647。作为2147483647 == MAX_INT,这可能意味着根本没有通话

对我来说(股票android 6.0,nexus 6),可以选择MAX_INT(一个调用为null)0和之间1000

我不确定这些值的含义是100%,但是大概0代表即时(因此很多)呼叫,而1000代表两次呼叫之间的间隔至少一秒钟。不过请记住,这纯粹是猜测。

当我找到更多信息时,我将编辑此答案,例如,通过查看所述测试应用程序的实现。