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 ?
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.
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.
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)
}
}
}
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!!!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With