FusedLocationProviderClient requestLocationUpdates doesn't trigger LocationCallBack for API 23 above

时间秒杀一切 提交于 2021-01-04 09:08:39

问题


I have run out of possible ways on how to solve this issue. I am technically using the same code that is working in API 23 and above before I made tweaks on my app.

The callback (in the requestLocationUpdate) does not trigger in phones that are marshmallow and above (but it works fine in the emulator, tested using the Pixel 2 API 26 emulator). Take note that permission is already granted/allowed and GPS is also enabled. Also as you can see in the code below I already put a try catch and still there are no errors or even any logs in the console that would say there is an error. I have tested this in multiple phones (Marshmallow, Nougat and Oreo phones, not working. Lollipop and Kitkat phones, working)

Codes affected:

private var mFusedLocationClient: FusedLocationProviderClient? = null
private val UPDATE_INTERVAL = (30 * 1000).toLong()  
private val FASTEST_INTERVAL: Long = 300000
private lateinit var mLocationRequest:LocationRequest
private lateinit var locationCallback: LocationCallback

override fun onCreate(savedInstanceState: Bundle?) {

     locationCallback = object : LocationCallback() {
         override fun onLocationResult(locationResult: LocationResult?) {
                //super.onLocationResult(locationResult)
                onLocationChanged(locationResult!!.lastLocation)
            }

          override fun onLocationAvailability(p0: LocationAvailability?) {
            }
     }

    startLocationUpdates()

}

protected fun startLocationUpdates() {
        mLocationRequest = LocationRequest.create()
        mLocationRequest!!.run {
            setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            setInterval(UPDATE_INTERVAL)
            setFastestInterval(FASTEST_INTERVAL)

        }
//      mLocationRequest = LocationRequest().apply {
//            interval = UPDATE_INTERVAL
//            fastestInterval = FASTEST_INTERVAL
//            priority = LocationRequest.PRIORITY_HIGH_ACCURACY
//      }

        val builder = LocationSettingsRequest.Builder()
                    .addLocationRequest(mLocationRequest)


        val client: SettingsClient = LocationServices.getSettingsClient(this)
        val task: Task<LocationSettingsResponse> = client.checkLocationSettings(builder.build())
        task.addOnSuccessListener { locationSettingsResponse ->

        }

        task.addOnFailureListener { exception ->
          print(exception)
        }

        registerLocationListner()

  }

private fun registerLocationListner() {

    if(android.os.Build.VERSION.SDK_INT >= 15 && checkPermission()) { 

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)

          try{

    //            locationCallback does not trigger if the phone is android API 23 or higher
         mFusedLocationClient?.requestLocationUpdates(mLocationRequest, locationCallback, null)

          }catch(e:Exception){

          }



    }


}

private fun checkPermission() : Boolean {
      if (ContextCompat.checkSelfPermission(this , android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
          return true;
      } else {
          requestPermissions()
          return false
      }
}

private fun requestPermissions() {
    if(android.os.Build.VERSION.SDK_INT >= 23){
        ActivityCompat.requestPermissions(this,
                arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION),
               1)

    }else{
        ActivityCompat.requestPermissions(this, arrayOf("Manifest.permission.ACCESS_FINE_LOCATION"),1)

    }
}

Also my gradle just to show my dependencies/used frameworks

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'io.fabric'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "my app id goes here"
        minSdkVersion 19
        targetSdkVersion 26
        versionName '2.5'
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        versionCode 4
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            debuggable true
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-dynamic-animation:26.1.0'
    implementation 'com.google.android.gms:play-services-ads:16.0.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'com.google.firebase:firebase-core:16.0.4'
    implementation 'com.google.firebase:firebase-firestore:17.1.1'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.9.5'
    implementation 'com.squareup.okhttp3:okhttp:3.8.1'
    implementation('com.github.bumptech.glide:glide:4.4.0') {
        exclude group: "com.android.support"
    }
    kapt 'com.github.bumptech.glide:compiler:4.4.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'

来源:https://stackoverflow.com/questions/52961114/fusedlocationproviderclient-requestlocationupdates-doesnt-trigger-locationcallb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!