Android FusedLocationProviderClient requestLocationUpdates不';以上API 23的t触发器位置回调

Android FusedLocationProviderClient requestLocationUpdates不';以上API 23的t触发器位置回调,android,kotlin,geolocation,google-location-services,fusedlocationproviderclient,Android,Kotlin,Geolocation,Google Location Services,Fusedlocationproviderclient,我已经没有办法解决这个问题了。在我对我的应用程序进行调整之前,我使用的代码与API 23及以上版本中的代码相同 手机中不会触发回调(在requestLocationUpdate中) 这是棉花糖和更高版本的(但它在模拟器中运行良好,使用Pixel 2 API 26模拟器进行测试)。请注意,权限已授予/允许,GPS也已启用。正如您在下面的代码中所看到的,我已经放置了一个try-catch,但仍然没有错误,甚至控制台中没有任何会说有错误的日志。我已经在多部手机上测试过了(棉花糖、牛轧糖和奥利奥手机,不

我已经没有办法解决这个问题了。在我对我的应用程序进行调整之前,我使用的代码与API 23及以上版本中的代码相同

手机中不会触发回调(在requestLocationUpdate中) 这是棉花糖和更高版本的(但它在模拟器中运行良好,使用Pixel 2 API 26模拟器进行测试)。请注意,权限已授予/允许,GPS也已启用。正如您在下面的代码中所看到的,我已经放置了一个try-catch,但仍然没有错误,甚至控制台中没有任何会说有错误的日志。我已经在多部手机上测试过了(棉花糖、牛轧糖和奥利奥手机,不起作用。棒棒糖和Kitkat手机,起作用)

受影响的代码:

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)

    }
}

您是否也尝试过请求
ACCESS\u rough\u LOCATION
权限?请尝试在设置成功内部调用registerLocationListner()listener@ianhanniballake是访问\u粗略\u位置也是granted@abitcode已在task.addOnSuccessListener上尝试,但也不起作用:(
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'