如何将我的位置(经度和纬度)作为短信发送到android中的另一部手机

时间:2016-06-24 05:20:19

标签: android gps location

我想知道如何通过单击按钮发送我的坐标。我有以下代码发送消息,如发送坐标。

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

请按照以下步骤操作

     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();
                }
            }
        });

选择地点后处理

    @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);
        }
    }