Jump to content
Bart Kindt

Android Foreground Server location access blocked

Recommended Posts

My Delphi App uses a Foreground Service to retrieve Fine Location and to transmit this to a online Server.

In Android 13, during initial installation, it asks the User to allow access to Fine location "When Using the App".

At this point, there is NO option to say "allow all the time".

Obviousely the Manifest contains the required Location entries, *and* it now also contains the line:

android:foregroundServiceType="location"

 

Despite all this, when the user puts the main App in the background, the Android OS disables the location data for the Foreground Service, and no track is generated.

 

When now going in Settings>Apps>Location, there *is* an option "allow all the time". Only when this is enabled, does the Foreground Service work as designed.

 

In addition, I see no way to detect (on startup) if this option is enabled, and as a result for most of my users, the system basically stopped working.

 

What can I do about this??

 

Edited by Bart Kindt

Share this post


Link to post

did you read this: 

 https://developer.android.com/training/location/background

https://developer.android.com/training/location/permissions

Quote

Note: The Google Play store has updated its policy concerning device location, restricting background location access to apps that need it for their core functionality and meet related policy requirements. Adopting these best practices doesn't guarantee Google Play approves your app's usage of location in the background.

Learn more about the policy changes related to device location.

 

Edited by programmerdelphi2k

Share this post


Link to post

Yes,I read all this. But this is a FOREGROUND service. Which is different. And it should work, but it does not. Because of this 'end-user' option of this "allow all the time"  which is

A) not available during initial installation and

B) If it is not set, I don't know about it and

C) if I DO know about it, I have to talk the end-user through all the steps to fix it and

D) even then after a certain amount of time, the OS may disable it again because the App has not been used for some time. Which happens all the time in my case, because the App is used for Search and Rescue purposes only, and there could be months between a SAROP.

 

There must be some way around this.

 

Share this post


Link to post

@Bart Kindt

 

  1. by default, in Android 11+++ you CANNOT ASK "foreground/background" permissions at same time!!!
    1. first one, later other!  Otherwise, the Android dont use anyone!!!

that way, you can :

procedure TForm1.BtnForegroundClick(Sender: TObject);
begin
  TPermissionsService.DefaultService.RequestPermissions( { }
  LLocationForegroundPermission,                         { }
  MyReqPerms,                                            { }
  MyRatDisplay                                           { }
    );
end;

procedure TForm1.BtnBackgroundClick(Sender: TObject);
begin
  TPermissionsService.DefaultService.RequestPermissions( { }
  LLocationBackgroundPermission,                         { }
  MyReqPerms,                                            { }
  MyRatDisplay                                           { }
    );
end;

initialization

LLocationForegroundPermission := [             { }
  'android.permission.ACCESS_COARSE_LOCATION', { }
'android.permission.ACCESS_FINE_LOCATION'      { }
  ];
//
LLocationBackgroundPermission := [                { }
  'android.permission.ACCESS_BACKGROUND_LOCATION' { in Android 11, you'll can see your "Allow all the time" option!!! }
  ];

 

Edited by programmerdelphi2k

Share this post


Link to post
7 minutes ago, programmerdelphi2k said:

Otherwise, the Android dont use anyone!!!

? 😄 open weeh-wah?

Share this post


Link to post
21 hours ago, Bart Kindt said:

In addition, I see no way to detect (on startup) if this option is enabled, and as a result for most of my users, the system basically stopped working.

 

What can I do about this??

You should be able to detect whether they've granted "allow all the time" by using:

PermissionService.IsPermissionGranted('android.permission.ACCESS_BACKGROUND_LOCATION'); 

The CrossPlatformLocation demo in Kastri has logic to ensure the user is aware that it will work only if it is granted, starting from the RequestPermissions method, here. If they've already granted sufficient permissions, it just "falls through" to starting the location services.

Share this post


Link to post

Uh, I have never heard of "

LLocationForegroundPermission

and I cannot find any references to it.

I am using this during runtime:

// cPermissionLocation = 'android.permission.ACCESS_FINE_LOCATION';

try
      TPermissionsService.DefaultService.RequestPermissions([cPermissionLocation], LocationPermissionResultHandler, DisplayLocationRationale);
    except
      on E:Exception do; // snip
    end;

Is there a special "android.permission.(LocationForegroundPermission?)"

 

Thanks, Bart

Share this post


Link to post

Re Dave: Got your reply after replying myself. I will go through the Kastri code and see how they do it.

Funny thing is, nowhere else do I find any reference to

android.permission.ACCESS_BACKGROUND_LOCATION

I cannot see it anywhere in the Android documentation. I must be looking at the wrong place...

 

Share this post


Link to post
14 hours ago, Bart Kindt said:

Uh, I have never heard of "


LLocationForegroundPermission

Have you seen this in my code?

 

...

initialization

LLocationForegroundPermission := [             {  ARRAY }
  'android.permission.ACCESS_COARSE_LOCATION', { }
'android.permission.ACCESS_FINE_LOCATION'      { }
  ];
//
LLocationBackgroundPermission := [                { ARRAY }
  'android.permission.ACCESS_BACKGROUND_LOCATION' { in Android 11, you'll can see your "Allow all the time" option!!! }
  ];

 

Share this post


Link to post

@programmerdelphi2k: Sorry, I must be blind.

 

The android.permission.ACCESS_BACKGROUND_LOCATION has fixed my problem.

Big thanks to both of you!

 

Bart.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×