Android 电信系统角色管理器

时间:2021-05-25 03:22:13

标签: android android-service android-manifest android-permissions android-developer-api

我正在尝试实现调用应用程序。对于 API < 29,我可以选择我的拨号器应用程序作为默认拨号器。但是对于 API >= 29,我无法获得显示提示,也无法将我的应用程序设置为设置下的默认拨号程序。它没有显示为一个选项。但是它使用 API < 29。 这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ceptor">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Ceptor">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.CALL" />
                <action android:name="android.intent.action.CALL_BUTTON" />
                <action android:name="android.intent.action.ANSWER" />
                <action android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.category.BROWSABLE" />
                <action android:name="android.intent.action.VIEW" />
                <!-- The directives below this comment seem to be sufficient for API < 29 -->
                <action android:name="android.intent.action.DIAL" />
                <data android:scheme="tel" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.DIAL" />
                <!-- The directives above this comment seem to be sufficient for API < 29 -->
                <action android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.CALL" />
                <action android:name="android.intent.action.CALL_BUTTON" />
                <action android:name="android.intent.action.ANSWER" />
            </intent-filter>

        </activity>

        <receiver android:name=".CeptorBootCompleteReceiver" android:directBootAware="true" >
            <intent-filter>
                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

        <service android:name=".CeptorConnectionService"
            android:label="The_label_for_my_connection_service"
            android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE">
            <intent-filter>
                <action android:name="android.telecom.ConnectionService" />

            </intent-filter>
        </service>


    </application>

</manifest>

对于 API < 29,似乎只需要 DIAL 和 DEFAULT。

这里是我在 TelecomManager 注册我的东西的地方。

        // change from com.google.android.dialer

        if (Build.VERSION.SDK_INT >= 29)
        {
            RoleManager rm = (RoleManager) getSystemService(Context.ROLE_SERVICE);
            Intent intent = rm.createRequestRoleIntent(RoleManager.ROLE_DIALER);
            startActivityForResult(intent, 1);
        }
        else
        {
            Intent intent = new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER);
            intent.putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, this.getPackageName());
            startActivity(intent);
        }


        TelecomManager tm = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);

        // Create a new PhoneAccountHandle with the name "CeptorHandle" which uses my CeptorConnectionService.
        ComponentName cn = new ComponentName("com.example.ceptor", "com.example.ceptor.CeptorConnectionService");
        PhoneAccountHandle pah = new PhoneAccountHandle(cn,"CeptorHandle");

        
        // Create a new phone account which is associated with the PhoneAccountHandle "CeptorHandle".
        final int capabilities =    PhoneAccount.CAPABILITY_CALL_PROVIDER |
                                    PhoneAccount.CAPABILITY_CONNECTION_MANAGER;

        PhoneAccount pa = PhoneAccount.builder(pah, "CeptorHandle").setCapabilities(capabilities).build();

    }

所以我们在这里看到我将 ROLE_SERVICE 和 ROLE_DIALER 用于我的 RoleManager。 但是,我从来没有得到提示。从这里阅读它:

https://proandroiddev.com/android-q-to-the-res-q-9a88733aedda

我看到它说:

<块引用>

为了让 RoleManager 正确解析请求,您的清单必须指定类别、权限或其他意图过滤器,以便正确持有角色。如果您不这样做,请求将被自动取消

但即使在官方 Android 文档中,我也没有看到角色 ROLE_DIALER 的特定类别、权限或其他意图过滤器的确切列表。

https://developer.android.com/reference/androidx/core/role/RoleManagerCompat#ROLE_DIALER

我认为 API 之间的区别在于较旧的 API 使用 TelecomManager 的 Intent,而较新的 API 使用这个新的 RoleManager。我想就是这样,但也许我离题了。

总之……为什么这对 API < 29 有效,但在其他情况下无效?

1 个答案:

答案 0 :(得分:0)

<action android:name="android.intent.category.DEFAULT" />

应该是

<category android:name="android.intent.category.DEFAULT" />