-1

I want to send sms but it's not allow permission.

Error is

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

plz help me to fix it.

protected void sendMessage(String number , String msh)
    {
        try {

            PendingIntent sentPi    = PendingIntent.getBroadcast(this,0,new Intent("sent"),0);
            PendingIntent deliver   = PendingIntent.getBroadcast(this,0,new Intent("delivered"),0);

            SmsManager smsManager   = SmsManager.getDefault();
            smsManager.sendTextMessage(number,null,msh,sentPi,deliver);

        }
        catch (Exception e){
             Log.d("SMS_k",e.toString());
        }
    }
Aksen P
  • 4,564
  • 3
  • 14
  • 27
soraya
  • 11
  • 5
  • 1
    Add this "android.permission.SEND_SMS" permission to your Manifest.xml – Sanwal Singh Aug 08 '19 at 06:48
  • Possible duplicate of [java.lang.SecurityException: Sending SMS message: uid 10051 does not have android.permission.SEND\_SMS](https://stackoverflow.com/questions/41395680/java-lang-securityexception-sending-sms-message-uid-10051-does-not-have-androi) – isaaaaame Aug 08 '19 at 07:28
  • Thanks for respone. I added SEND_SMS,But didn't work. – soraya Aug 08 '19 at 07:32

1 Answers1

1

First of all you need to put this in your manifest.xml

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

Then you can do this to send the SMS

try{
    SmsManager smgr = SmsManager.getDefault();
    smgr.sendTextMessage("ANY NUMBER",null,"YOUR MESSAGE GOES HERE",null,null);
    Toast.makeText(MainActivity.this, "SMS Sent Successfully", Toast.LENGTH_SHORT).show();
}
catch (Exception e){
    Toast.makeText(MainActivity.this, "SMS Failed to Send", Toast.LENGTH_SHORT).show();
}

If you want to do it via Intent as you were trying to do you just need to add this :

Intent intent=new Intent(getApplicationContext(),YOURACTIVITY.class);  
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);  

try{
    SmsManager sms=SmsManager.getDefault();  
    sms.sendTextMessage("ANY NUMBER", null, "YOUR MESSAGE GOES HERE", pi,null);
}catch(Exception e){
    //Something wrong happened
}  
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • try catch is still necessary as tablets and devices without sim cards will still throw and exception when trying to do this. just a small FYI from someone who was burnt by a crash from tablets :D – Kushan Aug 08 '19 at 06:55