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

Xamarin.Forms使用CallRedirectionService抛出ClassNotFoundException如何解决

问题原因

CallRedirectionService 是 Android 10(API Level 29)才新增的系统API,而你用到的InCallService从Android 6.0(API Level 23)就已经存在,所以后者可以正常运行,前者会报类找不到异常。

解决步骤

  • 首先确认项目编译配置:Xamarin.Android项目的**目标框架(Target Framework)**必须设置为API 29及以上,才能正确引用该类的绑定接口。
  • 确认运行环境:测试设备/模拟器的系统版本必须为Android 10及以上,低于该版本的系统本身没有内置这个类,运行必然触发异常。如果需要兼容低版本设备,需要在代码里加版本判断,仅当系统版本 >= Android 10时才调用相关逻辑。
  • 修正服务注册逻辑:你不能在MainPage的构造函数中手动new MyCallService实例,Android系统服务由系统统一管理,需要在代码或者AndroidManifest.xml中注册才能被系统识别:
    方式一:给Service类加上特性直接配置
    using Android.Telecom;
    using Android.App;
    using Android.Content;
    
    namespace xam
    {
        [Service(Name = "com.yourpackagename.MyCallService", Permission = "android.permission.BIND_CALL_REDIRECTION_SERVICE")]
        [IntentFilter(new[] { "android.telecom.CallRedirectionService" })]
        public class MyCallService : CallRedirectionService
        {
            public override void OnPlaceCall(Android.Net.Uri handle, PhoneAccountHandle initialPhoneAccount, bool allowInteractiveResponse)
            {
                //业务逻辑
            }
        }
    }
    
    方式二:在AndroidManifest.xml中添加注册配置
    <service android:name="crc64bf934172c93e128d.MyCallService"
             android:permission="android.permission.BIND_CALL_REDIRECTION_SERVICE">
        <intent-filter>
            <action android:name="android.telecom.CallRedirectionService" />
        </intent-filter>
    </service>
    
  • 完成权限申请:使用该服务需要动态申请电话相关权限,同时需要引导用户将你的应用设置为默认的电话辅助应用,否则系统不会触发你的自定义服务逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 01:36:03