4

I created a sticky Foreground service which restarts every-time it gets killed, it seems to work fine but I noticed that in some cases ( when the OS kills it) it just wont restart. How can I make it restart EVERY time it gets killed? I want this service to run always.

I want it to run like those

Like those services.

This is my Foreground service onStartCommand:

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

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0, notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("InsiteMobile Service")
            .setContentText("Run the app by clicking here")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pendingIntent)
            .build();
    Log.i("InsiteMobileLog", "Service Started");
    startForeground(1, notification);

    //do heavy work on a background thread
    //stopSelf();

    return START_STICKY;
}

And this is how i call the service in MainActivity onCreate():

public void startService() {

    Intent serviceIntent = new Intent(this, AppService.class);
    serviceIntent.putExtra("inputExtra", "InsiteMobileService");

    ContextCompat.startForegroundService(this, serviceIntent);
}
Ohados
  • 83
  • 3
  • 13
  • 1
    fortunately you cannot do that - i really would not like to have a malicious service that is "immortal" – pskink Sep 26 '18 at 08:56
  • Why i cant and WhatsApp, Facebook and all of those can? Btw its a private app for peraonal use, and this is what i need. – Ohados Sep 26 '18 at 09:02
  • it does not matter if its for personal use or not: those kind of services are prohibited - if you need "long lived" services start them in the foreground (`Service#startForeground()` method) – pskink Sep 26 '18 at 09:12
  • I wrote that i do use startForeground(). But in some cases this service gets killed by the OS and wont get restarted in this case. – Ohados Sep 26 '18 at 09:20
  • can you show your foreground code? Foreground services are not candidates for the OS to kill off when it is low on resources. – Tim Sep 26 '18 at 09:25
  • also please never compare yourself to the big company apps, they have deals with google and their apps are whitelisted – Tim Sep 26 '18 at 09:27
  • so post your code: otherwise its going to be an academic discussion... – pskink Sep 26 '18 at 10:12
  • i guess that what @Melih Toksari wrote is the deal, in Oreo foreground services do get killed.. my code is pretty general.. i start the service in my MainActivity like this : ` public void startService() { Intent serviceIntent = new Intent(this, AppService.class); serviceIntent.putExtra("inputExtra", "InsiteMobileService"); ContextCompat.startForegroundService(this, serviceIntent); }` and i use startForeground() in my onStartCommand() and return START_STICKY. – Ohados Sep 26 '18 at 10:15
  • go to [Background Execution Limits](https://developer.android.com/about/versions/oreo/background) and ^F `After the system has created the service, the app has five seconds to call the service's ...` - are you sure that your service works like that? – pskink Sep 26 '18 at 10:22
  • I guess so, because the service being called in the Oncreate of MainActivity, and i always get a notification about the service which means that its running properly. The only thing is when the OS decides to kill the service, then, it wont comeback. – Ohados Sep 26 '18 at 10:43

4 Answers4

5

Currently I am working on same project where I need services to be running in background. There are several things that we need to care about
1. In onDestroy() method of service, start itself again using startService Intent
2.For OS with and below kitkat use onTaskRemoved(intent); in onStartCommand() method
3. Return START_STICKY in onStartCommand()

And after this, you need to understand one more thing, phone manager of the device tends to kill the background processes based on battery optimization policies. Check for the phone manager settings of your testing device and then try again.

Also, if you are going use notification to keep the service alive, you need to recheck the permission for sending notification through your application.

Abhijit
  • 95
  • 5
  • 2
    Can you please explain number 2 again? i couldn't understand. And can you please be more specific about phone manager thing? – Ohados Sep 26 '18 at 10:08
  • START_STICKY doesn't work for Android kitkat and below, hence we have to use onTaskRemove() to make services run in background for as many devices as possible. – Abhijit Sep 26 '18 at 12:45
  • And, almost all the devices has phone manager which makes sure that phone performs smoothly. For that it kills background services as well as apps which are idle in the system for a while. So while testing your app, just look for phone manager settings/ battery optimization settings to tackle them through coding. – Abhijit Sep 26 '18 at 12:48
1

Android Oreo has some limitations about services. You can look this thread https://stackoverflow.com/a/47611385/4082934. Also Evernote team has good library about background services compatible with Android Oreo (API Level 26). JobScheduler library provides periodic services with JobInfo.Builder.setPeriodic(long intervalMillis, long flexMillis) method. Minimum working time of this service is 15 minutes. Some foreground service tutorials:

  1. https://blog.klinkerapps.com/android-o-background-services/
  2. https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo-ab384762a66c
1

You can't create service that working always on latest OS versions. But you can use ForegroundService with notification, that can't be killed. The principle is that the user should always see that your application is running and consuming the OS resources.

Ivan
  • 61
  • 5
  • 2
    I wrote that i do use startForeground(). But in some cases this service gets killed by the OS and wont get restarted in this case. – Ohados Sep 26 '18 at 09:20
0

I am also doing a project which needs a service who is immortals, so you should use Job Scheduler for this purpose.