简体   繁体   中英

No response from intent to prompt user to set default handler for SMS

I'm currently having trouble getting the app to send the user a dialogue to set the default handler for SMS on API 29.

I initially followed https://developer.android.com/guide/topics/permissions/default-handlers .

It didn't work so I looked around and saw this: How to set Default SMS prompt for KitKat .

So I followed https://android-developers.googleblog.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html and added the snippet to my manifest XML (without actual implementation, as the StackOverflow user said in the post). Unfortunately every time I click the button that starts the ACTION_CHANGE_DEFAULT intent, nothing happens except he log.

My main activity has the following onCreate:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var btn = findViewById<Button>(R.id.button3)
        btn.setOnClickListener {
            Log.d("TAG", "Sending change default intent")
            val setSmsAppIntent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
            setSmsAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName)
            startActivityForResult(setSmsAppIntent, CHANGE_SMS_DEFAULT_RESULT)
        }
    }

Manifest:

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

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

    <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/AppTheme">
        <service
            android:name=".FetchSMS"
            android:exported="false"></service>

        <activity
            android:name=".DisplayMessageActivity"
            android:parentActivityName=".MainActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- These are ONLY used to set app as default SMS -->
        <receiver android:name=".SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>

        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver android:name=".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>

        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".ComposeSmsActivity" >
            <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" />
            </intent-filter>
        </activity>

        <!-- Service that delivers messages from the phone "quick response" -->
        <service android:name=".HeadlessSmsSendService"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
            android:exported="true" >
            <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>

Log:

在此处输入图像描述

Would appreciate some help. Thanks in advance.

My minSdkVersion is 19

Edit: I just tried this on the emulator on KitKat (API 19) and it worked - but it doesn't on Q (API 29)

Try below piece of code.

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            RoleManager roleManager = mContext.getSystemService(RoleManager.class);
            // check if the app is having permission to be as default SMS app
            boolean isRoleAvailable = roleManager.isRoleAvailable(RoleManager.ROLE_SMS);
            if (isRoleAvailable){
                // check whether your app is already holding the default SMS app role.
                boolean isRoleHeld = roleManager.isRoleHeld(RoleManager.ROLE_SMS);
                if (isRoleHeld){
                    Intent roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
                    startActivityForResult(roleRequestIntent,requestCode);
                }
            }
        } else {
            Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, defaultSystemApp);
            startActivityForResult(intent, requestCode);
        }

Add this permission is manifest

            <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>

I had to modify Praveen's code to make it work. Main difference is with if(isRoleHeld). Here is how it look like.

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
        RoleManager roleManager = getApplicationContext().getSystemService(RoleManager.class);
        // check if the app is having permission to be as default SMS app
        assert roleManager != null;
        boolean isRoleAvailable = roleManager.isRoleAvailable(RoleManager.ROLE_SMS);
        if (isRoleAvailable){
            // check whether your app is already holding the default SMS app role.
            boolean isRoleHeld = roleManager.isRoleHeld(RoleManager.ROLE_SMS);
            if (!isRoleHeld){
                Intent roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
                startActivityForResult(roleRequestIntent,resultCode);
            }
        }
    } else {
        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getApplicationContext().getPackageName());
        startActivityForResult(intent, resultCode);
    }

try this code it works fine with me

    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.Q){
        //String mypackagename = getPackageName();
        if(Telephony.Sms.getDefaultSmsPackage(this)!=null){
            if (Telephony.Sms.getDefaultSmsPackage(this).equals(getPackageName())){
                //todo go nain activity
            }else{
                Intent setSmsAppIntent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
                setSmsAppIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,getPackageName());
                startActivityForResult(setSmsAppIntent, 1);
            }
        }else{toast("no tlphony");}
    }
    else{
        RoleManager rolemanager = getApplicationContext().getSystemService(RoleManager.class);
        if (rolemanager.isRoleAvailable(RoleManager.ROLE_SMS)){
            if (rolemanager.isRoleHeld(RoleManager.ROLE_SMS)){
                //todo go nain activity
            }
            else{
                Intent roleRequestIntent = rolemanager.createRequestRoleIntent(RoleManager.ROLE_SMS);
                startActivityForResult(roleRequestIntent,1);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM