如何检测来电号码和外出呼叫号码时,应用程序被杀或不在后台安卓8.0,9.0



我正在开发一个应用程序,我的要求是存储所有传入和传出呼叫的详细信息,如号码、持续时间、时间

我使用广播接收器以及运行时权限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上启动接收器

当一个应用程序通过"强制关闭";用户会立即暂停应用程序的所有广播接收器,系统会阻止他们接收未来的广播,直到用户手动重新打开该应用程序。

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

参见此处

这个规则有一个例外——如果该应用程序被设置为默认的电话/短信应用程序,它仍然可以在接到电话/短信时醒来。我认为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

相关内容

最新更新