Foreground service not receiving location updates in Android 7.0+ when screen is off

前端 未结 5 1525
面向向阳花
面向向阳花 2021-02-04 03:29

I am trying to create an Android application that continuously logs device location data in realtime while the device screen is off. My code works correctly with Android 6.0 and

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 03:45

    I had the same issue and I have found a working solution. In order to listen to location updates, you should NOT call this method:

    public Task requestLocationUpdates (LocationRequest request, 
    LocationCallback callback, Looper looper)
    

    You should use a pending intent instead of a callback, i.e. you should call the method:

    public Task requestLocationUpdates (LocationRequest request, 
    PendingIntent callbackIntent)
    

    Checkout the link to read more: https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderClient.html#requestLocationUpdates(com.google.android.gms.location.LocationRequest, android.app.PendingIntent)

提交回复
热议问题