1

I am coding an application that track and log the device location with gps, it worked perfectly on API 21 but when I switch to API 26 it stopped working.

I used the request location updates that send pending intent periodically:

public void startLocationUpdates() throws SecurityException 
{
    String provider = LocationManager.GPS_PROVIDER;


    PendingIntent pi = getLocationPendingIntent(true);
    //this method keep updating unless we specify it to stop

    mLocationManager.requestLocationUpdates(provider, 0, 0, pi);
}

I'm handling the pending intent with this receiver:

public class LocationReceiver extends BroadcastReceiver 
{
    private static final String TAG = "LocationReceiver";

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Log.d(TAG, "Receive an intent");
        Location= (Location)intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
        Log.d(TAG, this + " Got location from " + loc.getProvider() + ": " + loc.getLatitude() + ", " + loc.getLongitude());
    }
}

I think it have something to do with this line:

2019-02-08 21:41:05.872 1995-2015/system_process W/BroadcastQueue: Background execution not allowed: receiving Intent { act=com.pproject.runtracker.ACTION_LOCATION flg=0x10 (has extras) } to com.pproject.runtracker/.Controller.LocationReceiver

I'm not sure what this meant though

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • Possible duplicate of https://stackoverflow.com/questions/48265730/in-oreo-8-0-0-api-26-how-to-get-a-location-services-update-when-the-app-i – Raskilas Feb 08 '19 at 15:22
  • How you register your broadcast receiver? if you just declare in manifest without registering it programmatically this is the problem , since api 24 you must register you broadcast in code. – Maksim Novikov Feb 11 '19 at 10:51
  • @MaksimNovikov Okay, the code worked when using receiver in the java code. The problem was the broadcast was explicit. Thanks for the help – Pedro Oscar Feb 11 '19 at 12:43
  • You welcome @PedroOscar – Maksim Novikov Feb 11 '19 at 12:47

1 Answers1

2

This mean that your app cannot request/provide location data frequently without being "foreground". So you need to implement foreground service and notify about use of GPS. You shouldn't use implicit broadcasts. For one to one link I use Callback pattern and it work, for one to many you can use Messenger or EventBus.

Also I do recommend to use Fused Location Provider because it more power effective and eliminates problem like "cold start".

Raskilas
  • 691
  • 6
  • 17
  • But isn't my app already foregrounded due to "An app is considered foreground if It has a visible activity, whether the activity is started or paused." – Pedro Oscar Feb 11 '19 at 08:43
  • I updated answer, problem with implicit broadcasts https://github.com/AfzalivE/AwakeDebug/issues/3 – Raskilas Feb 11 '19 at 10:36