title: How to get Android current location "only on demand" using Location Manager but without Location Listener?
body: <p>My app does not need on-the-fly location updates. It needs the location only when (1)The app starts; (2)A specific activity/fragment is opened; (3)The user hits "get my location" button. </p>
  
  <p>The criteria is: when demanded the location must come fast even if it is coarse with (if possible) minimum battery consumption. I plan to use a method along the lines of:</p>
  
  <pre><code>public void getLocation() {
          LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  
          Location location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
  
          if(location != null) {
              //save the location to shared prefs to use when needed
              return;
          }
  
          location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  
          if(location != null) {
              //save the location to shared prefs to use when needed
              return;
          }
  
          location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  
          if(location != null) {
              //save the location to shared prefs to use when needed
              return;
          }
  
          // if reached that point the location cannot be got!!
      }
  </code></pre>
  
  <p>The plan is to call that method every-time the app starts, or a specific page is activated, or user wants to get the current location. Unless those actions trigger a location update, the old location or no location is enough. Does this approach make sense? Can I do that without implementing Location Listener and/or calling the method "requestLocationUpdates()"? My concern is "getLastKnownLocation()" may not give the current location when called since I am not sure how often or at what conditions that "last known location" from a provider is updated.</p>
  
  <p>Any suggestions and comments are highly appreciated.</p>