I'm trying to develop an app , where I need to get the GPS coordinates of the device's location to send them by SMS.
What happens is that with a click on a button the logic will acquire the coordinates and send the SMS with them, with just click .
My problem at this point is to pass the coordinates to the SendSMS()
method.
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();
}
It sends these same coordinates to the SendSMS method to incorporate them in the message.
Any help on how to solve this?