Android Tutorial Background location tracking

Discussion in 'Tutorials & Examples' started by Erel, Nov 29, 2018.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    Simple example of a foreground service that keeps the process running in the background. The current location is shown in the persistent notification.

    The app starts at boot and theoretically should run all the time. It also schedules itself to run with StartServiceAt. This can help in cases where the OS kills the process.
     

    Attached Files:

  2. Multiverse app

    Multiverse app Active Member Licensed User

    Hi,
    How much important is to call
    Code:
    Private lock As PhoneWakeState
    lock.PartialLock
    How much more battery is consumed in the background after getting the partial lock?
    Thanks.
     
  3. Multiverse app

    Multiverse app Active Member Licensed User

    Also,
    Code:
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
    This means the service is scheduled to start every 30 seconds. right?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Partial lock: If you want to try and prevent the CPU from sleeping then acquire this lock. It doesn't have any effect when the device is not sleeping.
    No. 30 * DateTime.TicksPerMinute.
     
    mendiburen and Multiverse app like this.
  5. netsistemas

    netsistemas Member Licensed User

    Very important to do (don´t eliminate in sample):
    Code:
    Service.StartForeground(nid,  CreateNotification("..."))
    in Service_Start event of Tracker service

    else tracker service are destroyed.
     
    Last edited: Dec 8, 2018
  6. Multiverse app

    Multiverse app Active Member Licensed User

    Also,
    Just noticed that Service.StopForeground() is not called.
    Does system handle it automatically, or is it just a programming error?
     
  7. netsistemas

    netsistemas Member Licensed User

    I believe that is the right thing. If the service that handles the notifications stops, the rest of services will do it automatically, which would be a big problem. I suppose if you want to stop the trace service, you must close first the notification service (close Notificacion lanch in StartForeGrond).
    Excuse me and rejoice in my explanations. I'm not an expert on these issues.
    Android 8+ is more automatic: close service (service starter ignored, but no others)
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Neither. This app is intended to always run so it is never stopped. Call StopService(<service name which I don't remember>) if you want to stop it.

    BTW, it is already running for 5 days here without any interruption.
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    This is already implemented in the example.
     
    Multiverse app likes this.
  10. Christian García S.

    Christian García S. Active Member Licensed User

    Hello @Erel,

    I have a question, when you want to stop the service with StopService(Servicio) but you have:

    Code:
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
    The service will start again even when you stop service before, how I can do ? with variables ?

    Thanks
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Good question. You also need to call:
    Code:
    CancelScheduledService(Me)
     
    js1234, BillMeyer and AldoRodrigo like this.
  12. Yeskay

    Yeskay Member Licensed User

    While compiling the above said MyLocation Project am receiving the following error
    B4A Version: 8.80 BETA #1
    Parsing code. (0.00s)
    Building folders structure. (0.02s)
    Compiling code. (0.01s)
    Compiling layouts code. (0.00s)
    Organizing libraries. (0.00s)
    Generating R file. Error
    ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontVariationSettings
    ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:ttcIndex
     
  13. DonManfred

    DonManfred Expert Licensed User

    1. You should always start a new thread for your questions.
    2. Use the b4a sdk manager and update all recommeded items.
    3. Use Android.jar from Platform 28
     
Thread Status:
Not open for further replies.
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