Android Question Does Wait For (job) JobDone(job As HttpJob) work in service while screen is off?

Inman

Well-Known Member
Licensed User
Longtime User
The background location tracking example code works fine in its own. The service stays alive and it keeps on getting GPS location as well. What I want to do is send this location to a web service to save the location data. The code to fetch location is unchanged from the example. The only addition is SendLocationData function.
B4X:
Sub GPS_LocationChanged (Location1 As Location)
    If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerMinute Then
        Dim n As Notification = CreateNotification($"$2.5{Location1.Latitude} / $2.5{Location1.Longitude}"$)
        n.Notify(nid)
        LastUpdateTime = DateTime.Now
        SendLocationData(Location1.Longitude,Location1.Latitude)
    End If
End Sub

Sub SendLocationData(Lon As Double,Lat As Double)
    Dim userid As String
  
    ToastMessageShow("Sending location data to server...",True)
    userid="21"
  
    Dim job As HttpJob
      
    job.Initialize("",Me)
    job.PostString("http://apiservice.com/location_mapper.php","user_id=" & userid & "&longitude=" & Lon & "&latitude=" & Lat)
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Log(job.GetString)
    Else
        ToastMessageShow(job.ErrorMessage,True)
    End If
    job.Release
End Sub

The above code is working fine on the tested Samsung phone, even when the screen is off. But on Realme and Redmi phones, this works only the first time i.e. when the app is installed and launched. After that, while it does get location from GPS (as notification is still there), the server is not getting data, even when screen is on.

This makes me wonder if Wait For (job) JobDone(job As HttpJob) does not work while the app is not in the foreground. Any other reason why this request is not going through?
 

Inman

Well-Known Member
Licensed User
Longtime User
Mostly energy savings. Check your app to be able to be running in the background (Android settings)

That has been checked. In fact before disabling all the power saver and battery optimising options for this app, the background location tracking service itself used to get killed in about 5 minutes after the screen is turned off. Now the service is alive, which means power saver is not an issue anymore. GPS location is also working but http post request is not going through.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
After that, while it does get location from GPS (as notification is still there), the server is not getting data, even when screen is on.
The fact that the notification is still there doesn't mean that the program is still actively running.
This makes me wonder if Wait For (job) JobDone(job As HttpJob) does not work while the app is not in the foreground
It does work.

Add some log messages and see whether the location changed event is raised.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
The fact that the notification is still there doesn't mean that the program is still actively running.

It does work.

Add some log messages and see whether the location changed event is raised.

Unfortunately the problematic device is at a remote location. My own Samsung phone doesn't have any issue.

The fact that the notification is still there doesn't mean that the program is still actively running.

What if I set notification.OnGoingEvent=True? Will that notification be cleared when the service gets killed?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Also check Settings > Protected Apps and make sure your app is there and has the setting enabled.

If not the os will kill or block the service.

Also as @KMatle mentioned check also for additional power saving tools. For example Wiki phones have this additional stuff that also blocks network access after a while.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
The above code is working fine on the tested Samsung phone, even when the screen is off. But on Realme and Redmi phones, this works only the first time i.e. when the app is installed and launched. After that, while it does get location from GPS (as notification is still there), the server is not getting data, even when screen is on.

This makes me wonder if Wait For (job) JobDone(job As HttpJob) does not work while the app is not in the foreground. Any other reason why this request is not going through?

I think the Xiaomi devices do not allow to change the power settings of apps and thus terminate them...
 
Last edited:
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
I think the Xiaomi devices do not allow to change the power settings of apps and thus terminates them...
If true it will be bummer for me, as I have only Xiaomi Phone :(
But they have very big market share so I think there must be some setting somewhere to fix it.

(No I have not tried any of the above apps, still haven't got proper free time from my paid project works)

Regards,

Anand
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
If true it will be bummer for me, as I have only Xiaomi Phone :(
But they have very big market share so I think there must be some setting somewhere to fix it.

(No I have not tried any of the above apps, still haven't got proper free time from my paid project works)

Regards,

Anand

Go to AirDroid web page for further information.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
If true it will be bummer for me, as I have only Xiaomi Phone :(
But they have very big market share so I think there must be some setting somewhere to fix it.

(No I have not tried any of the above apps, still haven't got proper free time from my paid project works)

Regards,

Anand

You can check https://dontkillmyapp.com/ for details on how to exclude your app from all the power saver features on different phones. But it is a pain to convince user to do all this and many times even after all this it still fails.
 
Upvote 0
Top