当应用程序在后台运行时,广播接收器并不总是被调用

时间:2018-12-30 14:16:20

标签: android broadcastreceiver

我制作了一个必须注册来自其他电话的来电的应用程序,并且该程序必须在后台运行。我创建了BroadcastReceiver,我只是在其中启动了一个前景IntentService,该前景必须向服务器发送一些信息。问题是当我的应用程序在后台运行时,广播接收器并不总是触发。我正在使用Android 8 Oreo测试此应用。这是我的manifest文件的一部分

<receiver
                android:name=".receiver.CallReceiver"
                android:enabled="true">
            <intent-filter android:priority="99">
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter>
            <intent-filter android:priority="100">
                <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
            </intent-filter>
        </receiver>

        <service
                android:name=".ForegrounCustomService"
                android:enabled="true"
                android:exported="true">
        </service>

这里是绝对类PhoneCallReceiver

abstract class PhoneCallReceiver: BroadcastReceiver() {

    @SuppressLint("MissingPermission")
    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent == null || context == null) return

        if (intent.action != Intent.ACTION_NEW_OUTGOING_CALL) {
            val stateString = intent.extras?.getString(TelephonyManager.EXTRA_STATE) ?: return
            val number = intent.extras?.getString(TelephonyManager.EXTRA_INCOMING_NUMBER) ?: return

            val state = when (stateString) {
                TelephonyManager.EXTRA_STATE_IDLE -> TelephonyManager.CALL_STATE_IDLE
                TelephonyManager.EXTRA_STATE_OFFHOOK -> TelephonyManager.CALL_STATE_OFFHOOK
                TelephonyManager.EXTRA_STATE_RINGING -> TelephonyManager.CALL_STATE_RINGING
                else -> TelephonyManager.CALL_STATE_IDLE
            }
            onCallStateChanged(context, state, number)
        }
    }

    private fun onCallStateChanged(context: Context, state: Int, number: String) {
        when (state) {
            TelephonyManager.CALL_STATE_RINGING -> isIncommingCall = true
            TelephonyManager.CALL_STATE_OFFHOOK -> {
                isIncommingCall = lastState == TelephonyManager.CALL_STATE_RINGING
                if (isIncommingCall && !SharedPreferencesManager.wasCallAnswered) {
                    SharedPreferencesManager.wasCallAnswered = true
                    onIncomingCallAnswered(context, number)
                }
            }
            TelephonyManager.CALL_STATE_IDLE -> {
                if (SharedPreferencesManager.wasCallAnswered) {
                    SharedPreferencesManager.wasCallAnswered = false
                    onIncomingCallEnded(context, number)
                }
            }
        }
        lastState = state
    }

    protected abstract fun onIncomingCallAnswered(context: Context, number: String)
    protected abstract fun onIncomingCallEnded(context: Context, number: String)

    companion object {
        private var isIncommingCall = false
        private var lastState = TelephonyManager.CALL_STATE_IDLE
    }
}

这是CallReceiver

class CallReceiver: PhoneCallReceiver() {

    override fun onIncomingCallAnswered(context: Context, number: String) {
        ContextCompat.startForegroundService(context, ForegrounCustomService.createIntent(context, number, START))
    }

    override fun onIncomingCallEnded(context: Context, number: String) {
        ContextCompat.startForegroundService(context, ForegrounCustomService.createIntent(context, number, STOP))
    }

    companion object {
        const val START = "start"
        const val STOP = "stop"
    }
}

那么我想念的是什么? (所有权限均被授予)

0 个答案:

没有答案