title: auto send sms when gps location is available
body: <p>i'm developing an app to send user location via sms after a fixed time and if the phone restarts the app should starts automatically in the background without any launcher activity. I have quiet accomplished my task I'm only facing problem in getting location, as gps takes some time and app gets crash when it does not find any location.
   here is my code of main Activity</p>
  
  <pre><code>public class MainActivity extends Activity {
  
   @Override
      public void onCreate(Bundle savedInstanceState) 
       {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
      startLocationTracking();
  </code></pre>
  
  <p>}</p>
  
  <pre><code>private void startLocationTracking()
  {
      AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
      Intent alarmintent1 = new Intent(MainActivity.this, AlarmReceiver.class);
      PendingIntent sender1=PendingIntent.getBroadcast(MainActivity.this, 100, alarmintent1, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
  
      try {
          am.cancel(sender1);
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          System.out.println("exjfkd"+e);
      }
  
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.MINUTE,10);
  
      am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*600, sender1);
      System.out.println("set timer");
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public class AlarmReceiver extends BroadcastReceiver{
  
  
  long time = 600* 1000; 
  long distance = 10; 
  
  @SuppressLint("NewApi") 
  
  @Override
  public void onReceive(final Context context, Intent intent) {
  
      System.out.println("alarm receiver....");
      Intent service = new Intent(context, MyService.class);
      context.startService(service);
  
      //Start App On Boot Start Up
      if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
      Intent App = new Intent(context, MainActivity.class);
      App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(App);
  
   }
  try{
      LocationManager locationManager = (LocationManager)context
              .getSystemService(Context.LOCATION_SERVICE);
  
  
      Criteria criteria = new Criteria();
  
      String provider = locationManager.getBestProvider(criteria, true);
  
      locationManager.requestLocationUpdates(provider, time,
              distance, locationListener);
  
  
      Location location = locationManager.getLastKnownLocation(provider);
  
      TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
      String device_id = tm.getDeviceId();  // returns IMEI number 
  
  
      String phoneNo = "+923362243969";
  
      String Text = "Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;
  
  
      SmsManager smsManager = SmsManager.getDefault();
      smsManager.sendTextMessage(phoneNo, null, Text, null, null);
      Log.i("Send SMS", "");
  
   } 
      catch (Exception e) {
      e.printStackTrace();
   } 
      this.abortBroadcast();
  
  }
  
  
  LocationListener locationListener = new LocationListener() {
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
      }
  
      @Override
      public void onProviderEnabled(String provider) {
  
      }
  
      @Override
      public void onProviderDisabled(String provider) {
  
      }
  
      @Override
      public void onLocationChanged(Location location) {
  
      }
  };
  </code></pre>
  
  <p>}</p>
  
  <p>i'm little confuse in logic I want my app to send sms when gps coordinates are available. and if gps in disabled on the phone it if network is available on the phone it should get the location through network  and send it through the network. </p>