locked
Continue tracking location of app when in background? RRS feed

  • Question

  • User378560 posted

    We are developing a cross-platform application in which we want to continue tracking the user even if the app is in the background. We require that the

    We have run into situations in which the app stops tracking. When examining the logs we notice that there will be a long period of no logging. We are logging 50 things a minute and then there is a 14 minute gape.

    I understand that the OS (especially iOS) can suspend or kill an app if the OS needs to do this.

    My questions:

    Can we log when an app is going to be killed, and when it is going to be suspended? What can we do, if anything, to prevent the app from being suspended or killed?

    I am fairly new to Xamarin. Any guidance would be greatly appreciated.

    Bryan

    Friday, May 17, 2019 9:53 PM

Answers

  • User371688 posted

    When we check the android document:Background Location Limits, we will see

    In an effort to reduce power consumption, Android 8.0 (API level 26) limits how frequently background apps can retrieve the user's current location. Apps can receive location updates only a few times each hour.

    Note: These limitations apply to all apps used on devices running Android 8.0 (API level 26) or higher, regardless of an app's target SDK version.

    However?—?this doesn’t mean that there is no solution: applications like Google Maps etc still use your location while being on the background, so there has to be a solution. The same page gives you some hints

    The system distinguishes between foreground and background apps. An app is considered to be in the foreground if any of the following is true:

    It has a visible activity, whether the activity is started or paused. It has a foreground service. Another foreground app is connected to the app, either by binding to one of its services or by making use of one of its content providers. For example, if a foreground app binds to any of the following components within another app, that other app is considered to be in the foreground: Input method editor (IME) Wallpaper service Notification listener Voice or text service If none of those conditions is true, the app is considered to be in the background.

    So as a solution: create a foreground service!

    For more details: https://hackernoon.com/android-location-tracking-with-a-service-80940218f561

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, May 20, 2019 8:02 AM
  • User371688 posted

    When the service is in the background, you can try to restart it by using a BroadcastReceiver which will receive a signal when someone or something kills the service; its role is to restart the service.

    For more details, you can refer to: https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android

    Even though it's Java code, it's easy to understand and convert

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 22, 2019 7:02 AM

All replies

  • User371688 posted

    When we check the android document:Background Location Limits, we will see

    In an effort to reduce power consumption, Android 8.0 (API level 26) limits how frequently background apps can retrieve the user's current location. Apps can receive location updates only a few times each hour.

    Note: These limitations apply to all apps used on devices running Android 8.0 (API level 26) or higher, regardless of an app's target SDK version.

    However?—?this doesn’t mean that there is no solution: applications like Google Maps etc still use your location while being on the background, so there has to be a solution. The same page gives you some hints

    The system distinguishes between foreground and background apps. An app is considered to be in the foreground if any of the following is true:

    It has a visible activity, whether the activity is started or paused. It has a foreground service. Another foreground app is connected to the app, either by binding to one of its services or by making use of one of its content providers. For example, if a foreground app binds to any of the following components within another app, that other app is considered to be in the foreground: Input method editor (IME) Wallpaper service Notification listener Voice or text service If none of those conditions is true, the app is considered to be in the background.

    So as a solution: create a foreground service!

    For more details: https://hackernoon.com/android-location-tracking-with-a-service-80940218f561

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, May 20, 2019 8:02 AM
  • User378560 posted

    I have a follow on question. We also face the problem of the OS killing our app if it is in the background. Ideally, we would want to never be killed. Does making a foreground service stop the OS from killing the app? If not, is there another way to stop this or at least make it much more unlikely?

    Monday, May 20, 2019 4:41 PM
  • User371688 posted

    When the service is in the background, you can try to restart it by using a BroadcastReceiver which will receive a signal when someone or something kills the service; its role is to restart the service.

    For more details, you can refer to: https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android

    Even though it's Java code, it's easy to understand and convert

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 22, 2019 7:02 AM