Psycop Posted January 8, 2017 at 02:07 PM Report #601904 Posted January 8, 2017 at 02:07 PM Boa tarde, Estou a tentar criar uma pequena app que envie uma mensagem com as coordenadas do dispositivo, no entanto não estou a conseguir implementar a obtenção das coordenadas e respectiva injecção na SMS. Consigo enviar uma SMS, mas agora como é que obtenho as coordenadas e as injecto na SMS? Aqui fica o código que tenho até agora: public class MainActivity extends AppCompatActivity { private static final int PERMISSION_REQUEST = 100; protected Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button_panic = (Button) findViewById(R.id.button_panic); button_panic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { 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); final String nome = "Nuno"; final String apelido = "Santos"; final String telefone = "123456789"; //Verify if sharedPreferences no Null - User Data Are Available if (nome != "" && apelido != "" && telefone != "") { String message = "PANIC - Preciso de Ajuda " + nome + " " + apelido; //+ " " + "Esta é a minha localização: Latitude: " + latitude_msg + " Longitude: " + longitude_msg; //Sens SMS SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(telefone, null, message, null, null); Toast.makeText(getApplicationContext(), "SMS Enviada.", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Dados do Utilizador Não Preenchidos!!", 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(); } } Alguém me pode ajudar a implementar a obtenção das coordenadas e a injecção na SMS? Terei de utilizar uma AsyncTask para obter as coordenadas e só depois dessa obtenção as injectar na SMS e enviar a respectiva? Agradecia a ajuda, pois tenho tentado sem sucesso até então. Cumps
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now