How to set FLAG_IM/MUTABLE for FusedLocationProviderClient in Kotlin

178 Views Asked by At

Im currently trying to get live location of the device to Display it on am android compose google map and after trying out alot of different tutorials and also chatgpt :) I got stuck at this Error more then one time:

java.lang.IllegalArgumentException: com.example.myapplication: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
                                                                                                    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

I couldnt find any solution to this yet so maybe someone can help me. My Code that calls the FusedLocationProviderClient looks like this :

private lateinit var fusedLocationClient: FusedLocationProviderClient

class MainActivity : ComponentActivity() {
    private var fusedLocationClient: FusedLocationProviderClient? = null
    private var locationRequest: LocationRequest? = null
    private var locationCallback: LocationCallback? = null
    @SuppressLint("MissingPermission")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Greeting(name = "Yeet")

                }
            }
        }
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        buildLocationCallBack()
        buildLocationRequest()
        fusedLocationClient!!.requestLocationUpdates(
            locationRequest!!,
            locationCallback!!,
            null
        )
    }

The buildLocationRequest function:

private fun buildLocationRequest() {
    locationRequest = LocationRequest()
    locationRequest!!.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
    locationRequest!!.setInterval(100)
    locationRequest!!.setFastestInterval(100)
    locationRequest!!.setSmallestDisplacement(1f)
}

And finally the buildLocationCallBack function:

private fun buildLocationCallBack() {
    locationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult) {
            for (location in locationResult.locations) {
                val latitude = java.lang.String.valueOf(location.latitude)
                val longitude = java.lang.String.valueOf(location.longitude)
                Log.d("LOCATION",latitude)
                Log.d("LOCATION",longitude) //Currently just logging to test
            }
        }
    }
}

I gave the app permissions (Fine and coarse) in the Manifest.xml and inside the emulator (Location Permission).

Thanks for any help!

val locationProviderClient = LocationServices.getFusedLocationProviderClient(this)
val locationRequest = LocationRequest().apply {
    interval = 10000
    fastestInterval = 5000
    priority = LocationRequest.PRIORITY_HIGH_ACCURACY

}
val intent = Intent(this, MyLocationReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
locationProviderClient.requestLocationUpdates(locationRequest, pendingIntent)

I tried using this location with intents and specifying the "PendingIntent" but it had the same error.

1

There are 1 best solutions below

2
On

I had the exact same problem a while ago and I'm pretty sure I fixed it by changing FLAG_IMMUTABLE to FLAG_MUTABLE.

So for you, try changing:

val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

to

val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_MUTABLE)

I'm curious to know if it works so let me know please, thank you! (Sorry if it doesn't xD)