Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API issues stemming from Android 13/14 changes to location services #210

Merged
merged 4 commits into from
May 25, 2024

Conversation

lukaskurz
Copy link
Contributor

What was the problem

On newer Android 13 and 14 Devices, the startLocationService() function causes a fatal crash, causing the app to exit and show the error mentioned in #200.

How was it fixed

In the linked issue is a fork that already adresses 1 one of the issues caused by the version change, namely the extra flag for the reicever exported status, but it has some other issues that arise from Android 13 as well.

I fixed the registerReceiver call by making a API level check and depending on version code, call the function with or without the flag, in order to still stay compatible with API level 26 and below where the flag does not exist, inspired by this StackOverflow answer.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    registerReceiver(broadcastReceiver, filter, RECEIVER_EXPORTED)
} else {
    registerReceiver(broadcastReceiver, filter)
}

There was also another issue, caused be new permission requirements for the foreground service, where now need to more precisely define what kind of foreground service it is, which I added to the manifest.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

We also need to again change the function call signature of the foreground service to add the new permission type based on API level as before, inspired by this StackOverflow answer.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    startForeground(NOTIFICATION_ID, notification.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION)
} else {
    startForeground(NOTIFICATION_ID, notification.build())
}

@BWMuller
Copy link
Contributor

@MoralCode @Almoullim For what it is worth, I can confirm that the changes in this PR are working as expected and update the needed parts to allow for android 14 correctly.

@lukaskurz Only extra bit that would be useful is to update the README at the bottom to also include the additional uses-permission so that it stays in sync there

@MoralCode
Copy link
Collaborator

@MoralCode @Almoullim For what it is worth, I can confirm that the changes in this PR are working as expected and update the needed parts to allow for android 14 correctly.

sounds good!

@lukaskurz Only extra bit that would be useful is to update the README at the bottom to also include the additional uses-permission so that it stays in sync there

Once this gets updated then I'll see how it compares to the other open PR's (i admit I'e not been paying much attention lately) and get this merged

@lukaskurz
Copy link
Contributor Author

@MoralCode i changed the permissions in the readme, should be fine now

@MoralCode
Copy link
Collaborator

aight, trial run lets see how this goes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants