title: Android broadcast gps turned on update location
body: <p>Well even having a lot of reading here, i can't figure out the issue. It looks like i never get gps turned on event.</p>
  
  <p>After some checking in debug, the flag isGps is correct. But as a result, user alert works nice but not update location. I just want to add a marker when gps is turned on and it looks like something is not correctly synchronised here.</p>
  
  <p><strong>And that even with high accuraty location.</strong></p>
  
  <p>I use this code from my fragment.</p>
  
  <pre><code>public class MapDialogFragment extends DialogFragment
          implements OnMapReadyCallback, GoogleMap.OnMarkerDragListener {
  
  .....
  
         private BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
              @Override
              public void onReceive(Context context, Intent intent) {
  
                  if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {
  
  
                      boolean isGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  
                      if(isGPS){
                          // never goes here WHEN GPS TURNED ON !!! WHY ?? MAKES ME CRAZY
                          // + how get current location here ? is updateMyLocation call ok ?
                          updateMyLocation();
                      }
                      else{
                           // user alert
                           Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.message_location_error_gps_off), Toast.LENGTH_LONG).show();
                      }
                  }
              }
          };
  
          @Override
          public void onResume() {
              super.onResume();
              getContext().registerReceiver(mGpsSwitchStateReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
          }
  
          @Override
          public void onPause() {
              super.onPause();
              getContext().unregisterReceiver(mGpsSwitchStateReceiver);
          }
  
       private void updateMyLocation(){
              Task locationResult = mFusedLocationProviderClient.getLastLocation();
              locationResult.addOnCompleteListener(getActivity(), new OnCompleteListener() {
                  @Override
                  public void onComplete(@NonNull Task task) {
                      if (task.isSuccessful() &amp;&amp; task.getResult() != null) {
                          // Set the map's camera position to the current location of the device.
                          mLastKnownLocation = (Location) task.getResult();
                          googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                                  new LatLng(mLastKnownLocation.getLatitude(),
                                          mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
  
                          latitude = mLastKnownLocation.getLatitude();
                          longitude = mLastKnownLocation.getLongitude();
  
                          // add marker
                          buildMarkerFromLocation();
  
                      } else {
                          Log.d(TAG, "Current location is null. Using defaults.");
                          Log.e(TAG, "Exception: %s", task.getException());
                      }
                  }
              });
          }
  ...
  }
  </code></pre>
  
  <p>So here is my logic :</p>
  
  <ol>
  <li>Check if GPS is turned on</li>
  <li>If it is send mFusedLocationProviderClient.getLastLocation() request</li>
  <li>Get the result but always get no result when GPS is just turned on</li>
  </ol>
  
  <p>So basically, how to provide a default location without hard code one ? </p>
  
  <p>Here is more information about google api page :</p>
  
  <p>The getLastLocation() method returns a Task that you can use to get a Location object with the latitude and longitude coordinates of a geographic location. The location object may be null in the following situations:</p>
  
  <ol>
  <li><p>Location is turned off in the device settings. The result could be null even if the last location was previously retrieved because disabling location also clears the cache.</p></li>
  <li><p>The device never recorded its location, which could be the case of a new device or a device that has been restored to factory settings.</p></li>
  <li><p>Google Play services on the device has restarted, and there is no active Fused Location Provider client that has requested location after the services restarted. To avoid this situation you can create a new client and request location updates yourself. For more information, see Receiving Location Updates.</p></li>
  </ol>
  
  <p>Hope i will solve this with your help. Should be something obvious but i dunno what is going wrong...</p>