0

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.

TechGeek
  • 316
  • 1
  • 11
  • To be eligible to be selected as the default, your app needs a few specific components listed in the manifest: https://stackoverflow.com/a/30133663. – Mike M. Jul 14 '22 at 12:05
  • Okay, is that why its not showing the dialog? – TechGeek Jul 14 '22 at 12:56
  • That's at least one reason why, yes. Everything else looks OK, at a quick glance. – Mike M. Jul 14 '22 at 15:30
  • I added the components to the manifest but somehow the system cannot figure out that the app should need sms role – TechGeek Jul 15 '22 at 05:22
  • I'm not sure what you mean, exactly. The only thing the system looks at is your app's manifest, so if you mean that your code is getting to the `StartActivityForResult()` call but still not showing the dialog, then it sounds like the manifest might still be off. Have you tested it on older versions yet? That is, Android P or below? – Mike M. Jul 15 '22 at 16:54
  • Hey mike it worked and thanks, I mnaged to make my app the default sms role holder so it can read device IMEI but cannot read phone number, should I use TelcomManager to read the number? – TechGeek Jul 16 '22 at 07:10

0 Answers0