Hello Android developers, I am trying to make my app the default sms app on my phone with the following code but its not working as it is not displaying the dialog for a user to choose which app to use as the default sms app. I am aware that as of Android 10 then we need to use the RoleManager class to check if the app is the default sms app and additionally check if the app holds the role and then start an activity for the user to set the current app to have that role. I have followed every required step from the documentation and some previously posted questions on this site but there seems to be an error that is silent because no exception is thrown. Help me make this code display the dialog to the user for changing the app for the sms roles.
//register a delegate for reading the phone number
button.Click += (o, p) =>
{
Toast.MakeText(this,Telephony.Sms.GetDefaultSmsPackage(ApplicationContext),ToastLength.Long).Show();
//check first if the app is the default sms privilege holder
if (Telephony.Sms.GetDefaultSmsPackage(ApplicationContext) != PackageName)
{
//use the role manager api to change the default sms app
//check the api level
if (Build.VERSION.SdkInt > BuildVersionCodes.P)
{
Log.Debug("API", "api greater than 28");
//api level is greater than 28 then use RoleManager class
RoleManager roleManager = (RoleManager)GetSystemService(Context.RoleService);
//check if the role is available first
if (roleManager.IsRoleAvailable(RoleManager.RoleSms))
{
//log the role manager availability
Log.Debug("Sms role available", roleManager.IsRoleAvailable(RoleManager.RoleSms).ToString());
//chek if the role is held
if (roleManager.IsRoleHeld(RoleManager.RoleSms))
{
//means the app has the sms role and we do not need to change
}
else
{
Log.Debug("Info", "Sms Role is not held");
//means the app does not have the sms role and we need to request the user to change
Intent changeIntent = roleManager.CreateRequestRoleIntent(RoleManager.RoleSms);
StartActivityForResult(changeIntent,909);
}
}
else
{
//the role is not avaialble and cannot be changed
Log.Debug("Sms role available",
roleManager.IsRoleAvailable(RoleManager.RoleSms).ToString());
}
}
else
{
//api level is less than or equal to 28 use the deprecated way
Intent changeIntent = new Intent(Telephony.Sms.Intents.ActionChangeDefault);
changeIntent.PutExtra(Telephony.Sms.Intents.ExtraPackageName, PackageName);
StartActivity(changeIntent);
}
}
and here is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="SimCardManager.SimCardManager">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_PRECISE_PHONE_STATE"/>
<uses-permission android:name="android.permission.USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER"/>
<application android:allowBackup="true" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
</application>
</manifest>
An answer in Android Java is also accepted as that is what I use to convert Java code to C#, the code is still Android Java working as a Xamarin Android C# binding, Thank You for your time and effort helping me.