Problem sending SMS message - Android Studio

0

I need help in a code that I am elaborating, it consists of sending the coordinates by a text message and entering the phone number in an editText, I am a beginner for android studio, the problem is that when I enter the phone in the text and I give the send button does not send any message and the message "Failed to send", the app is testing it on a cell phone with dual SIM and it does not work, also the probe on a cell phone with only one SIM and neither it works, I already have the permissions and at the moment of pressing the button it asks for the permissions to send SMS, I hope you can help me, here I leave the code.

public class MainActivity extends Activity implements LocationListener {
protected LocationManager LM;
private TextView Coordenadas, Direccion;
private Button Enviar;
private EditText Telefono;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Telefono = (EditText) findViewById(R.id.Telefono);

    LM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000);
    } else
        {
        LM.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1000, this);
        LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1000, this);

        }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Enviar = (Button) findViewById(R.id.Enviar);

    Enviar.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            try
            {
                Coordenadas = (TextView) findViewById(R.id.Coordenadas);
                Direccion = (TextView) findViewById(R.id.Direccion);
                String messageToSend = Coordenadas.getText().toString();
                String messageToSend2 = Direccion.getText().toString();
                if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED)
                {
                    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.SEND_SMS))
                    {
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 1);
                    } else
                        {
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 1);
                        }
                }else
                    {
                    String number = Telefono.getText().toString();

                    SmsManager sms = SmsManager.getDefault();
                    sms.sendTextMessage(number, null, messageToSend + messageToSend2, null, null);
                    }
                Toast.makeText(getApplicationContext(), "Mensaje Enviado!", Toast.LENGTH_LONG).show();
            } catch (Exception e)
            {
                Toast.makeText(getApplicationContext(), "Fallo el envio!", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    });
}

@Override
public void onLocationChanged(Location location)
{
    Coordenadas = (TextView) findViewById(R.id.Coordenadas);
    Direccion = (TextView) findViewById(R.id.Direccion);
    Coordenadas.setText("Mi ubicacion actual es: " + " Lat = " + location.getLatitude() + " Long = " + location.getLongitude());
    this.setLocation(location);
}
public void setLocation(Location location)
{
    //Obtener la direccion de la calle a partir de la latitud y la longitud
    if (location.getLatitude() != 0.0 && location.getLongitude() != 0.0)
    {
        try
        {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> list = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
            if (!list.isEmpty())
            {
                Address DirCalle = list.get(0);
                Direccion.setText("Mi direccion es: /n" + DirCalle.getAddressLine(0));
            }

        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

@Override
public void onProviderDisabled(String provider)
{
    Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider)
{
    Log.d("Latitude","enable");

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
    Log.d("Latitude","status");
}

}

    
asked by Erick Villarreal 25.10.2018 в 02:04
source

0 answers