我已经用完了如何解决这个问题的可能方法。在对我的应用程序进行调整之前,我在技术上使用的是在 API 23 及更高版本中工作的相同代码。
回调(在 requestLocationUpdate 中)不会在棉花糖及以上的手机中触发(但它在模拟器中工作正常,使用 Pixel 2 API 26 模拟器进行测试)。请注意,权限已被授予/允许,并且 GPS 也已启用。此外,正如您在下面的代码中看到的那样,我已经放了一个 try catch,但控制台中仍然没有错误,甚至没有任何日志表明存在错误。我已经在多部手机中进行了测试(棉花糖、牛轧糖和奥利奥手机,不工作。棒棒糖和奇巧手机,工作)
受影响的代码:
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)
}
}
另外我的 gradle 只是为了显示我的依赖项/使用的框架
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'