Skip to content

Commit 3cea066

Browse files
committed
fix: Use LocationListenerCompat to avoid crashes on API <=30
Fixes #627
1 parent 74645d7 commit 3cea066

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

library/src/main/java/com/android/gpstest/library/data/SharedLocationManager.kt

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ import android.annotation.SuppressLint
2020
import android.content.Context
2121
import android.content.SharedPreferences
2222
import android.location.Location
23-
import android.location.LocationListener
2423
import android.location.LocationManager
25-
import android.os.Bundle
2624
import android.util.Log
25+
import androidx.core.location.LocationListenerCompat
2726
import com.android.gpstest.library.util.PreferenceUtil.minDistance
2827
import com.android.gpstest.library.util.PreferenceUtil.minTimeMillis
2928
import com.android.gpstest.library.util.hasPermission
@@ -54,25 +53,11 @@ class SharedLocationManager constructor(
5453
@SuppressLint("MissingPermission")
5554
private val _locationUpdates = callbackFlow {
5655
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
57-
// Implementation of unused methods is still required for API Level 30 and lower (#627)
58-
@Suppress("RedundantOverride") val callback = object : LocationListener {
59-
override fun onLocationChanged(location: Location) {
60-
//Log.d(TAG, "New location: ${location.toNotificationTitle()}")
61-
// Send the new location to the Flow observers
62-
trySend(location)
63-
}
64-
override fun onProviderDisabled(provider: String) {
65-
super.onProviderDisabled(provider)
66-
}
67-
68-
override fun onProviderEnabled(provider: String) {
69-
super.onProviderEnabled(provider)
70-
}
71-
72-
@Deprecated("Deprecated in Java")
73-
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
74-
super.onStatusChanged(provider, status, extras)
75-
}
56+
// Use LocationListenerCompat to avoid crashes on API Level 30 and lower (#627)
57+
val callback = LocationListenerCompat { location ->
58+
//Log.d(TAG, "New location: ${location.toNotificationTitle()}")
59+
// Send the new location to the Flow observers
60+
trySend(location)
7661
}
7762

7863
if (!context.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION) ||

0 commit comments

Comments
 (0)