I'm trying to write an app that sends SMS. When checking if I have the required permissions it returns true but still crashes with SecurityException.
When button is pressed
private void startAutoMsg() {
Log.d("Starting Auto Msg");
//FIXME: Start proper loop
if (canSendSms()) {
sendMessage();
mAutoMsgButton.setBackgroundColor(Color.GREEN);
}
}
I'm using the below function to determine if I have the proper permission
private boolean canSendSms() {
if ((ContextCompat.checkSelfPermission(mContext, Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_GRANTED)) {
Log.d("Permission granted");
return true;
} else {
Log.d("Permission denied");
ActivityCompat.requestPermissions(
mActivity, new String[]{Manifest.permission.SEND_SMS}, 101);
return false;
}
}
The above code returns true and therefore SMS is trying to be sent with this
private void sendMessage() {
Log.d("sending message");
PendingIntent sentPI = PendingIntent.getBroadcast(
mContext, 0, new Intent(Constants.ACTION_SMS_SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(
mContext, 0, new Intent(Constants.ACTION_SMS_DELIVERED), 0);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
mSmsManager.sendTextMessage("mNumber", null, mText, sentPI, deliveredPI);
}
});
thread.run();
}
In my manifest:
<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.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />
Log from crash
D/SmsApp: TelephonyFragment: canSendSms(533): Permission granted
D/SmsApp: TelephonyFragment: sendMessage(222): sending message
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.phone, PID: 16736
java.lang.SecurityException: Sending SMS message: uid 1001 does not have android.permission.SEND_SMS.
at android.app.ContextImpl.enforce(ContextImpl.java:1727)
at android.app.ContextImpl.enforceCallingPermission(ContextImpl.java:1749)
at android.content.ContextWrapper.enforceCallingPermission(ContextWrapper.java:750)
at android.content.ContextWrapper.enforceCallingPermission(ContextWrapper.java:750)
at com.android.internal.telephony.IccSmsInterfaceManager.sendText(IccSmsInterfaceManager.java:410)
at com.android.internal.telephony.UiccSmsController.sendTextForSubscriber(UiccSmsController.java:136)
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:366)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:349)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment$2.run(TelephonyFragment.java:230)
at java.lang.Thread.run(Thread.java:764)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment.sendMessage(TelephonyFragment.java:233)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment.startAutoMsg(TelephonyFragment.java:517)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment.toggleAutoMsg(TelephonyFragment.java:507)
at com.rawinc.smsapp.ui.telephony.TelephonyFragment.lambda$-com_rawinc_smsapp_ui_telephony_TelephonyFragment_11555(TelephonyFragment.java:359)
at com.rawinc.smsapp.ui.telephony.-$Lambda$uKVldJdEkN_fZa3QWm3EZHDa2r8$2.$m$0(Unknown Source:4)
at com.rawinc.smsapp.ui.telephony.-$Lambda$uKVldJdEkN_fZa3QWm3EZHDa2r8$2.onClick(Unknown Source:0)
at android.view.View.performClick(View.java:6178)
at android.view.View$PerformClick.run(View.java:24416)
at android.os.Handler.handleCallback(Handler.java:769)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:255)
at android.app.ActivityThread.main(ActivityThread.java:6555)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)