我在片段中从PhoneStateListener获取回调有问题。
这是设置:
class StatusBarFragment {
private lateinit var telephonyManager: TelephonyManager
val phoneStateListener: PhoneStateListener = object : PhoneStateListener() {
override fun onServiceStateChanged(serviceState: ServiceState) {
Timber.d("Current cell state : ${serviceState.state}")
}
override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Timber.d("Current cell signal: ${signalStrength.level}")
} else {
Timber.d("Current cell signal:${signalStrength.gsmSignalStrength}")
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE or PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
}
}
不调用日志(甚至一次)。可能是什么问题?