6

Scenario: We are sending SMS to particular number if user grantees the SMS permission.

Devices: Only Samsung devices with Android 11.

Code:

SmsManager.getSmsManagerForSubscriptionId(subscriptionId)
.sendTextMessage(destinationAddress, null, getString(R.string.xyz)
.format(token), sendSMSPendingIntent, null)

Exception:

java.lang.SecurityException: Package com.android.phone (uid=1001) does not match provided uid 10246

android.os.Parcel.createExceptionOrNull Parcel.java:2385
android.os.Parcel.createException Parcel.java:2369
android.os.Parcel.readException Parcel.java:2352
android.os.Parcel.readException Parcel.java:2294
com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber ISms.java:2102
android.telephony.SmsManager$1.onSuccess SmsManager.java:618
android.telephony.SmsManager.sendResolverResult SmsManager.java:1627
android.telephony.SmsManager.resolveSubscriptionForOperation SmsManager.java:1588
android.telephony.SmsManager.sendTextMessageInternal SmsManager.java:613
android.telephony.SmsManager.sendTextMessage SmsManager.java:451```
Hiren Patel
  • 52,124
  • 21
  • 173
  • 151

2 Answers2

4

You have to check the airplane mode status and mobile network status before trying to send sms.

Skip send sms call and show error message if airplane mode is enable or signal strength is zero.

Check airplane mode:

public static boolean isAirplaneModeOn(Context context) {
       return Settings.System.getInt(context.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}

check mobile network

@RequiresApi(api = Build.VERSION_CODES.P)
public static boolean isNoSignalStrength(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
    return telephonyManager.getSignalStrength().getLevel() == 0;
}

I got the solution from this link https://askandroidquestions.com/2021/08/04/samsung-android-11-java-lang-securityexception-package-com-android-phone-uid1001-does-not-match-provided-uid-10571/?unapproved=6705&moderation-hash=ad920e86b8ab19ada9b29f39694490d5#comment-6705

Happy Mittal
  • 109
  • 7
  • Is this a common issue with Samsung devices? The URL you provided is not currently working. – Bink Apr 23 '22 at 13:56
  • Yes, Samsung updated os code for android 11 or higher. Other OEM devices don't throw any exception on send sms failure due to network strength. I am not sure why url is not working but you can try above code to fix the problem. – Happy Mittal Apr 24 '22 at 16:57
2

Found the root cause.

When SIM card is inactive then device may throw the Security exception.

Solution: Just replace with working SIM card.

Hope it helps.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151