Search code examples
javaandroid-studiosmsmanagerfusedlocationproviderapi

How to send location using sms? (fused location api)


I'm currently making an app for school.. my app functions specifically for sending users most accurate location via SMS. so I used fused location api ... but now I don't know how to incorporate the SMS manager on my current app ... at present I only built a simple app which shows the users current location by pressing a button I just want help on how to incorporate SMS manager in my app. I also want the message to be in a URL form plus the coordinates so that it will be easy for the receiver to track the user.

here is my code Main activity

    import android.Manifest;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.os.Looper;
    import android.support.annotation.NonNull;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    import com.google.android.gms.location.FusedLocationProviderClient;
    import com.google.android.gms.location.LocationCallback;
    import com.google.android.gms.location.LocationRequest;
    import com.google.android.gms.location.LocationResult;
    import com.google.android.gms.location.LocationServices;

    public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_CODE = 2300;
TextView textView;
Button sosbtn;
FusedLocationProviderClient fusedLocationProviderClient;
LocationRequest locationRequest;
LocationCallback locationCallback;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case REQUEST_CODE: {
            if (grantResults.length > 0) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {

                }


            }

        }

    }


}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);
    sosbtn = (Button) findViewById(R.id.sosbtn);

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
    } else {
        buildLocationRequest();
        buildLocationCallBack();
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

        sosbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
                    return;
                }
                fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());

            }
        });

    }

}

private void buildLocationCallBack() {
    locationCallback= new LocationCallback()
    {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            for(Location location:locationResult.getLocations())
                textView.setText(String.valueOf(location.getLatitude())+"/"+String.valueOf(location.getLongitude()));
        }
    };

}

private void buildLocationRequest() {
    locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(5000);
    locationRequest.setFastestInterval(3000);
    locationRequest.setSmallestDisplacement(5);


}

}

your help will be greatly appreciated.


Solution

  • Here's an example, declare the location object as a private field and on the click of your button:

    SmsManager smsManager = SmsManager.getDefault ();
    
    String message = "My location: https://www.google.com/maps/@?api=1&map_action=map&center=" + location.getLatitude () + "," + location.getLongitude ();
    String phoneNumber = "+1....";
    
    smsManager.sendTextMessage (phoneNumber, null, message, null, null);
    

    The above link should redirect your recipient to google maps.

    SmsManager.sendTextMessage

    Don't forget to add this permission in your manifest:

    <uses-permission android:name="android.permission.SEND_SMS"/>
    

    and request the send sms permission in runtime.