尽管在本地进行了声明,但尚未调用PhoneStateListener回调

时间:2018-10-01 14:34:23

标签: android kotlin

我在片段中从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)
    }
}

不调用日志(甚至一次)。可能是什么问题?

0 个答案:

没有答案