我正在试图获得设备可以找到的所有可用单元格的列表。但是我被困住了,因为我的CellInfo总是空的,我不知道为什么。谁能给我个提示吗?谷歌几乎没有关于onCellInfoChanged()的信息。
MainActivity:
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);
}
}逻辑猫:
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)都会触发一次,而后者将正确返回设备正在使用的当前单元格。
发布于 2016-04-19 22:07:05
您只被调用一次并以null作为参数的真正原因如下:在默认情况下,似乎存在一个速率限制设置,在“测试”应用程序中可以观察到,它可以通过拨号*#*#INFO#*#* (即*#*#4636#*#*)来访问。
在测试应用程序中,选择"Phone Information“并向下滚动到按钮CELLINFOLISTRATE xxxx,在您的例子中可能是CELLINFOLISTRATE 2147483647。作为2147483647 == MAX_INT,这可能意味着根本没有调用
对我来说(股票Android6.0,nexus 6),可以在MAX_INT (一个带有null的调用)、0和1000之间做出选择。
我并不100%确定这些值是什么意思,但据推测,0表示即时(因此也是很多)调用,1000表示至少在调用之间的一秒。不过请记住,这纯粹是猜测。
我将编辑这个答案,因为我会发现更多,例如,通过查看所述测试应用的实现。
发布于 2013-11-18 16:39:14
@bofredo:它返回null,因为您还没有定义CellInfo。
无法从您的问题中看出您使用的是CDMA、GSM或LTE。从内存中看,CDMA似乎没有返回任何东西,但GSM (CellInfoGsm)和LTE (CellInfoLte)却是如此。所以做一些类似的事情,例如:
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;将返回CellInfoGsm或CellInfoLte的实例,该实例将允许您检索有关以下单元格的更多信息:
CellIdentityGsm cellIdentityGsm = cellInfoGsm.getCellIdentity();
CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity(); 或
CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();
CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength();然后使用cellIdentityGsm、cellIdentityLte、cellSignalStrengthGsm和cellSignalStrengthLte在onCellInfoChanged中做您想做的事情。
发布于 2019-03-11 09:38:58
除了@bimmlerd的回答。如果您正在通过拨号Phone Information (即*#*#4636#*#*)更新*#*#INFO#*#*。隐藏菜单可能不会出现。在这种情况下,如果您正在使用某个第三方电话管理应用程序,请使用默认拨号程序(因为它没有显示任何隐藏的电话信息菜单)。
注意:下面的图片将帮助您使用最新的Android操作系统。只需更新mobile info refresh rate中的电话信息1/2选项(我希望多个电话信息,因为多卡)


https://stackoverflow.com/questions/20049510
复制相似问题