Search code examples
androidsmsmanager

Sending a pre-saved message (as a string) using SmsManager


i am realy new to android developing and i get stuck a lot. In my application i am trying to send a pre-saved message to user specified numbers. I was able to send a message with smsmanager ( basic tutorials helped alot ) but i want the program to send a message which user has pre defined with gps location information.

I created a string in the Strings.xml file but i can't reach the string from the fragment i created. So basicly how can i send a pre written messages + gps info to pre chosen numbers ? ( the number and message will be chosen by the user for future use )


Solution

  • String stringineed =getString(R.string.nameofstringyourneedinstringsxml);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Geocoder gCoder = new Geocoder(context);
    List<Address> addresses = null;
    try {addresses = gCoder.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
        e.printStackTrace();
        }
    if (addresses != null && addresses.size() > 0) {
    String addressinfo = "addresses.get(0).getAddressLine(0) + "\n"+ addresses.get(0).getPremises() + "\n"+ addresses.get(0).getCountryCode() + "\n"+ addresses.get(0).getPostalCode() + "\n""}
    String prewrittenmessage =("This is the prewrittenmessage"+addressinfo);
    //or stringinneed+""+ addressinfo
    String phone = "some chosen number to be sent to";
    Intent sendtoemail = new Intent(Intent.ACTION_SEND);
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phone, null, prewrittenmessage ,null, null);