Android 10基站信息变化适配
Posted Alex_MaHao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 10基站信息变化适配相关的知识,希望对你有一定的参考价值。
在android上基站信息的获取可以通过方法:
mTelephonyManager.getAllCellInfo()
但在android10上,该方法有所变动。
Beginning with Android Q, if this API results in a change of the cached CellInfo, that change will be reported via onCellInfoChanged().
Apps targeting Android Q or higher will no longer trigger a refresh of the cached CellInfo by invoking this API. Instead, those apps will receive the latest cached results, which may not be current. Apps targeting Android Q or higher that wish to request updated CellInfo should call requestCellInfoUpdate(); however, in all cases, updates will be rate-limited and are not guaranteed. To determine the recency of CellInfo data, callers should check CellInfo#getTimeStamp().
This method returns valid data for devices with FEATURE_TELEPHONY. In cases where only partial information is available for a particular CellInfo entry, unavailable fields will be reported as android.telephony.CellInfo#UNAVAILABLE. All reported cells will include at least a valid set of technology-specific identification info and a power level measurement.
在应用target < 29
时,调用该方法会立即刷新基站信息。但在target >=29
时,调用该方法不会在立即刷新基站信息,可能获取的是一个缓存的基站信息
如果对基站信息的实时性要求不高,通过上面的方法即可。
但是如果对基站信息要求较高,需要通过requestCellInfoUpdate
去刷新基站信息,该方法介绍如下:
Requests all available cell information from the current subscription for observed camped/registered, serving, and neighboring cells.
Any available results from this request will be provided by calls to onCellInfoChanged() for each active subscription.
This method returns valid data for devices with FEATURE_TELEPHONY. On devices that do not implement this feature, the behavior is not reliable.
该方法会回调刷新后的基站信息,但是通过实验发现,回调的基站信息并不完整,再调用getAllCellInfo
获取更完整。
所以最后的逻辑如下:
final CountDownLatch countDownLatch = new CountDownLatch(1);
mTelephonyManager.requestCellInfoUpdate(mRequestCellExecutor, new TelephonyManager.CellInfoCallback()
@Override
public void onCellInfo(@NonNull List<CellInfo> cellInfo)
countDownLatch.countDown();
@Override
public void onError(int errorCode, @Nullable Throwable detail)
super.onError(errorCode, detail);
countDownLatch.countDown();
);
// 经过测试,一般为30ms左右
boolean result = countDownLatch.await(1000, TimeUnit.MILLISECONDS);
List<CellInfo> allCellInfo = mTelephonyManager.getAllCellInfo();
以上是关于Android 10基站信息变化适配的主要内容,如果未能解决你的问题,请参考以下文章
Android Q 基站刷新接口源码分析 & 适配双卡手机基站刷新逻辑
Android Q 基站刷新接口源码分析 & 适配双卡手机基站刷新逻辑
Android Q 基站刷新接口源码分析 & 适配双卡手机基站刷新逻辑