Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

kotlin - Android TelephonyManager requestCellInfoUpdate returning stale data on API 29

I've been encountering a problem for some time using Android's TelephonyManager. We use the TelephonyManager for phone signal live surveying purposes but in an attempt to move up to targeting API 29 it has become a problem. On API 28 and under, we just use TelephonyManager.allCellInfo to get all the cell info from the device and it works perfectly. However, if you target API 29 and use the same code above, the readings just sit there and never change. They would normally change every time you request, even if only slightly like a signal change from -98 to -99.

I discovered that on API 29, the process has to be a little different as explained in the Android docs. https://developer.android.com/reference/android/telephony/TelephonyManager#requestCellInfoUpdate(java.util.concurrent.Executor,%20android.telephony.TelephonyManager.CellInfoCallback)

The wording on the docs is a follows "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 as using TelephonyManager.allCellInfo no longer triggers a refresh of the data. Instead, I used TelephonyManager.requestCellInfoUpdate and used the callback. The code I used to solve this is below.
Note, this is Kotlin, but the same applies to Java.

val tm: TelephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

tm.requestCellInfoUpdate(context.mainExecutor, object : CellInfoCallback() {
    override fun onCellInfo(activeCellInfo: MutableList<CellInfo>) {
        for (cellInfo in activeCellInfo) {
            val timeStamp = cellInfo.timeStamp // This value determines the age of the data.
        }
    }
})

This almost solved the problem in that readings started to come through again, but upon further inspection, it appears the rate-limited part is now a new issue as the readings are most the time up to 10 seconds old which is very unhelpful when you're on the move. I can't find any more info about that rate-limit at all, other than it exists... It is making our software very unusable as a result. Currently, the only thing I can do to make this work well is to target API 28 but Google don't want that happening for much longer.

I also found some of the info on this thread quite useful getting the code above going. What is proper usage of requestCellInfoUpdate()?

Any help or advice would be greatly appreciated.

Henry

question from:https://stackoverflow.com/questions/65832152/android-telephonymanager-requestcellinfoupdate-returning-stale-data-on-api-29

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...