delight-im / Android-SimpleLocation

Utility class for easy access to the device location on Android
Apache License 2.0
200 stars 75 forks source link

Location tracking failed on background #28

Closed MuthuSubramaniam closed 6 years ago

MuthuSubramaniam commented 6 years ago

Hi Team, this library simplified lot of works. But in my application i am using background service to update user current location using alarmManager & Service.

**1. If user is in online then i send data to server. If not storing into Room Database.

  1. Every 5 minutes i updating location details.
  2. If i am in travel, after reached new location its not updating new location, when i open my app then . only its updating new location and sending to server**

Please help me if i am wrong. Thanks!

` class AppService : Service() { var NOTIFICATION_ID = "notification-id"

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
    super.onStartCommand(intent, flags, startId)

    val context = this
    val location = SimpleLocation(context)

    val address = getAddress(context, location.latitude, location.longitude)

    if (isConnectedOrConnecting(context)) {

        AppUtil.addAttendance(getLatLng(context), address, context)

        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val notification = getNotification(context, "Hai, sent your location$address")
        val id = intent.getIntExtra(NOTIFICATION_ID, 0)
        notificationManager.notify(id, notification)
        roomDB = Room.databaseBuilder(this, LocationDataBase::class.java, "location_db").allowMainThreadQueries().build()
        val list = roomDB.locationModel().listOfLocation

        sendSavedDataToServer(list, this)
    } `
ocram commented 6 years ago

This is not specific to this library. Instead, it’s simply related to your app being in the background, as you noted, and perhaps also to App Standby and Doze.

Location updates are provided to background apps only a few times each hour.

https://developer.android.com/about/versions/oreo/background-location-limits

Perhaps you may be able to use Context.startForegroundService.