Asif Mehmood
Asif Mehmood

Reputation: 141

Background Service for update location in background

it kills background service, to solve your issue you should use foreground service.

my background sticky service is kill on oreo and heigher devices any solution for getting location on background service when Activity is on backound

Upvotes: 3

Views: 2272

Answers (3)

Arpit Ratan
Arpit Ratan

Reputation: 3026

It's because of Android Oreo behaviour changes for background execution. Suggested alternatives are

1) Foreground service.

Use this if you are ok displaying notification to the user when you are trying to retrieve the location using Location API. This would be reliable and is suggested in the documentation.

Sample app from the documentation : LocationUpdatesForegroundService project on GitHub

An example of an app that allows the app to continue a user-initiated action without requesting all-the-time access to background location.

References
https://developer.android.com/training/location/receive-location-updates

2) Work Manager

This approach would be less reliable as you will not be able to control when exactly this would be called but can be used if you don't want to show notification to the user at all.

Upvotes: 2

Utkarsh Srivastava
Utkarsh Srivastava

Reputation: 174

You would not be able to run background services long running in Oreo as there are behavior changes, now Oreo to optimize system memory, battery etc, it kills background service, to solve your issue you should use foreground service.

Have a look at Background execution limits https://developer.android.com/about/versions/oreo/android-8.0-changes

A suggestion from me, if you can use FCM then go for it, because apps like WeChat, Facebook uses it, to deliver notifications and they don't face any problem.

Alternate solution which I have opted without FCM due to client requirement to run service that updates location in background. Here are steps below:-

  • You need to start your background service with showing notification in Oreo and above.
  • Them after that you need to keep in mind that after some time phone enters into Doze mode so you have to tackle that also
  • In Addition, Battery optimization must be disabled for the application too.
  • In Some custom ROM you need to manage the Auto-start permission to restart your service if the service is killed by the android.
  • And the most important part is, if the service is killed by the android system then send a Broadcast Message to the Broadcast receiver to restart your service one again

Hope you will do some more R&D work. I have shared my experience and the process by which i have done the same to run the service in the background.

Upvotes: 2

Fahad Alotaibi
Fahad Alotaibi

Reputation: 423

you must show notification for ForegroundService

    public class ForegroundService extends Service {
    public static final String CHANNEL_ID = "ForegroundServiceChannel";
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @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;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

and add permission

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

Upvotes: 0

Related Questions