0

我正在使用 Kotlin 构建一个 Android 应用程序。我正在尝试将我的应用程序构建为默认 SMS 处理程序,因为它要求 SMS 权限,因此我需要确保它暗示 Google PlayStore 的政策。但它没有显示任何弹出窗口或对话框来提示用户将应用程序设置为默认 SMS 处理程序。这是我的代码。

val setSmsAppIntent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
        setSmsAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName)
        startActivityForResult(setSmsAppIntent, DEFAULT_SMS_HANDLER_REQUEST)

当我运行我的代码时,它没有显示任何内容。我的代码有什么问题,我该如何解决?

我也试过这个。

 private fun askDefaultSmsHandlerPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val roleManager: RoleManager = getSystemService(RoleManager::class.java)
            // check if the app is having permission to be as default SMS app
            val isRoleAvailable = roleManager.isRoleAvailable(RoleManager.ROLE_SMS)
            if (isRoleAvailable) {
                // check whether your app is already holding the default SMS app role.
                val isRoleHeld = roleManager.isRoleHeld(RoleManager.ROLE_SMS)
                if (isRoleHeld) {
                    val roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS)
                    startActivityForResult(roleRequestIntent, DEFAULT_SMS_HANDLER_REQUEST)
                }
            }
        } else {
            val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName)
            startActivityForResult(intent, DEFAULT_SMS_HANDLER_REQUEST)
        }
    }

它也不起作用。

这是我的 andoridmanifest.xml

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

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <dist:module dist:instant="true" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" android:protectionLevel="signature" />
    <uses-permission android:name="android.permission.RECEIVE_MMS" />
    <uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:name=".AppController"
        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.22222Com">
        <activity android:theme="@style/Theme.22222Com.NoActionBar" android:name=".DisableVPNActivity" android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:theme="@style/Theme.22222Com.NoActionBar" android:name=".MainActivity" android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.app.role.SMS"/>
            </intent-filter>
        </activity>

        <activity android:label="@string/register" android:name=".RegisterActivity" android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/messages_activity_title" android:name=".MessageListActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/send_message_activity_title" android:name=".SendMessageActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:noHistory="true" android:label="@string/message_details_activity_title" android:name=".MessageDetailsActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:noHistory="true" android:label="@string/reply_message_activity_title" android:name=".ReplyMessageActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/confirm_message_activity_title" android:name=".ConfirmMessageActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/client_list_activity_title" android:name=".ClientListActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/update_location_activity_title" android:name=".UpdateLocationActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/permission_usage_activity_title" android:name=".PermissionUsageActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/enable_location_activity_title" android:name=".EnableLocationActivity" android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/notice_activity_title" android:name=".FakeNewsWarningActivity" android:noHistory="true">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/data_sync_activity_title" android:name=".DataSyncSwitchActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/conversation_list_activity_title" android:name=".ConversationListActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:noHistory="true" android:label="@string/add_new_client_activity_title" android:name=".AddNewClientActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <activity android:label="@string/choose_message_type_activity_title" android:name=".ChooseOutgoingMessageTypeActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:label="@string/send_ordinary_message_activity_title" android:name=".SendOrdinaryMessageActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

        <receiver android:name=".RegisterReceiversScheduler">
            <intent-filter>
                <action android:name="com.forkthecoup.com22222.RegisterReceiversScheduler" ></action>
            </intent-filter>
        </receiver>

        <receiver android:name=".ShareNewsRemainder">
            <intent-filter>
                <action android:name="com.forkthecoup.com22222.ShareNewsRemainder" ></action>
            </intent-filter>
        </receiver>

        <service android:name="com.android.mms.transaction.TransactionService" />

        <receiver android:name=".SmsListener" android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="com.forkthecoup.com22222.SmsListener" ></action>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>

<!--        <receiver-->
<!--            android:name=".messaging.SmsReceiver"-->
<!--            android:permission="android.permission.BROADCAST_SMS">-->
<!--            <intent-filter>-->
<!--                <action android:name="android.provider.Telephony.SMS_DELIVER" />-->
<!--            </intent-filter>-->
<!--        </receiver>-->

        <receiver
            android:name=".messaging.MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="com.klinker.android.send_message.MmsSentReceiver"
            android:taskAffinity="com.klinker.android.messaging.MMS_SENT"/>

        <receiver
            android:name=".messaging.MmsReceivedReceiverImpl"
            android:taskAffinity="com.klinker.android.messaging.MMS_RECEIVED"/>

        <service
            android:name=".messaging.HeadlessSmsSendService"
            android:exported="true"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" >
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>

    </application>
</manifest>
4

1 回答 1

0

在您的代码中

// check whether your app is already holding the default SMS app role.
val isRoleHeld = roleManager.isRoleHeld(RoleManager.ROLE_SMS)
if (isRoleHeld) {
                 ...
                }

角色不能被持有,所以将 if 条件更改为

if (!isRoleHeld) {
...
}
于 2021-04-27T22:26:52.960 回答