-1

I am migrating my Android App to target Android Oreo. I am running a service which should run indefinitely to perform a particular task.

The running of the service is to perform a particular task with the users' consent of course.

Till now my app has been targeting Android Marshmallow and the service is working fine and is running also fine.

This is my service

public class MyService extends Service {

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    private void DoSomething() {
     //Some Code keep watching something.       
    }


    @Override
    public int onStartCommand (Intent intent, int flags, int startId) {

        DoSomething();

        return  Service.START_STICKY;
    }


}

And this is how I am Starting the service from an Activity

startService(new Intent(getBaseContext(), MyService.class));

Now my problem is that when I target my app to Android Oreo then the service is not running and hence the function which I am trying to achieve is not working.

But whereas If my App target Android Marshmallow everything is working fine with the exact same code.

Can anyone help me to run a Serice indefinitely in Android App targeting Android Oreo would be appreciated?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rahulrr2602
  • 701
  • 1
  • 13
  • 34
  • 1
    [This](https://stackoverflow.com/questions/48996560/how-to-run-a-background-service-in-oreo-for-longer-period) maybe of some interest to you. – AnEnigmaticBug Dec 22 '18 at 11:34

2 Answers2

1

In Android Oreo+, background services have been severely restricted. To run your code, you've two options: use a Foreground service or use something like JobScheduler/WorkManager APIs.

Neither of them will work indefinitely though and WorkManager is only meant for deferrable tasks. This is something that we can't do much about.

AnEnigmaticBug
  • 939
  • 9
  • 17
  • Thanks For the Answer. But say Suppose I want to use a FileObserver in the background then how should I do it? Any Idea? Please help. – Rahulrr2602 Dec 22 '18 at 11:24
  • 1
    Why do you want to use the `FileObserver`? – AnEnigmaticBug Dec 22 '18 at 11:25
  • To Keep a watch on a directory for any new file being added. Sorry cannot disclose the app name or else it would have given you a better idea. – Rahulrr2602 Dec 22 '18 at 11:27
  • 1
    No problem. Your best bet is using a foreground service. They'll result in a notification in the status bar while they're running though(unlike background services pre oreo). Look at the "Background Execution Limits" section of [this](https://developer.android.com/about/versions/oreo/android-8.0-changes) page. – AnEnigmaticBug Dec 22 '18 at 11:30
  • Thanks. Can You please provide an example of foreground service? – Rahulrr2602 Dec 22 '18 at 12:39
  • Also, Can you please tell will foreground service run for indefinately even if the user is not interacting with the app? And what is the major difference between a normal service and an background service? – Rahulrr2602 Dec 22 '18 at 12:45
  • 1
    No component in Android can be guaranteed to run indefinitely. But Android tries hard not to kill a foreground service. Refer the link in my answer. You'll find most of the required information there. If you need further help, feel free to ask. – AnEnigmaticBug Dec 22 '18 at 13:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185651/discussion-between-rahulrr2602-and-nightmare). – Rahulrr2602 Dec 22 '18 at 13:26
0

For android Latest versions we have some limitations for runnig background services Android Oreo Limitations

satyan_android
  • 364
  • 4
  • 15