Doubt in Send SMS + GPS coordinates

Asked

Viewed 357 times

1

I’m trying to develop a app, where I need to get GPS coordinates from the location of the device to send them by SMS.

What happens is that I intend that with a click on a button the logic make the acquisition of the coordinates and the sending of the SMS with them, with only one click.

My problem right now is in passing the coordinates to the method SendSMS().

Could you help me?

/*
    @autor: Nuno Santos
    @date: 27/06/2016
    @version: 1.0
*/


public class MainActivity extends AppCompatActivity {

    //Variable Declaration
    private AdView mAdView;
    //Permission Request
    private static final int PERMISSION_REQUEST = 100;
    //GPS Location


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);


        /*Button PANIC Button
        @ Click this button to send a PANIC message to defined contact
        @ Inside the message go a PANIC message, name of person and GPS location
        */
        Button button_panic = (Button) findViewById(R.id.button_panic);
        button_panic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                /*Alert If User GPS Location Not Enabled
                @
                @Comment: If GPS Location Not Enabled Show a dialog to enable GPS
                 */
                final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    //Runtime Permission Request
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        if (checkSelfPermission(Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
                            if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {

                            } else {
                                requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST);
                            }
                        } else {
                            sendSMS();
                        }
                    } else {
                        sendSMS();
                    }
                } else {
                    showAlert();
                }
            }
        });

    }


    /*Send Message Method
    @
    @Comment: Method to send a SMS using an intent
    */
    private void sendSMS() {

        //SharedPreferences
        final SharedPreferences sharedpreferences;
        //Define SharedPreferences
        sharedpreferences = getSharedPreferences("PANIC_PREFERENCES", Context.MODE_PRIVATE);
        //Get SharedPreferences to Send PANIC SMS
        //Retrieve SharedPreferences
        final String nome = sharedpreferences.getString("Key_nome", null);
        final String apelido = sharedpreferences.getString("Key_apelido", null);
        final String telefone = sharedpreferences.getString("Key_telefone", null);

        //Retrieve GPS coordinates


        String message = "PANIC - Preciso de Ajuda " + nome + " " + apelido;
        /// / + " " + "Esta é a minha localização: Latitude: + sms_latitude + " Longitude: " + sms_longitude;

        //Sens SMS
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(telefone, null, message, null, null);
        Toast.makeText(getApplicationContext(), "SMS Enviada.", Toast.LENGTH_LONG).show();
    }


    /*Request Permission
    @
    @Comment: Method to request permission to send SMS in Android versions over 23 - Marshmallow
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            sendSMS();

        } else {


        }
    }



    /*GPS Location
    @
    @Comment: Method to request permission to send SMS in Android versions over 23 - Marshmallow
     */
    private void showAlert() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("Ative a Sua Localização")
                .setMessage("A Sua Localização GPS está 'Off'.\nPor favor Ative a sua localização para " +
                        "usar esta funcionalidade")
                .setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(myIntent);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    }
                });
        dialog.show();
    }
}


O que eu não consigo fazer é após fazer a recolha das coordenadas no método seguinte: 

    @Override
    public void onLocationChanged(Location location) {

        String latitude = location.getLatitude()
        String longitude = location.getLongitude();
    }

Send these same coordinates to the Sendsms method to embed them in the message.

Some help on how to solve this?

1 answer

2

So you can access the values of the variables latitudeand logitude any part(method) of the Activity class must declare them as fields.

Do so:

String latitude;
String longitude;

@Override
public void onLocationChanged(Location location) {

    latitude = Double.toString(location.getLatitude());
    longitude = Double.toString(location.getLongitude());
}

The methods location.getLatitude() and location.getLongitude() return a double, how you need to use values such as strings, they are converted using the method Double.toString().

The message will be built like this:

//Retrieve GPS coordinates


String message = "PANIC - Preciso de Ajuda " + nome + " " + apelido
+ " " + "Esta é a minha localização: Latitude: " + latitude + " Longitude: " + longitude;

//Sens SMS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.