注册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
相关产品推荐
相关产品推荐

