• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Android Oreo service - event of location change is not being fired when application is in background

impeham

Lurker
Sep 22, 2018
2
1
I am trying to create a service to keep running even when the application is not at foreground. when it is in foreground, all works well, but after switching away from it, the onLocationChanged is being called once every ~10 minutes (instead of 5 seconds when in foreground).

The goal is to keep scanning the location and send updates for processing even when the application is in background every 5 seconds (I want the service to keep running as long as the UI applications is not being totally closed).

I tried following many guides and posts in the net and none of them helped solving this. in the past it was working fine with android 6.0, but I upgraded to 8.0 and it stopped working.

This is the code I'm using:

public class MainActivity extends AppCompatActivity {
private Intent _scanServiceIntent;

@override
public void onStart()
{
super.onStart();
_scanServiceIntent = new Intent(this,ScanService.class);
bindService(_scanServiceIntent, m_serviceConnection, BIND_AUTO_CREATE);
}

private ServiceConnection m_serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
_scanService = ((ScanService.ScanServiceBinder)service).getService();
}

public void onServiceDisconnected(ComponentName className) {
Log.v("lsx", "onServiceDisconnected");
_scanService = null;
}
};
}


This is the ScanService class:


public class ScanService extends Service {

private LocationListener _locListener;
private LocationManager _locManager;

private static final String TAG = "locationtrigger";

@nullable
@override
public IBinder onBind(Intent intent) {
Log.v(TAG, "onBind");
return null;
}

@override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "ScanServiceonStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

@SuppressLint("MissingPermission")
@override
public void onCreate() {
Log.v(TAG, "ScanServiceonCreate - start");
List<UserLocationManager> locationsManagers = GetLocationsManagers();
_locListener = new MyLocationListener(locationsManagers);
_locManager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
_locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, _locListener);
Log.v(TAG, "ScanServiceonCreate - end");
}


@override
public void onDestroy() {
_locManager.removeUpdates(_locListener);
}

public List<UserLocationManager> GetLocationsManagers(){
// returning some managers
}

public class ScanServiceBinder extends Binder {
public ScanService getService() {
Log.v(TAG, "ScanServiceBinder.getService");
return ScanService.this;
}
}

class MyLocationListener implements LocationListener {

private List<UserLocationManager> _locationManagers;

public MyLocationListener(List<UserLocationManager> locationManagers)
{
_locationManagers = locationManagers;
}

@override
public void onLocationChanged(Location location)
{
if (location != null)
{
// call managers
}
}
}
}
 
You can also see if your application has permissions to run in forground etc, including java code etc,

In my opinion this code

"<uses-permission
android:name="android.permission.FORGROUND_SERVICE"
android:required="true"
>
</uses-permission>"

You can view more info on this here: https://developer.android.com/guide/topics/permissions/overview
 
Last edited by a moderator:
  • Like
Reactions: impeham
Upvote 0
  • Like
Reactions: Deleted User
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones