我有一个广播接收器类,用于识别电话呼叫状态,(传出,传入,CALL_STATE_OFFHOOK
,CALL_STATE_IDLE
)它适用于Android N和较低版本,但对于Android O,接收器可以& #39; t确定呼叫是否已结束,没有CALL_STATE_IDLE
状态,但正确识别出局,传入和CALL_STATE_OFFHOOK
状态
private fun startListening(context: Context) {
val telManager = context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
telManager?.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE)
}
private val phoneListener = object : PhoneStateListener() {
override fun onCallStateChanged(state: Int, incomingNumber: String) {
Log.e("CALL_STATE", "calling number $incomingNumber")
try {
when (state) {
TelephonyManager.CALL_STATE_RINGING -> {
Log.e("CALL_STATE", "CALL_STATE_RINGING")
}
TelephonyManager.CALL_STATE_OFFHOOK -> {
Log.e("CALL_STATE", "CALL_STATE_OFFHOOK")
}
TelephonyManager.CALL_STATE_IDLE -> {
Log.e("CALL_STATE", "CALL_STATE_IDLE")
}
else -> {
Log.e("CALL_STATE", "CALL_STATE_IDLE")
}
}
} catch (ex: Exception) {
ex.printStackTrace()
}
}
}
的Manifest.xml
<receiver android:name=".MyPhoneCallStateReceiver">
<intent-filter android:priority="989999">
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
我所发现的是,如果呼叫是传入的,它会正确识别CALL_STATE_IDLE
状态何时触发
我失踪了什么?