我正在尝试获取设备可以找到的所有可用单元的列表。但我被卡住了,因为我的 CellInfo 总是为空,我不知道为什么。有人可以给我一个提示吗?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)都被触发一次,后者正确返回设备正在使用的当前单元格。