Restrictions to background location and foreground services in Android 11

Restrictions to background location and foreground services in Android 11

Android 11 is officially out! There are many changes related to privacy you should know about and it’s good to check your apps for potential flaws. It’s also good to know what are the current platform restrictions.

I’d like to focus on important change in Android 11 that can affect your app if you are using location and foreground service. What’s important, it doesn’t matter if you are targeting your app to API29 or to the latest API30. If you are using device with Android 11 it’s also good to know how to deal with location permissions and when your app can be blocked.

Let’s get back to Android 10 for a moment.

Android 10 (API level 29) introduced a number of features and behavior changes to better protect users’ privacy. Read privacy changes docs.

To support the additional control that users have over an app’s access to location information, Android 10 introduces the ACCESS_BACKGROUND_LOCATION permission.

Unlike the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions, the ACCESS_BACKGROUND_LOCATION permission only affects an app’s access to location when it runs in the background. An app is considered to be accessing location in the background unless one of the following conditions is satisfied:

  • An activity belonging to the app is visible.
  • The app is running a foreground service that has declared a foreground service type of location.To declare the foreground service type for a service in your app, set your app’s targetSdkVersion or compileSdkVersion to 29 or higher. Learn more about how foreground services can continue user-initiated actions that require access to location.

TL;DR: It’s clear. You can use location in the app only when you are showing the user your app, user can see what’s going on and is aware of that. That’s why foreground service type was introduced.

Use case: GPS Tracker

Let’s talk about real example here. I’m working on an app that is recording trips (routes) with GPS. Change that was made in Android 10 wasn’t problematic for us. We are always showing user some UI that the app is working – notification (so we run foreground service working with active notification)

Question: Do we need to use ACCESS_BACKGROUND_LOCATION permission?

No. We are showing notification to the user, user can see that the app is working, everything is fine. And we are asking only for location Allow only while using the app. We don’t need background permission. That’s great!

Auto trip recording

In our app we have some feature called “autostart“. User can start recording automatically. For example when entering their vehicle. Simple BROADCAST RECEIVER matched with Bluetooth.

You can set this up. When you are connected to some BT device, app is starting recording route. We are starting foreground service, showing info to user.

Question: Do we need to use ACCESS_BACKGROUND_LOCATION to start GPS recording with BROADCAST_RECEIVER?

Android 10: No. Start foreground service, get location with foreground service.

Android 11: Yes. You can’t use location even with foreground service when it was started by BROADCAST_RECEIVER. Broadcast Receiver for Android 11 is background process.

When trying to start foreground service with Broadcast Receiver you’ll get this error:

Foreground service started from background can not have location

Solution: You need to get ACCESS_BACKGROUND_LOCATION for your app. Even when using foreground service. The same foreground service will work without having this permission if user stars it manually from your app (e.g. clicking record button in your app).

Changes in Android 11

At Android Developers Summit ’19 Dianne Hackborn said that “Abusing foreground services is becoming big issue and we have to do step to address that…

I believe this is the “step“.

Foreground services in Android 11

Source: /11/privacy/foreground-services

Android 11 changes when foreground services can access the device’s location, camera, and microphone. This helps protect sensitive user data.

Restrictions to access while in use

Restrictions to access while in useIf your app starts a foreground service while running in the background, the foreground service cannot access the microphone or camera. Additionally, the service cannot access location unless your app has background location access.

Asking for location permission in Android 11

I believe that in most cases you don’t need ACCESS_BACKGROUND_LOCATION for your app. It’s always good to analyse your needs here. If you need it for your use case – be aware of other changes here. You can’t ask for this permission directly. And this is another big change in Android 11.

Android 10 vs Android 11

If you are using Android 10 and you’ve added ACCESS_BACKGROUND_LOCATION to your manifest you’ll see this prompt:

Asking for BACKGROUND_PERMISSION in Android 10

Note: You’ll see Allow all the time permission if you are using Android 10.

When using Android 11 you won’t see option for Allow all the time anymore.

Get consent for background location in Android 11

This is Google’s suggestion how to deal with it. Starting Android 11 you need to explain user why you need background permission and you can’t get consent with just a click.

Always explain user why background location permission is needed and direct user to app’s settings screen.

You need to redirect user to app’s settings screen.

User is forced to manually change permission for your app (that’s why explaining him why he should do that is so important)

There’s a great codelab regarding location permission in Android 10 and Android 11. I suggest to check Receive location updates in Android with Kotlin.

This article was also published on Medium.

Android Dev Summit 2019 – my recap

Android Dev Summit 2019 – my recap

I was attending Android Dev Summit 2019 in Sunnyvale and I’d like to share my thoughts after the event. It’s my small recap of my experience. But let’s start from the beginning.

Android Dev Summit and Google I/O. Why do we need two Android events from Google?

You can’t compare these two events. They are completely different. I’m starting from this because I know that in #androiddev community it’s not obvious why do we have Android Dev Summit and why it’s not a part of Google I/O.

I’ve also been to Google I/O so I can tell the difference from my own experience.

Google I/O is probably the developer conference (especially if you are into Google’s technologies) but it’s more like a festival. I can’t compare this event to any other event I’ve been to but if you are a developer and you are focused around Google’s technologies – it’s like being at the best music festival with your favorite lineup.

If you are Android developer – Google I/O is important, but it’s focused on announcements. Of course you’ve got access to Googlers (networking is another best thing I can think of) but it’s not focused only on Android and it’s future.

Android Dev Summit is a conference focused only on Android, its framework and future. It’s organised by people directly involved in the Android development. You’ve got access to Googlers directly working on Android and all its parts (Jetpack, Play Store, Android Studio, Android for Cars…). It’s 2-day event and the goal here is to invite Android developers and give them deep technical talks about platform and coming changes to the platform. Also it’s the right moment to get feedback from them. This is important, I was surprised how all teams are open here and waiting for your feedback (greetings to Ian Lake)

For almost two days you’ve got access (Office hours) to all Android teams waiting for your questions.

If you are not attending the event in person – don’t worry – keynote is live streamed on YouTube and all other talks are shared on YouTube few days after the event. It’s one week since the event and you can already watch them here (all Android Dev Summit 2019 sessions)

Networking

Just like at Google I/O – it’s a good place to meet (new) people know only from the internet. A lot of GDEs (Google Developer Experts) and people working in companies located in Sillicon Valley area.

Breaks between talks were also really awesome – it was a perfect moment to share and exchange your thoughts with others about the topic.

Let’s get back to the main topic – Android

My favorite points from Android Dev Summit 2019

This part is especially for Android developers. I’ll skip keynote here but I think you should start watching talks from ADS from keynote to have a better overview.

If you are Android Developer, make sure to watch this:

  • Jetpack Compose
    I think everyone is following this topic and if you are Android developer you should know it’s the future. It’s good to know about upcoming changes here
  • Preparing for Scoped Storage
    This is a really big change in Android. I’m not sure why it is ignored by many Android developers. Make sure to watch this video and think how you should change your app to be ready for this.
    In my opinion it’s one of the biggest changes coming to Android. I don’t feel comfortable here with new APIs and I hope Google will give us more examples how we should use it and which API (this is my main concern).
  • LiveData with Coroutines and Flow
    I feel Flow will be trending topic on Android’s dev conferences.
  • Motion Layout
    Make sure to watch to this session (“What’s New in Android Studio Design Tools”). I think this is huge!
  • Understanding Compose
    Interesting, deep technical talk I didn’t understand when I was there. I needed to watch it again and probably I’ll do it again.
    I think it can be a game changer and we’ll see more talks about it on Android conferences.
  • Developing Themes with Style
    Crash course in themes and styles. And if you know it’s made by Nick Butcher and Chris Banes – you know it’s gonna be good. And it was it. One of my favorites.
  • What’s new in Android Studio 4.x
    I think it’s important to watch this session. Probably you are not using canary versions in Production but make sure you watch this to see what new tools you’ll get it.
  • An Opinionated Guide to Dependency Injection on Android
    OK, maybe it wasn’t real deep technical talk but you’ll learn here what’s the future of DI in Android and why you should still stick with Dagger. This is a good thing. I was waiting for this.
    TL;DR: Google knows dagger.android wasn’t the best thing they made for Android and in future they’ll try to give us better solution here. No details. I think we need to wait a while for more detailed info.
    PS. There is new codelab from Google for Dagger
  • Fragments: Past, Present, and Future
    More changes coming to Fragments. Be sure to watch it.

I was also pleasantly surprised with these talks:

If you are into Android development make sure to watch sessions from this event. Simply start from the keynote.

Report bug, use official issue tracker

This is something I also learned there. Google is listening to your feedback and there’s no better way of reporting it than official issue tracker.

If you think they are not reading it – you are wrong! It’s reporting issues on Twitter that is not working 😉

Android Auto / Android for Cars

I was looking forward to see some news regarding Android Auto / Android for Cars. Unfortunately, no news here. There was no Android Auto team present. There was only Android for Cars team presenting latest version of Android for Cars in Volvo XC40. But still the platform is closed for third party apps (other than multimedia and instant messaging).

You can see short presentation here: How to Build Media Apps for Cars (Android Dev Summit ’19)

I hope this situation will change in near future. For us, it’s the only one closed mobile platform for our navigation (Apple opened this gate before Google).

 

Some photos from the event