Android Question Background service tracker killed

Discussion in 'Android Questions' started by Mike1970, Jun 4, 2019.

  1. Mike1970

    Mike1970 Active Member Licensed User

    Hi everyone, i implemented this code in my app to track the position in background:
    https://www.b4x.com/android/forum/threads/background-location-tracking.99873/


    But after 4 hours it stopped working... what could be the problem? in the "Service_Create" of the tracking i have:

    Code:
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
        GPS.Initialize("gps")
        
    lock.PartialLock
    And i put this in the Starter:

    Code:
    Sub Service_TaskRemoved
        
    'This event will be raised when the user removes the app from the recent apps list.
        StopService(Tracker)
    End Sub
     
    Last edited: Jun 4, 2019
  2. Erel

    Erel Administrator Staff Member Licensed User

    What happens with the example as-is? Does it stop after 4 hours?
     
  3. Mike1970

    Mike1970 Active Member Licensed User

    i did't try to run for 4 hours that one
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You should.
     
    Mike1970 likes this.
  5. Mike1970

    Mike1970 Active Member Licensed User

    Now i'm tring, it's working good for now (it seems)

    I noticed that implemented in an real application the SO says that the process is draining the battery too much, and when there is that notification the Tracking is disabled, i think the two things are connected...
    What do you think?

    Edit i unlocked the phone and the "Gps in use" icon (and the notification with longitude etc...) are gone.. but no trace of the SO notification.
    As soon as i resumed the app from task manager the gps restarted
     
    Last edited: Jun 4, 2019
  6. Erel

    Erel Administrator Staff Member Licensed User

    Keeping the GPS on can drain the battery. Many manufacturers don't allow apps to run too long in the background, even if there is a foreground service. Especially if the app consumes a lot of power.

    I don't know what exactly are you trying to implement however it might be better not to use the GPS. You can start your app every hour and check the last known location with FusedLocationProvider.
     
    Mike1970 likes this.
  7. Mike1970

    Mike1970 Active Member Licensed User

    I already substitute the gps with the Fused location library, i followed the example “GoogleMaps and FusedLocationProvider” keeping only what i need.
    And it closes in the same way
     
  8. Mike1970

    Mike1970 Active Member Licensed User


    Ok, now i stop the FusedLocationProvider for 5 minutes and i start it the necessary time to trigger the location_changed one time.

    I went to sleep while the phone was running the app, but when i woke up the SO killed all the services (starter too), i went to the phone settings and i saw that there was my app running with: 1 process and 0 services
    and it should be: 1 process and 3 Services

    What wrong is going on?? D:
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    This means that it was killed.

    As I wrote, many devices do not allow apps to run too long in the background. You can switch to using StartServiceAt with 30+ minutes interval and a foreground service that runs for a limited time.
     
  10. Mike1970

    Mike1970 Active Member Licensed User

    Sorry Erel, i didn't understand well.
    I modified yours GPS Tracker example, and in that service there is the "StartServiceAt" already.


    Code:
    Sub Service_Start (StartingIntent As Intent)
            
    Service.StartForeground(nid, CreateNotification("Wait ..."))
            LatRad = 
    0.0
            LonRad = 
    0.0

            
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)     '<-----

            timerGPSenabling.Enabled = 
    False 'this code set to false the timer that (after getting a location) triggers every 5 minutes, to enable and disable the GPS
            Track
            
    StartService(gpsChecker) 'This service is a standard service with a timer that every 5seconds checks if the gps is enabled
    End Sub
    What do you mean I have to do?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    The expected behavior is that StartServiceAt will cause the app to start. It is possible that the OS doesn't let it. Check your device settings and see whether there is an option to whitelist your app.
     
  12. Mike1970

    Mike1970 Active Member Licensed User

    Yes it was supposed to do this.
    But sometime it stain on for 7 hours, other time for an hour, it's random, the process is killed... i don't understand why, i have to try another device and see what happens..

    I found a funciton named "App Lock" but it's disabled
     
  13. Mike1970

    Mike1970 Active Member Licensed User

    I tried to add this permissions

    Code:
    AddPermission(android.permission.FOREGROUND_SERVICE)
    AddPermission(android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
    i tried to use a sticky-service

    But nothing works. the OS still kill all services
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Sticky services should not be used.

    You should try to start your app once every several hours and close it shortly after.
     
  15. Mike1970

    Mike1970 Active Member Licensed User

    Yes i know that they are substitute with "StartServiceAt", but it was only a test.


    What does it mean?
    If i close the app all things just stops.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

  17. Mike1970

    Mike1970 Active Member Licensed User

    Already implemented... nothing



    Yes, but doing this i make the purpose of the app useless..
    The app have to stay opened for hours, in order tu track the user location every 5 minute (enabling an disablin the gps)

    now i'm not understanding you tip erel :(
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that there is anything that you can do to make it run longer. As written above several times, many devices will not let you run too long in the background.
     
  19. Mike1970

    Mike1970 Active Member Licensed User

    And there is any way to detect if it kills it?
     
  20. OliverA

    OliverA Well-Known Member Licensed User

    Just throwing an idea out. If the phone is connected to the network constantly, then use something like Firebase Cloud Messaging to sent a message to the device at a particular interval (in your case every five minutes). This way the app would not have to be running at all and the incoming FCM message would wake up the app, it can do it's processing and just stop again. Some pointers:
    https://www.b4x.com/android/forum/t...d-background-data-notification.73356/#content
    https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/
    BTW, I'm pretty sure I've read @agraham state that he has no background apps running and does all his processing via FCM. I guess with this type of solution, you would need something that would generate the FCM message(s) at the given interval desired.
     
    Mike1970 likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice