Android Facil Android Facil - 5 months ago 20
Android Question

How to send my location (longitude and latitude) as an SMS to another cell phone in android

I want to know how to send my coordinates via SMS by clicking a button. I have the following code that a message is sent, as sending the coordinates.

public class MainActivity extends AppCompatActivity {
Button Enviar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Enviar = (Button)findViewById(R.id.btnEnviar);

Enviar.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
EnviarMensaje("cell phone number","message");


}
});
}
private void EnviarMensaje (String Numero, String Mensaje){
try {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(Numero,null,Mensaje,null,null);
Toast.makeText(getApplicationContext(), "Mensaje Enviado.", Toast.LENGTH_LONG).show();
}

catch (Exception e) {
Toast.makeText(getApplicationContext(), "Mensaje no enviado, datos incorrectos.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}

Answer

Follow these steps

     String msg_txt =""; 
     // Declare as global variable

     Enviar.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                 PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

                try {
                     startActivityForResult(builder.build(ComposeThreadsActivity.this), PLACE_PICKER_REQUEST);
                } catch (GooglePlayServicesRepairableException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (GooglePlayServicesNotAvailableException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

Handle after selecting place

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        if (data != null) {
            if (requestCode == PLACE_PICKER_REQUEST) {
                if (resultCode == RESULT_OK) {
                    Place place = PlacePicker.getPlace(data, this);
                    String toastMsg = String.format("Place: %s",
                            place.getName());
                    String address = String.valueOf(place.getAddress());
                    String placeName = String.valueOf(place.getName());

                    LatLng latLong = place.getLatLng();
                    String lat = String.valueOf(latLong.latitude);
                    String lon = String.valueOf(latLong.longitude);
                    StringBuilder sb = new StringBuilder();
                    if (!TextUtils.isEmpty(placeName)) {
                        if (placeName.contains(lat)) {

                            sb.append("http://maps.google.com/?q=" + lat + ","
                                    + lon);
                        } else {
                            sb.append("Place: " + placeName);
                            if (!TextUtils.isEmpty(address)) {
                                sb.append("\nAddress: " + address);
                            }
                            sb.append("\nLink: http://maps.google.com/?q="
                                    + lat + "," + lon);
                        }
                        msg_txt = sb.toString();
                        EnviarMensaje("cell phone number", msg_txt);

                    }

                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
Comments