FusedLocationProviderClient requestLocationUpdates不会触发上述API 23的LocationCallBack

时间:2018-10-24 04:27:42

标签: android kotlin geolocation google-location-services fusedlocationproviderclient

关于如何解决此问题,我已经用尽了所有可能的方法。从技术上讲,在我对应用程序进行调整之前,我使用的是与API 23及更高版本相同的代码。

回调(在requestLocationUpdate中)不会在电话中触发 (棉花糖及更高版本)(但在模拟器中运行正常,使用Pixel 2 API 26模拟器进行了测试)。请注意,权限已被授予/允许,并且GPS也已启用。另外,正如您在下面的代码中看到的那样,我已经尝试了捕获,但仍然没有错误,甚至控制台中的任何日志都说有错误。我已经在多部手机(棉花糖,牛轧糖和奥利奥手机,无法使用。棒棒糖和奇巧手机,正在使用)中进行了测试

受影响的代码:

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)

    }
}

我的荣幸也只是为了展示我的依赖关系/使用的框架

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'

0 个答案:

没有答案