gpt4 book ai didi

android - Nougat 上未调用 TelephonyManager 的 PhoneStateListener

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:21 28 4
gpt4 key购买 nike

我制作了一个示例应用程序和 PhoneStateListener

TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
telephony.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.i("brian", "call state = " + state + " incoming number " + incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:

break;
case TelephonyManager.CALL_STATE_OFFHOOK:

break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager

Log.i("brian", "READ_PHONE_STATE = " + ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE));

按预期工作,但当我将上述代码添加到我的更大的应用程序时,onCallStateChanged 仅在最初订阅时调用。没有电话状态变化通知我。在这两个项目中,底部日志行“READ_PHONE_STATE =”始终被授予,我的目标是 sdk 22,所以我认为没有运行时权限。在我较大的应用程序中,我将代码粘贴到主要 Activity 和长期服务中,都没有获得状态更改事件。当我在 android < 7.0 上运行我的代码时,它们都可以工作,我不知道为什么。在警告或错误日志中看不到任何实质内容。

最佳答案

正如 Vairavan 在 this 中提到的那样回答,PhoneStateListener 的引用方式发生了内部变化:

Local reference to PhoneStateListener is kept track internally only by a weak reference. This makes it eligible for garbage collection upon function exit and once the listener is finalized, apps will not receive any further updates. Solution is to keep a reference to PhoneStateListener via class member variable.

参见:https://github.com/aosp-mirror/platform_frameworks_base/commit/f5d7c587e86c64e657a15a12ad70e75d47c48d99#diff-5af2ac899de823cf60597e554bf67fe0 .

关于android - Nougat 上未调用 TelephonyManager 的 PhoneStateListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40183905/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com