Android Question Run GPS Tracker Service in Sleep Mode

AndroidMadhu

Active Member
Licensed User
Hello,
I am trying to develop GPS Tracker Service, and my app is running fine when the App is Alive.
But when the App is in Sleep mode or in background the GPS tracker is not working and not providing any lat/long after some time.

The below link I am following but it is not working. After 1 minute of sleep the GPS tracking stop sending lat/long.

https://www.b4x.com/android/forum/threads/continous-background-gps-tracking.110628/#content

https://www.b4x.com/android/forum/threads/background-location-tracking.99873/

The below is the code block:
B4X:
 Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
GPS.Initialize("gps")
lock.PartialLock
End Sub

Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(nid, CreateNotification("..."))
StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
Track
End Sub

B4X:
 Public Sub Track
If Tracking Then Return
If Main.rp.Check(Main.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
Log("No permission")
Return
End If
GPS.Start(0, 0)
Tracking = True
End Sub

Sub GPS_LocationChanged (Location1 As Location)
Dim strLoc As String
If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerSecond Then
Dim n As Notification = CreateNotification($"$2.5{Location1.Latitude} / $2.5{Location1.Longitude}"$)
n.Notify(nid)
LastUpdateTime = DateTime.Now
gpslat = Location1.Latitude
gpslong = Location1.Longitude
If gpslat>0 And gpslong>0 Then
'CallSubDelayed2(Main,"Update_Location",Location1)
Dim bc As ByteConverter

strLoc=gpslat & "/" &gpslong '& qrActivity.sessionId ''''"newloc_" &
If Starter.mqtt.Connected Then

Starter.mqtt.Publish("driver_" & qrActivity.sessionId,bc.StringToBytes(strLoc ,"UTF8"))

Else
Log("MQTT not connected")
End If
ToastMessageShow(gpslat & "/" &gpslong,True)
End If
End If
End Sub

Please advice on this.....

Thanks
 

TILogistic

Expert
Licensed User
Longtime User
Read:

 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Hello,
@oparra ..... I have read the above doc.
But when I am adding the below parameter at manifest file I am getting the error during build:
B4X:
SetServiceAttribute(Tracker, android:foregroundServiceType, "location")

The Error :
B4X:
AndroidManifest.xml:67: error: No resource identifier found for attribute 'foregroundServiceType' in package 'android'

AAPT path: C:\Android_B4A\tools\..\build-tools\27.0.1\aapt.exe
Exit code:1

Please advice
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
you are using this method
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I am using B4A v 9.80
Start with updating to 10.2
Also reinstall the SDK as described in the 10.2 Update

Also note that you need to set the path to android.jar from platform 29 in Tools-Configure Paths

Follow the installations instructions cartefully: https://www.b4x.com/b4a.html
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
version 10.2




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="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.

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

Alex_197

Well-Known Member
Licensed User
Longtime User
I have updated to 10.2. Now after going to background the App working for 5 mins. Then GPS stop working.

please advice on this please....
Did you run your code in Debug mode or in Release? In Debug - it will not work in background more than 5 minutes.
If it's 5 minutes in Release mode - try to implement a time into Starter - I played with it and it works for for 30 minutes (I don't need more).

Also see this thread - similar to your problem how to add a timer https://www.b4x.com/android/forum/t...g-put-to-sleep-by-android.123903/#post-774138
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…