title: How to get last known location for Location manager in Android?
body: <p>I am using simple <strong><em>location manager</em></strong> object to <strong>get lastKnownLocation()</strong> of device 
  but getting <strong>null object</strong> in <strong>return</strong> can any one tell me why ?</p>
  
  <p><strong>Code :</strong></p>
  
  <pre><code>public Location getLocation() {
      LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);        
      if (locationManager != null) {          
          Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
          if (lastKnownLocationGPS != null) {             
              return lastKnownLocationGPS;
          } else {                
              Location loc =  locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
              System.out.println("1::"+loc);----getting null over here
              System.out.println("2::"+loc.getLatitude());
              return loc;
          }
      } else {            
          return null;
      }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.location);
      getLocation();-----calling service
  
   }
  </code></pre>
  
  <p><strong>permissions given :</strong></p>
  
  <pre><code> &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
   &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
   &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
   &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
   &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  </code></pre>
  
  <p>so is anything missing to set up? I have checked my location service is on in device
  please give some links for working examples</p>