Android Question Background Location/Tracking and Android 10+

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello community,

In this (https://www.b4x.com/android/forum/threads/background-location-tracking.99873/#content) thread, @Erel talks about the new implementation of GPS background tracking and Android 10+, which requires the line SetServiceAttribute(Tracker, android:foregroundServiceType, "location") in Manifest to request to the user to choose if allows or not background tracking. This woks fine if I use target SDK 28 and Android 10, but doesn't work if I use <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="29"/> (sdk 29, as required by Google).
See the screens:
First Image: Target sdk 29 + Android 10 Second Image: Target SDK 28 + Android 10 - same Manifest .
Screenshot_2020-10-11-15-50-04-742_com.google.android.permissioncontroller.jpg
Screenshot_2020-10-11-15-53-13-939_com.google.android.permissioncontroller.jpg

The SetServiceAttribute(Tracker, android:foregroundServiceType, "location") was added in manifest editor in both cases. I really don't know how to request to user the GPS background authorization in SDK 29 + Android 10 and even didn't find it in the forum.
Does anybody know what am I doing wrong?

Thanks!
 

Attachments

  • Screenshot_2020-10-11-15-50-04-742_com.google.android.permissioncontroller.jpg
    Screenshot_2020-10-11-15-50-04-742_com.google.android.permissioncontroller.jpg
    180 KB · Views: 348

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Simply security and permission policies.

The user will have control of the permissions of the location of their device.

The location can be done in the background.

Only the device will notify the user that there is a background app that they have given permission to.
You mean that if the service is started with startforeground the icon will be showed and a single user authorization type is enough ? That's it?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The clue seems to be in this statement:
  • If you don't need location access in the background, remove it.
    If your app targets Android 10 (API level 29) or higher, remove the ACCESS_BACKGROUND_LOCATION permission from your app's manifest. When you remove this permission, all-the-time access to location isn't an option for the app on devices that run Android 10.
(Link: https://developer.android.com/training/location/background#evaluate)

Even adding this permission to the Location example provided by @Erel does not give you the all-the-time access permission. And if you check out the code to the Tracker service module, it makes sense. Tracker is not a background task, since it requests Service.StartForeground() in the Service_Start method and is therefore considered a FOREGROUND service. All foreground Activities/Services will only get the while-in-use and deny options. To get the all-the-time access option, the permission must be requested in a background task. More information on background tasks: https://developer.android.com/guide/background

I'm sorry that I have not provided a clear cut solution here, but at least the output that Android 10 produces seems to be consistent with the provided Android developers documentation (and is therefore not a bug).
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Further testing (as of now) shows that the all-the-time access option is not necessary for the My Location testing application. As long as the app runs and it has the while-in-use permission, the app receives GPS updates, even if the Activity is sent to the background. Actually, closing the activity (at least in my case) via the Task selector does not stop the foreground Tracker service and therefore the My Location app continues to receive GPS updates via the GPS library. I have to actually go the the Applications list in settings and force stop the application before GPS updates to the application stop.

Tested with:
B4A 10.2
Android SDK 29
JDK OpenJDK 11.0.1 (From AnywhereSoftware's B4A page)

Application running on
Samsung Galaxy S9 (SM-G960U)
One UI version 2.1
Android Version 10
Kernel version 4.9.186-18803994

This is the B4A Manifest file
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>
)
'<!-- Required only when requesting background location access on
'Android 10 (API level 29) and higher. -->
'AddManifestText(
'<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
')

SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.

SetServiceAttribute(Tracker, android:foregroundServiceType, "location")
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Further testing (as of now) shows that the all-the-time access option is not necessary for the My Location testing application. As long as the app runs and it has the while-in-use permission, the app receives GPS updates, even if the Activity is sent to the background. Actually, closing the activity (at least in my case) via the Task selector does not stop the foreground Tracker service and therefore the My Location app continues to receive GPS updates via the GPS library. I have to actually go the the Applications list in settings and force stop the application before GPS updates to the application stop.

Tested with:
B4A 10.2
Android SDK 29
JDK OpenJDK 11.0.1 (From AnywhereSoftware's B4A page)

Application running on
Samsung Galaxy S9 (SM-G960U)
One UI version 2.1
Android Version 10
Kernel version 4.9.186-18803994

This is the B4A Manifest file
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
)
'<!-- Required only when requesting background location access on
'Android 10 (API level 29) and higher. -->
'AddManifestText(
'<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
')

SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.

SetServiceAttribute(Tracker, android:foregroundServiceType, "location")
It looks that Google/Android is still testing the behaviors and permissions for location services. For me, up to Android 9, didn't matter if target sdk was 28 or 29. There was a "3 questions" box (Allow all the time, Allow during use and not allow).
When upgraded to Android 10 and sdk 29 the box has now only 2 options (allow during use and don't allow). But it looks , as you reported, that "allow during using" option in Android 10 and sdk 29 isn't so restrictive... let's see what Google will do in the future. Considering that we are small players and Google IS Google I'm always afraid about what could be decided by them. :rolleyes:
 
Upvote 0

delgadol

Member
The problem of using the services in the background; it really depends on what version of android you are using; I mean what brand; XIAOMI; HUAWEI have the most aggressive customization layer I have ever worked with; many applications that need to run in the background; in these particular layers they fail.

I have argued a lot with clients; they tell me: But if my pixel runs normal; why not in my xiaomi ?; And even if you meet all the programming requirements to run in the background; in these layers there is always some difficulty; And it's not just B4X; is others compiled in Android Studio; IONIC.

Now I use 2 or 3 different models of equipment to test my works; always rubbing my fingers when I need to test on xiomi.
 
Upvote 0
Top