0

Only text message is able to send through this code but I don't know how to code for to send current location link with text message

    case R.id.button7:

    Intent i1 = new Intent(Intent.ACTION_CALL);
    i1.setData(Uri.parse("tel: 700000000"));
    startActivity(i1);

     try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage("70000000", null, "hello", null, null);
            Toast.makeText(getApplicationContext(), "SMS Sent!",
                        Toast.LENGTH_LONG).show();
          } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                "SMS faild, please try again later!",
                Toast.LENGTH_LONG).show();
            e.printStackTrace();
          }
    }}
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

If you want to add it to the SMS message you need a way to recover it from the rest of the message.

Just define a separator character that will never be used on the text.

Example:

"hello|3.3452|22.2323"

Once you receive that string on the other side you can use

String.split("|");

That will return an array containing this:

[0]"hello"
[1]"3.3452"
[2]"22.2323"

Just knowing that Lat will be always at position 1 Long at 2 and the text message at 0

Hope this helps.

Nanoc
  • 2,381
  • 1
  • 20
  • 35
0

To send user's location as TextMessage you need to get Latitude and Longitude of the phone, you can do that by.

GPSTracker mGps = new GPSTracker(MainActivity.this);
double latitude = mGps.getLatitude();
double longitude = mGps.getLongitude();

Now to send it as a TextMessage, you need to use SmsManager. Use this function in your button press.

protected void sendSMSMessage() {
        Log.i("Send SMS", "");
        double latitude = mGps.getLatitude();
        double longitude = mGps.getLongitude();

        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        String phoneNo = mTxtphoneNo.getText().toString();//get phone number from the textView of the editText or wherever you have feeded the number
        String message = "These are my co-ordinates:-" + latitude + ", "
                + longitude + "\nAddress:-";

        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, message, null, null);
            Toast.makeText(
                    getApplicationContext(),
                    "SMS sent with current latitude and logitude",
                    Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    "SMS faild, please try again.", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
penta
  • 2,536
  • 4
  • 25
  • 50