OnLocationChanged() Doesn't work in background

I’m using the following code to get user’s current location. When app is on foreground it’s working perfectly however when i try to change location at emulator in background it doesn’t give me the location. What can i do?

class BackgroundLocation:Service(),LocationListener {

override fun onBind(intent: Intent?) = null

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    super.onStartCommand(intent, flags, startId)
    val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
    val locationListener = BackgroundLocation()

    return START_STICKY

override fun onLocationChanged(location: Location) {

override fun onProviderDisabled(provider: String) {
override fun onProviderEnabled(provider: String) {
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {


here is solutions:

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Solution 1

Currently you are using a background service. It works when is running as and as soon as app goes in background, service stops.
To get continuous updated in background you need to update service to a foreground service.

    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
        startForeground(1, notification);
        //do heavy work on a background thread
        return START_NOT_STICKY;


 <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

public void startService() {
    Intent serviceIntent = new Intent(this, ForegroundService.class);
    serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");
    ContextCompat.startForegroundService(this, serviceIntent);

If you are running the app on android 10 and above you need to request Background location.

  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

You can read all about background location, it limitation and foreground service here –

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply