.NET MAUI如何实现CallScreeningService?

o8x7eapl  于 2023-10-21  发布在  .NET
关注(0)|答案(1)|浏览(113)

我想实现CallScreeningService,以便在呼叫者振铃时知道其电话号码。我已经遵循了这个文档CallScreeningService。文档说,应用程序必须请求呼叫筛选角色。但是当我在MainActivity.cs中请求角色时,结果是Canceled。
MainActivity.cs

public class MainActivity : MauiAppCompatActivity
{
    public static MainActivity ActivityCurrent { get; set; }
    private static int REQUEST_ID = 1;

    public MainActivity()
    {
        ActivityCurrent = this;
    }
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        RoleManager roleManager = (RoleManager)GetSystemService(RoleService);
        Intent intent = 
        roleManager.CreateRequestRoleIntent(RoleManager.RoleCallScreening);
        StartActivityForResult(intent, REQUEST_ID);
    }
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent 
    data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ID)
        {
            if (resultCode == Result.Ok)
            {
            }
            else
            {
                // The resultCode is always Canceled
            }
        }

    }
}

IncomingCallService.cs(如何注册此服务?)

public class IncomingCallService : CallScreeningService
{
    public override void OnScreenCall(Call.Details callDetails)
    {
        var handle = callDetails.GetHandle();
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/appicon" 
    android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
      <uses-permission android:name="android.permission.READ_CALL_LOG" />
      <uses-permission android:name="android.permission.BIND_SCREENING_SERVICE" />
</manifest>
hc2pp10m

hc2pp10m1#

您是否错过了IncomingCallService的属性?下面是我的代码:

[Service( Exported = true, Permission = "android.permission.BIND_SCREENING_SERVICE")]
 [IntentFilter(new string[] { "android.telecom.CallScreeningService" })]
 public class IncomingCallService : CallScreeningService
 {
     public override void OnScreenCall(Call.Details callDetails)
     {
         var handle = callDetails.GetHandle();
     }
 }

以及应用程序启动时的请求对话框:

我得到的请求代码:

更新:

调试结果:

更新2:

单击...,然后单击Call Device

相关问题