Do not send SMS android studio

1

I'm trying to do SMS sending tests but it does not send anything.

  

Permissions

if (ContextCompat.checkSelfPermission(Menu_AgroMovil.this,
                Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
   if (ActivityCompat.shouldShowRequestPermissionRationale(Menu_AgroMovil.this,
                    Manifest.permission.SEND_SMS)) {
       ActivityCompat.requestPermissions(Menu_AgroMovil.this,
                        new String[]{Manifest.permission.SEND_SMS}, 1);
   } else {
       ActivityCompat.requestPermissions(Menu_AgroMovil.this,
                        new String[]{Manifest.permission.SEND_SMS}, 1);
   }
}else {
   //...
}
  

Send button

String number = "12121"; //claro un numero coherente. 
String sms = "Mensaje a enviar ...";

try {
   SmsManager smsManager = SmsManager.getDefault();
   smsManager.sendTextMessage(number,null,sms,null,null);
   Toast.makeText(Menu_AgroMovil.this,"SMS enviado",Toast.LENGTH_SHORT).show();
}catch (Exception e){
   Toast.makeText(Menu_AgroMovil.this,"Error al enviar SMS",Toast.LENGTH_SHORT).show();
}
    
asked by DoubleM 23.02.2017 в 10:22
source

1 answer

1

A. Permissions in the manifest:

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

B. Then you can send it in two ways:

1. With SmsManager

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("elNumero", null, "mensaje sms", null, null);

2. With Pending Intent

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("cuerpo_sms", "default content"); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);

Note: If it does not work please show the error message you are giving.

    
answered by 23.02.2017 в 13:27