Issue
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.
Solution
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
Answered By - Jeffrey Blattman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.