user8559076
user8559076

Reputation: 55

Sending current location as SMS Android Studio

I'm new to Android Studio.

I'm trying to send my current location as a text message when clicking a button but nothing happens when I click.

First, here's my XML:

<Button android:id="@+id/emergency"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:background="@drawable/button_selector1"
        android:drawableEnd="@android:drawable/ic_dialog_alert"
        android:gravity="center"
        android:maxLines="1"
        android:paddingLeft="15dip"
        android:paddingRight="15dip" />

Then, here's my MainActivity code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.ly_home);

    emergency = (Button) findViewById(R.id.emergency);

    emergency.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            GPStracker g = new GPStracker(getApplicationContext());
            Location l = g.getLocation();
            if (l != null){
                double lat = l.getLatitude();
                double lon = l.getLongitude();
                String message="http://maps.google.com/maps?saddr="+lat+","+lon;
                String number = "xxxxxxxx";
                SmsManager smsManager = SmsManager.getDefault();
                StringBuffer smsBody = new StringBuffer();
                smsBody.append(Uri.parse(message));
                android.telephony.SmsManager.getDefault().sendTextMessage(number, null, smsBody.toString(), null,null);
            }
        }
    });
}

And, my GPStracker class:

public class GPStracker implements LocationListener{

    Context context;
    public GPStracker(Context c){
        context = c;
    }

    public Location getLocation(){

        LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (isGPSEnabled){
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,6000,10,this);
            Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            return l;
        }else{
            Toast.makeText(context,"Please enable GPS", Toast.LENGTH_LONG).show();
        }
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

I've given the permissions in the manifest: Fine location, Coarse location and receiving and sending text messages. What am I doing wrong?

Upvotes: 2

Views: 10928

Answers (2)

Ramees Thattarath
Ramees Thattarath

Reputation: 1093

As per the below answer https://stackoverflow.com/a/43199211/6835152

There are two different ways to retrieve the location, one is based on network and another is based on GPS services. Certainly, location based on GPS services would always be more accurate. According to the documentation, it might take some time to receive the location using the GPS services. In this case, the last known location can be used to get the recently available location by using getLastKnownLocation() method. Here, to get the last available location, you should change LocationManager.GPS_PROVIDER to LocationManager.NETWORK_PROVIDER.

And if you wish to receive the accurate, real time location based on GPS services, I recommend you to use FusedLocationApi. You can find it here https://developer.android.com/training/location/receive-location-updates.html

Upvotes: 1

Adolf Dsilva
Adolf Dsilva

Reputation: 14360

Used LocationServices and fusedlocation API Also Have you added dependency for google play services?

Here is a tutorial for getting the current location.

Also you can find code for sending sms here

Upvotes: 1

Related Questions