What is proper usage of requestCellInfoUpdate()?

本小妞迷上赌 提交于 2021-02-18 08:23:24

问题


Utilizing onSignalStrengthsChanged, getAllCellInfo(), and related methods, my app monitors cell signal data and displays the results in realtime. My code works very well when targeting API 28 and lower, automatically refreshing the data as it changes. Targeting API 29 results in some Android 10 devices failing to update the data -- but not all.

I discovered TelephonyManager.requestCellInfoUpdate() was added to API 29, which may(?) be needed to resolve this issue. However, I have been unable to find any information about this method beyond the concise definition on the Android Reference. Does this method need to be used to refresh cell info? Are any code samples or further explanations available?

If that method is not relevant, is there another change in API 29 that could cause this behavior? ACCESS_FINE_LOCATION is confirmed to be granted, which appears to be the only other relevant API change.


回答1:


Reading the docs there is a mention of this in the getAllCellInfo() documentation.

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().

So the preference is if you are targeting Android Q or higher, you should be opting for requestCellInfoUpdate()




回答2:


I have noticed the same behaviour targeting Android 10 (API Level 29). The only workaround I have found is to regularly poll the API and look for changes. Example code below:

Timer timer = new Timer();      
    timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                tm.requestCellInfoUpdate(minThreadExecutor, new TelephonyManager.CellInfoCallback() {
                        @Override
                        public void onCellInfo(@NonNull List<CellInfo> list) {
                          //Extract needed data
                        }
                    });
                }
            }, 1000, 1000 );



回答3:


// 1. Create a TelephonyManager instance 
telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);  
// 2. Define a CellInfoCallback callback
TelephonyManager.CellInfoCallback cellInfoCallback = new TelephonyManager.CellInfoCallback() {  
    @Override
    public void onCellInfo(List<CellInfo> cellInfo) {
        // DO SOMETHING
    } 
}
// 3. Now you can call the method to DO SOMETHING
telephonyManager.requestCellInfoUpdate(this.getMainExecutor(), cellInfoCallback);


来源:https://stackoverflow.com/questions/61075598/what-is-proper-usage-of-requestcellinfoupdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!