3

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

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Asif Mehmood
  • 141
  • 1
  • 14

3 Answers3

2

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.

  • 1
    _if service is running on background and uninstall app which service function is called_ – Asif Mehmood Jan 29 '20 at 06:17
  • if service is running on background and uninstall app can ondestroye of service is call or not – Asif Mehmood Jan 29 '20 at 06:19
  • @asif :- there are no methods which will be called, when the user uninstalls the app – Utkarsh Srivastava Jan 29 '20 at 10:11
  • @asif :- but you can register a Broadcast Receiver, which will listen for you and trigger a listener when the user clicks on the uninstall button. please refer this link for more details of registering the Broadcast Receiver https://stackoverflow.com/questions/18692571/how-can-an-app-detect-that-its-going-to-be-uninstalled – Utkarsh Srivastava Jan 29 '20 at 10:14
  • 1
    you have two methods in the service onDestroy() and onTaskRemoved() but both the methods are uncertain means it is not fixed that every time when service is being killed then these function will be called ???It's not mandatory... I have struggled a lot and with my experience i have seen that it's not mandatory that in all the cases onDestroy() will be called – Utkarsh Srivastava Jan 29 '20 at 10:18
  • [link](https://stackoverflow.com/questions/6209730/is-it-possible-to-detect-android-app-uninstall) ** try this it is helpfull ** But wait there is a way if you are using FireBase server CCM Server . the device Id will get invalidated and hence you get to know that the user/device has uninstalled the application and you can reach out to them separately through m – Asif Mehmood Jan 31 '20 at 10:47
2

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.

Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
0

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” />
Fahad Alotaibi
  • 416
  • 3
  • 9