【发布时间】: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