title: GPS tracking in background using service
body: <p>I have a service for listening the GPS locations and I want to display an updated location in Toast, I run the service in other activity by clicking a Button. The service is created successfully but not showing the toast of updated location. Here is code: </p>
  
  <pre><code>   public class locationlistening_service extends Service implements LocationListener  {
  
      static LocationManager locationManager;
      LocationListener locationListener;
  
      private static final String TAG = "Calculations";
      @Override
      public void onCreate() {
          Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
          Log.d(TAG, "onCreate");
          run();
      }
  
      private void run(){
  
          final Criteria criteria = new Criteria();
  
          criteria.setAccuracy(Criteria.ACCURACY_FINE);
          criteria.setSpeedRequired(true);
          criteria.setAltitudeRequired(false);
          criteria.setBearingRequired(false);
          criteria.setCostAllowed(true);
          criteria.setPowerRequirement(Criteria.POWER_LOW);
          //Acquire a reference to the system Location Manager
          locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
          // Define a listener that responds to location updates
          locationListener = new LocationListener() {
                public void onLocationChanged(Location location) 
                {
                   recordLocation(location);
                }
  
                public void onStatusChanged(String provider, int status, Bundle extras) {}
  
                public void onProviderEnabled(String provider) {}
  
                public void onProviderDisabled(String provider) {}
  
          };
      }
  public void recordLocation(Location loc) 
  {
  Toast.makeText(locationlistening_service.this,"Lat: " + String.valueOf(loc.getLatitude()) + " Long: " + String.valueOf(loc.getLongitude()),Toast.LENGTH_SHORT).show();
  
  }
  
  
  
      public void onLocationChanged(Location location) {
          // TODO Auto-generated method stub
  
      }
  
      public void onProviderDisabled(String provider) {
          // TODO Auto-generated method stub
  
      }
  
      public void onProviderEnabled(String provider) {
          // TODO Auto-generated method stub
  
      }
  
  
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
          // TODO Auto-generated method stub
  
      }
  
          }
  </code></pre>
  
  <p>there is call of service in other activity.</p>
  
  <pre><code>public void startButton(View view) {
  
      startService(new Intent(this, locationlistening_service.class));
  }
  </code></pre>