Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an sms app as default app in android programmatically

Tags:

android

I have developed an android app for receiving sms from a particular number . It can marks sms as read from that number . TO mark sms as read , the application needs to be set as default . How can I set an app as default in android programmatically ?

like image 624
osimer pothe Avatar asked Oct 01 '15 11:10

osimer pothe


People also ask

How do I make my SMS app my default app?

However, if this isn't working, there's another way to assign a message app as the default. Navigate to and open Settings, and then tap Apps. Tap Choose default apps, and then tap SMS app. Select your desired message app.

What is default SMS handler in Android?

For example, a default SMS handler should be able to send text messages. Your app must provide a privacy policy. Your app must make its core functionality clear in the Play Store description. For example, a default Phone handler should describe its phone-related capabilities in the description.


3 Answers

The old method will not run on android 10 these are the code you can use to request for becoming default app

Request to become default in android 10 For java developer

    public void makeMyAppDefaultRequest(String packageName){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        RoleManager roleManager =getSystemService(RoleManager.class);
        if (roleManager.isRoleAvailable(RoleManager.ROLE_SMS)) {
            if (roleManager.isRoleHeld(RoleManager.ROLE_SMS)) {
                requestPermission()
            } else {
                Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
                startActivityForResult(intent, 101);
            }
        } else {
            
        }
    } else {
        if (Telephony.Sms.getDefaultSmsPackage(this).equals(packageName)) {

        } else {
            Intent intent =new  Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName);
            startActivityForResult(intent, 101);
        }
    }
}

For Kotlin developer

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        val roleManager = getSystemService(RoleManager::class.java)
        if (roleManager!!.isRoleAvailable(RoleManager.ROLE_SMS)) {
            if (roleManager.isRoleHeld(RoleManager.ROLE_SMS)) {
                askPermissions()
            } else {
                val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS)
                startActivityForResult(intent, MAKE_DEFAULT_APP_REQUEST)
            }
        } else {
            toast(R.string.unknown_error_occurred)
            finish()
        }
    } else {
        if (Telephony.Sms.getDefaultSmsPackage(this) == packageName) {
            askPermissions()
        } else {
            val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName)
            startActivityForResult(intent, MAKE_DEFAULT_APP_REQUEST)
        }
    }
}
like image 76
Wajahat iqbal Waji Avatar answered Sep 20 '22 04:09

Wajahat iqbal Waji


As I understand you need to ask the user to set your app as the default messaging app while launching your App. To do that use below code:

Set below intent-filter to activity which you want to show. In this case I have considered "DefaultSMSAppChooserActivity"

<activity android:name=".DefaultSMSAppChooserActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Call below method to show all available messaging app on his/her device including yours to choose from as default.

public static void openSMSappChooser(Context context) {
    PackageManager packageManager = context.getPackageManager();
    ComponentName componentName = new ComponentName(context, DefaultSMSAppChooserActivity.class);
    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    Intent selector = new Intent(Intent.ACTION_MAIN);
    selector.addCategory(Intent.CATEGORY_APP_MESSAGING);
    selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(selector);

    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
}

You can use the above method anywhere as per your requirement.

Hope this helps you!!!

like image 7
Geek Avatar answered Oct 06 '22 13:10

Geek


You can't.

Third party developers cannot set their own app as default applications. Only the users of your app can do this on their own device.

Anyway you can register your app to act as a messaging application through intent-filter

like image 5
Magudesh Avatar answered Oct 06 '22 11:10

Magudesh