onCellInfoChanged()回调始终为null [英] onCellInfoChanged() callback is always null

查看:251
本文介绍了onCellInfoChanged()回调始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am trying to get a list of all available cells the device can find. But i am stuck, as my CellInfo is always null and i don't figure why. Can someone give me a hint? There is pretty few info on onCellInfoChanged() at google.

MainActivity:

MainActivity:

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

CellListener:

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) 

您可以看到两个事件(onCellInfoChanged& onCellLocationChanged)都被触发一次,后者正确返回了设备正在使用的当前单元格。

As you can see both events (onCellInfoChanged & onCellLocationChanged) get triggered once and the latter is correctly returning the current cell the device is using.

推荐答案

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

The real reason you are only being called exactly once and with null as an argument is the following: there appears to be a rate-limiting setting in place by default, as can be observed in the "testing" app, which can be accessed by dialing *#*#INFO#*#* (i.e. *#*#4636#*#*).

在测试应用中,选择电话信息并向下滚动到按钮 CELLINFOLISTRATE xxxx 大概是 CELLINFOLISTRATE 2147483647 。由于 2147483647 == MAX_INT ,这可能意味着根本没有呼叫

In the testing app, choose "Phone Information" and scroll down to the button CELLINFOLISTRATE xxxx, in your case presumably CELLINFOLISTRATE 2147483647. As 2147483647 == MAX_INT, this probably means no calls at all

对我来说(股票android 6.0,联系6 ),可以在 MAX_INT (一个调用为null), 0 1000之间进行选择

For me (stock android 6.0, nexus 6), there's the a choice between MAX_INT (one call with null), 0 and 1000.

我不确定100%这些值是什么意思,但是大概0代表即时(因此很多)通话,两次通话之间至少要间隔一秒钟才能达到1000。

I'm not 100% sure what these values mean, but presumably the 0 stands for instant (and thus very many) calls, and 1000 for something like at least a second between calls. Keep in mind though, this is pure speculation.

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

I will edit this answer as I find out more, for example by looking at the implementation of said testing app.

这篇关于onCellInfoChanged()回调始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆