title: Fused Location Not returning location when GPS turned on
body: <p>We have a requirement to get user location with different intervals. I am using Fused Location API recommended by google to fetch user location. Application wants to get user location in both mode whether internet available or not. we are getting user location warning him to enable his/her GPS always to get his/her location.</p>
  <ol>
  <li>if user have WiFi and GPS active i am getting location its fine.</li>
  <li>if user have GPS active only again i am getting location. but sometime i am getting location with very higher accuracy like 1000 meter</li>
  <li>but if user turned off his GPS for a while and turn it on again.
  Then after turning on GPS Fused Location unable to get location
  until i open google map or connect to internet.</li>
  </ol>
  <blockquote>
  <p>Following are my devices</p>
  </blockquote>
  <blockquote>
  <pre><code> 1. Galaxy Samsung S4 os 5.1
   2. Realme 7Pro os 10
  </code></pre>
  <p>This Question is asked on multiple threads but still i am unable to
  get my answer</p>
  <p>I have read Fused Location Documentation and stack overflow answer which says cache cleared when user turned of GPS and when you open turn on GPS again there should be one client should be connected to get location.</p>
  </blockquote>
  <p><em><strong>but my real concern is how i can get my location without going to
  google map or activating my internet. IS there any option to minimize accuracy in point 2 and how i can get location in case 3 ( real headache )</strong></em></p>
  <p>MainActivity.kt</p>
  <pre><code>class MainActivity : AppCompatActivity() {
  
      lateinit var fusedLocationProviderClient: FusedLocationProviderClient
      var REQUEST_CHECK_SETTINGS = 100
  
      companion object {
          private const val LOCATION_PERMISSION_REQUEST_CODE = 999
          const val TAG = &quot;MainActivity&quot;
      }
  
  
      @SuppressLint(&quot;MissingPermission&quot;)
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          
  
  
          button.setOnClickListener {
  
              latTextView.text = &quot;0.0&quot;
              lngTextView.text = &quot;0.0&quot;
              accuracy.text = &quot;0.0&quot;
  
              when {
                  PermissionUtils.isAccessFineLocationGranted(this) -&gt; {
                      when {
                          PermissionUtils.isLocationEnabled(this) -&gt; {
                              if (::fusedLocationProviderClient.isInitialized) {
                                  removeLocationUpdates(true)
                              } else {
                                  setUpLocationListener()
                              }
                          }
                          else -&gt; {
                              PermissionUtils.showGPSNotEnabledDialog(this)
                          }
                      }
                  }
                  else -&gt; {
                      PermissionUtils.requestAccessFineLocationPermission(
                          this,
                          LOCATION_PERMISSION_REQUEST_CODE
                      )
                  }
              }
          }
      }
  
      private fun removeLocationUpdates(isStartLocation: Boolean) {
          var result = fusedLocationProviderClient.removeLocationUpdates(locationCallBack)
          result.addOnCompleteListener {
              Log.e(TAG, &quot;request removed&quot;)
              if (isStartLocation) {
                  setUpLocationListener()
              }
  
          }
      }
  
      @SuppressLint(&quot;MissingPermission&quot;)
      private fun setUpLocationListener(priority: Int = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) {
  
  
          fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
          // for getting the current location update after every 2 seconds with high accuracy
          val locationRequest = LocationRequest()
          locationRequest.interval = 4000
          locationRequest.fastestInterval = 1000
          locationRequest.priority = priority
  
          val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)//.setAlwaysShow(true)
          val client: SettingsClient = LocationServices.getSettingsClient(this)
          val task: Task&lt;LocationSettingsResponse&gt; = client.checkLocationSettings(builder.build())
  
          task.addOnSuccessListener {
              Log.e(TAG, &quot;task success&quot;)
              fusedLocationProviderClient.requestLocationUpdates(
                  locationRequest,
                  locationCallBack,
                  Looper.myLooper()
              )
          }
  
          task.addOnFailureListener { exception -&gt;
              Log.e(TAG, &quot;task exception&quot;)
              if (exception is ResolvableApiException) {
                  // Location settings are not satisfied, but this can be fixed
                  // by showing the user a dialog.
                  try {
                      // Show the dialog by calling startResolutionForResult(),
                      // and check the result in onActivityResult().
                      exception.startResolutionForResult(
                          this@MainActivity,
                          REQUEST_CHECK_SETTINGS
                      )
                  } catch (sendEx: IntentSender.SendIntentException) {
                      // Ignore the error.
                  }
              }
          }
  
  
        
      }
  
      var locationCallBack = object : LocationCallback() {
          override fun onLocationResult(locationResult: LocationResult) {
              super.onLocationResult(locationResult)
  
              Log.e(TAG, &quot;Locations found : ${locationResult.locations.size}&quot;)
  
              removeLocationUpdates(false)
  
              for (location in locationResult.locations) {
  
                  Log.e(TAG, &quot; Location time is : ${location.time}&quot;)
  
                  latTextView.text = location.latitude.toString()
                  lngTextView.text = location.longitude.toString()
                  accuracy.text = location.accuracy.toString()
              }
              // Few more things we can do here:
              // For example: Update the location of user on server
          }
  
          @SuppressLint(&quot;MissingPermission&quot;)
          override fun onLocationAvailability(p0: LocationAvailability?) {
              super.onLocationAvailability(p0)
              Log.e(TAG, &quot;Location result is ${p0}&quot;)
  
              availability.text = p0?.isLocationAvailable?.toString()
  
  
          }
      }
  
      override fun onRequestPermissionsResult(
          requestCode: Int,
          permissions: Array&lt;out String&gt;,
          grantResults: IntArray
      ) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults)
          when (requestCode) {
              LOCATION_PERMISSION_REQUEST_CODE -&gt; {
                  if (grantResults.isNotEmpty() &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                      when {
                          PermissionUtils.isLocationEnabled(this) -&gt; {
                              setUpLocationListener()
                          }
                          else -&gt; {
                              PermissionUtils.showGPSNotEnabledDialog(this)
                          }
                      }
                  } else {
                      Toast.makeText(
                          this,
                          getString(R.string.location_permission_not_granted),
                          Toast.LENGTH_LONG
                      ).show()
                  }
              }
          }
      }
  
  
  }
  </code></pre>
  <p>PermissionUtils.kt</p>
  <pre><code>object PermissionUtils {
      /**
       * Function to request permission from the user
       */
      fun requestAccessFineLocationPermission(activity: AppCompatActivity, requestId: Int) {
          ActivityCompat.requestPermissions(
              activity,
              arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
              requestId
          )
      }
  
      /**
       * Function to check if the location permissions are granted or not
       */
      fun isAccessFineLocationGranted(context: Context): Boolean {
          return ContextCompat
              .checkSelfPermission(
                  context,
                  Manifest.permission.ACCESS_FINE_LOCATION
              ) == PackageManager.PERMISSION_GRANTED
      }
  
      /**
       * Function to check if location of the device is enabled or not
       */
      fun isLocationEnabled(context: Context): Boolean {
          val locationManager: LocationManager =
              context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
          return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
                  || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
      }
  
      /**
       * Function to show the &quot;enable GPS&quot; Dialog box
       */
      fun showGPSNotEnabledDialog(context: Context) {
          AlertDialog.Builder(context)
              .setTitle(context.getString(R.string.enable_gps))
              .setMessage(context.getString(R.string.required_for_this_app))
              .setCancelable(false)
              .setPositiveButton(context.getString(R.string.enable_now)) { _, _ -&gt;
                  context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
              }
              .show()
      }
  }
  </code></pre>
  <p>Manifest.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;com.location&quot;&gt;
  
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/AppTheme&quot;&gt;
          &lt;activity android:name=&quot;.MainActivity&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  <p>Build.gradle</p>
  <pre><code>apply plugin: 'com.android.application'
  apply plugin: 'kotlin-android'
  apply plugin: 'kotlin-android-extensions'
  
  android {
      compileSdkVersion 29
      buildToolsVersion &quot;29.0.3&quot;
  
      defaultConfig {
          applicationId &quot;com.location&quot;
          minSdkVersion 21
          targetSdkVersion 29
          versionCode 1
          versionName &quot;1.0&quot;
  
          testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
      }
  
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
          }
      }
  
  }
  
  dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation &quot;org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version&quot;
      implementation 'androidx.appcompat:appcompat:1.1.0'
      implementation 'androidx.core:core-ktx:1.2.0'
      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'androidx.test.ext:junit:1.1.1'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
      implementation 'com.google.android.gms:play-services-location:17.0.0'
  }
  </code></pre>
  <p>activity_mail.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/availability&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;60sp&quot;
          android:layout_marginBottom=&quot;8dp&quot;
          android:gravity=&quot;center&quot;
          android:text=&quot;@string/default_lat&quot;
          android:textColor=&quot;@color/colorBlack&quot; /&gt;
  
  
      &lt;Button
          android:textAllCaps=&quot;false&quot;
          android:id=&quot;@+id/button&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Fetch Location&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;
  
      &lt;LinearLayout
          android:id=&quot;@+id/linearLayout&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:orientation=&quot;vertical&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
  
          &lt;TextView
              android:id=&quot;@+id/latTextView&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;60sp&quot;
              android:layout_marginBottom=&quot;8dp&quot;
              android:gravity=&quot;center&quot;
              android:text=&quot;@string/default_lat&quot;
              android:textColor=&quot;@color/colorBlack&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/lngTextView&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;60sp&quot;
              android:layout_marginBottom=&quot;8dp&quot;
              android:gravity=&quot;center&quot;
              android:text=&quot;@string/default_lng&quot;
              android:textColor=&quot;@color/colorBlack&quot; /&gt;
  
          &lt;TextView
              android:id=&quot;@+id/accuracy&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;60sp&quot;
              android:layout_marginBottom=&quot;8dp&quot;
              android:gravity=&quot;center&quot;
              android:text=&quot;0&quot;
              android:textColor=&quot;@color/colorBlack&quot; /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>