【问题标题】:How to detect Incoming call number and Out going call number when app is killed OR not in background Android 8.0,9.0当应用程序被杀死或不在后台Android 8.0、9.0时如何检测来电号码和去电号码
【发布时间】:2020-08-03 10:43:35
【问题描述】:

我正在开发一个应用程序,我的要求是存储所有来电和去电详细信息,例如号码、持续时间、时间

我为此使用广播接收器以及运行时权限 READ_PHONE_STATE,READ_CALL_LOG

使用当前代码,应用程序在前台和后台运行正常,但是当我杀死应用程序时,它不工作,它没有检测传入/传出呼叫。

下面是我的清单文件代码

 <receiver
        android:name=".utils.CallReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter >
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.HOME" />

            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

广播接收器

 override fun onReceive(context: Context?, intent: Intent) {

    //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
    if (intent.action == "android.intent.action.NEW_OUTGOING_CALL") {
        savedNumber = intent.extras!!.getString("android.intent.extra.PHONE_NUMBER")
    } else {
        val stateStr =
            intent.extras!!.getString(TelephonyManager.EXTRA_STATE)
        val number =
            intent.extras!!.getString(TelephonyManager.EXTRA_INCOMING_NUMBER)
        var state = 0
        if (stateStr == TelephonyManager.EXTRA_STATE_IDLE) {
            state = TelephonyManager.CALL_STATE_IDLE
        } else if (stateStr == TelephonyManager.EXTRA_STATE_OFFHOOK) {
            state = TelephonyManager.CALL_STATE_OFFHOOK
        } else if (stateStr == TelephonyManager.EXTRA_STATE_RINGING) {
            state = TelephonyManager.CALL_STATE_RINGING
        }
        if (number != null && !number.isEmpty() && !number.equals("null")) {
            onCallStateChanged(context, state, number);
            Log.d("TEST :","NUMBER =>"+number);
            return;
        }


    }

我需要能够在应用程序被终止时检测来电的解决方案,就像真正的呼叫者应用程序一样,并且希望在发生呼叫时在 Android 7、8、9 上启动接收器

【问题讨论】:

    标签: java android kotlin broadcastreceiver telephonymanager


    【解决方案1】:

    当用户通过“FORCE CLOSE”杀死某个应用时,该应用的所有广播接收器都会立即暂停,并且系统会阻止它们接收未来的广播,直到用户手动重新打开该应用。

    这是为了防止应用程序在明显希望该应用程序关闭的用户强制关闭时工作,而传入的广播接收器可以再次唤醒该应用程序。

    see here

    此规则有一个例外 - 如果应用程序已设置为默认电话/短信应用程序,它仍然能够在接到电话/短信时唤醒。 我假设 TrueCaller 被设置为默认处理程序,以便能够解决此限制。

    要成为 Android P 及以下版本的默认电话处理程序 - 请参阅此处的文档:https://developer.android.com/reference/android/telecom/TelecomManager#ACTION_CHANGE_DEFAULT_DIALER

    在 Android Q 上,调用此方法:https://developer.android.com/reference/android/app/role/RoleManager#createRequestRoleIntent(java.lang.String)ROLE_DIALER

    【讨论】:

    • 感谢您的回复,您能告诉我如何设置默认电话/短信应用吗?
    • 添加到我的回答中
    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多