title: Auto Sends GPS Coordinates via SMS
body: <p>I made an Application that shows my GPS coordinates (Langitude and Latitude) number, which designed with out a map. But I would like to send the lat and long to mobile number/phone number Automatically (no need a send button) every time I open this Application. Anyone can help me.. </p>
  
  <p><strong>Gps.java</strong></p>
  
  <pre><code>package Sample.gps.send;
  
  import android.app.Activity;
  
  import android.content.Context;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.widget.Toast;
  
  public class Gps extends Activity
  {
  @Override
  
  public void onCreate(Bundle savedInstanceState)
  {
  
  super.onCreate(savedInstanceState);
  
  setContentView(R.layout.main);
  
  
  /* Use the LocationManager class to obtain GPS locations */
  
  LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  
  LocationListener mlocListener = new MyLocationListener();
  
  
  mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
  
  
  }
  
  
  /* Class My Location Listener */
  
  public class MyLocationListener implements LocationListener
  {
  
  @Override
  
  public void onLocationChanged(Location loc)
  
  {
  
  loc.getLatitude();
  
  loc.getLongitude();
  String Text = "My location is: " +
  
  "Latitude = " + loc.getLatitude() +
  
  "Longitude = " + loc.getLongitude();
  
  
  
  
  Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
  
  }
  
  
  @Override
  
  public void onProviderDisabled(String provider)
  
  {
  
  Toast.makeText( getApplicationContext(),
  "GPS Disabled",
  
  
  Toast.LENGTH_SHORT ).show();
  
  }
  
  
  @Override
  
  public void onProviderEnabled(String provider)
  
  {
  
  Toast.makeText( getApplicationContext(),
  
  
  "GPS Enabled",
  Toast.LENGTH_SHORT).show();
  
  }
  
  
  @Override
  
  public void onStatusChanged(String provider, int status, Bundle extras)
  
  {
  
  
  }
  
  }
  
  }
  </code></pre>
  
  <p>Here is for the <strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="Sample.gps.send"
            android:versionCode="1"
            android:versionName="1.0"&gt;
  
  &lt;application android:icon="@drawable/icon" 
               android:label="@string/app_name"&gt;
  &lt;activity android:name=".UseGps"
            android:label="@string/app_name"&gt;
  &lt;intent-filter&gt;
  &lt;action android:name="android.intent.action.MAIN"/&gt;
  &lt;category android:name="android.intent.category.LAUNCHER"/&gt;
  &lt;/intent-filter&gt;          
            &lt;/activity&gt;            
               &lt;/application&gt; 
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"&gt;
  &lt;/uses-permission&gt;          
   &lt;uses-permission android:name="android.permission.SEND_SMS"&gt;
      &lt;/uses-permission&gt;             
            &lt;/manifest&gt;
  </code></pre>
  
  <p>Here MyLocationListener is an inner class. Can anyone tell how to retrieve the coordinates whenever onLocationChanged method is called? The location is displayed using a Toast. Is it possible to get the coordinates by calling any method from onCreate()? </p>