1

i am making a android project so can anyone tell me how do i send my current location in URL via SMS . Let me explain what i want to know when the mobile got a shake then my app sent a string SMS to selected contact now i want to add location feature in my app mean i want to send the location via URL in SMS ,when the receiver hit on URL then he can the location of sender in his map ?

  private void sendSMS(String ph, String message) {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(ph, null, message, null, null);
        Toast.makeText(getApplicationContext(), "SMS SENT",
                Toast.LENGTH_LONG).show();
    }
Abhishek
  • 682
  • 6
  • 17

2 Answers2

3

I suggest you to get the current latitude and longitude of the device. The in step two you can create a String which is a searching url with those parameters which looks like this

   String message = "http://maps.google.com/?q=" + {latitude} + "," + {longitude}

And for the further info to get the current longitude and latitude have a look here

Community
  • 1
  • 1
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • can i done with this SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage("phoneNo", null, "http://maps.google.com/?q="+lat+","+lng, null, null); – Abhishek Mar 08 '16 at 10:13
  • Yes, sure But I didn't tried getting the current lat and long yet. You can try that yourself. – Shree Krishna Mar 08 '16 at 10:14
  • @Abhishek It's better creating a variable rather than writing this line there. Make a variable as in my answer and add that string as in your code template. – Shree Krishna Mar 08 '16 at 10:16
  • i used this before ,but can you tell me how do i declare lat and lng? – Abhishek Mar 08 '16 at 10:16
  • you are saying that i have to use your code in this , SharedPreferences sharedPref = getSharedPreferences("nameInfo" , Context.MODE_PRIVATE); String ph = sharedPref.getString ("phone",""); String message="help me"; sendSMS(ph, message); – Abhishek Mar 08 '16 at 10:19
  • Have you tried using `locationManager.getLastKnownLocation` ? – Shree Krishna Mar 08 '16 at 10:19
  • 1
    @Abhishek I am just saying about the body of sms. Add the URL as the message. And when the receiver clicks that URL then he will redirected to your current location. Why are you including these `SharedPreferences` here ? – Shree Krishna Mar 08 '16 at 10:23
  • ok so you are saying that just to add String message = "http://maps.google.com/?q=" + {latitude} + "," + {longitude} in sms body , right sir? – Abhishek Mar 08 '16 at 10:33
  • Yes !! you got that now. But you can include some custom messages as well. And I suggest you to add that link at the end of the message. that looks nice.. – Shree Krishna Mar 08 '16 at 10:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105669/discussion-between-shree-krishna-and-abhishek). – Shree Krishna Mar 08 '16 at 10:53
1

normal way then you can insert a url as bellow:

String pos_url = "http://sharegps.com?lat=00000000000&long=00000000000"; // you can replace your real gps position and your domain (or use maps.google.com)

First way:

SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(ph, null, pos_url, null, null);
        Toast.makeText(getApplicationContext(), "SMS SENT",
                Toast.LENGTH_LONG).show();

Second way:

private void shareTextUrl() {
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

    // Add data to the intent, the receiving app will decide
    // what to do with it.
    share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");

    share.putExtra(Intent.EXTRA_TEXT, pos_url);

    startActivity(Intent.createChooser(share, "Share location!"));

}

GiapLee
  • 436
  • 2
  • 8