Search code examples
androidkotlingpslocationmaps

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

@SuppressLint("MissingPermission")
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()
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,10f,locationListener)

    return START_STICKY
}



override fun onLocationChanged(location: Location) {
   println(location.toString())
}

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

}


Solution

  • 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.

    ForegroundService.java

    @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            String input = intent.getStringExtra("inputExtra");
            createNotificationChannel();
            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")
                    .setContentText(input)
                    .setSmallIcon(R.drawable.ic_stat_name)
                    .setContentIntent(pendingIntent)
                    .build();
            startForeground(1, notification);
            //do heavy work on a background thread
            //stopSelf();
            return START_NOT_STICKY;
        }
    

    AndroidManifest.xml

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

    MainActivity.java

    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 - https://developer.android.com/about/versions/oreo/android-8.0-changes
    https://developer.android.com/guide/components/services