You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

注册CallStateListener时出现错误权限弹窗问题求助

问题:应用监听来电状态却被系统识别为通话管理应用

我的应用需求是手机接到来电时自动停止音频处理,但启动应用后,Android系统错误地判定该应用想要发起和管理通话(如图所示)。


初始化权限请求与回调注册代码

// 在构造方法中
requestPermissionLauncher = activity.registerForActivityResult(
    new ActivityResultContracts.RequestPermission(), 
    this::registerTelephonyCallback);

// 初始化阶段
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    if(ActivityCompat.checkSelfPermission(activity.getContext(), Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED){
        registerTelephonyCallback(true);
    } else {
        requestPermissionLauncher.launch(Manifest.permission.READ_PHONE_STATE);
    }
} else {
    //noinspection deprecation
    // 使用已废弃的 TelephonyManager.listen()
}

registerTelephonyCallback() 方法实现

private void registerTelephonyCallback(boolean phoneStateCanBeRead){
    if (!phoneStateCanBeRead){
        return;
    }
    TelephonyManager telephonyManager = (TelephonyManager) activity.getContext().getSystemService(Context.TELEPHONY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        Executor mainExecutor = ContextCompat.getMainExecutor((Activity)activity);
        telephonyManager.registerTelephonyCallback(INCLUDE_LOCATION_DATA_NONE, mainExecutor, new PhoneCallback());
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        Executor mainExecutor = ContextCompat.getMainExecutor((Activity)activity);
        telephonyManager.registerTelephonyCallback(mainExecutor, new PhoneCallback());
    }
}

TelephonyCallback 实现类

@RequiresApi(api = Build.VERSION_CODES.S)
private class PhoneCallback extends TelephonyCallback implements TelephonyCallback.CallStateListener {
    @Override
    public void onCallStateChanged(int state) {
        if (!audioServiceIsRegistered()) {
            return;
        }
        if (state == TelephonyManager.CALL_STATE_IDLE) {
            if (isExternallyPaused) {
                playAudio();
            }
            isExternallyPaused = false;
        } else {
            pauseAudio();
            isExternallyPaused = true;
        }
    }
}

内容的提问来源于stack exchange,提问作者Residuum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 01:38:15