React native android: Access location data when your app is in the background without ACCESS_BACKGROUND_LOCATION permission

The problem

Most apps don’t need to access location in the background and hence shouldn’t be requesting and using that. Apps should instead access location in the foreground using a foreground service.

When is your app considered to be using foreground location?

The system considers your app to be using foreground location if a feature of your app accesses the device’s current location in one of the following situations:

1. An activity that belongs to your app is visible.

2. Your app is running a foreground service. When a foreground service is running, the system raises user awareness by showing a persistent notification. Your app retains access when it’s placed in the background, such as when the user presses the Home button on their device or turns their device’s display off.

An app is considered to be accessing location in the background unless one of the following conditions is satisfied:

1. An activity belonging to the app is visible.

2. 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.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
...
...
...
<!-- android:foregroundServiceType="location" -->
<service android:name="com.supersami.foregroundservice.ForegroundService" android:foregroundServiceType="location"></service>
<service android:name="com.supersami.foregroundservice.ForegroundServiceTask" android:foregroundServiceType="location"></service>
</application>
</manifest>

 by the author.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store