2

I'm trying to send a message (SMS) from one emulator to another:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="eheio.com.exo2">

    <uses-permission android:name="android.permission.SEND_SMS"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Send message method:

public void sendMessage(View view) {
        EditText number = findViewById(R.id.number);
        EditText message = findViewById(R.id.message);
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(number.getText().toString(), null, message.getText().toString(), null, null);
            Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS failed, please try again later!", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

I'm getting the following exception:

java.lang.SecurityException: Sending SMS message: uid 10082 does not have android.permission.SEND_SMS.

Ayoub k
  • 7,788
  • 9
  • 34
  • 57
  • 1
    if you are targeting API23 and above It is a dangerous permission, it requires runtime permission request. – Mark Oct 12 '18 at 23:47

2 Answers2

3

For starters, you can inspect what permissions are granted to your apps by doing:

adb shell dumpsys package <your package>

and you'll see something like:

grantedPermissions:
  android.permission.MANAGE_ACCOUNTS
  android.permission.WRITE_SYNC_SETTINGS
  android.permission.RECEIVE_BOOT_COMPLETED

The android.permission.SEND_SMS permission is dangerous protection level. That means as of API 23 you need to prompt the user to accept the permission. It's not enough to declare its use in the manifest. You can read about that here: https://developer.android.com/guide/topics/permissions/overview#dangerous-permission-prompt

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
1

The android.permission.SEND_SMS is dangerous protection level. So It's not enough to declare <uses-permission android:name="android.permission.SEND_SMS"/> in the manifest. So, above API level 23 you need to prompt the user to accept the permission, when the app is running.

You can try the following code:

SmsManager smsManager = SmsManager.getDefault();
ActivityCompat.requestPermissions(this,new String[] { Manifest.permission.SEND_SMS}, 1);
smsManager.sendTextMessage(strPhoneNo, null, strMessage, null, null);
Thomas Hirsch
  • 2,102
  • 3
  • 19
  • 39