Ohados
Ohados

Reputation: 83

How to run infinite service on Android (Foreground service killed)?

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);
}

Upvotes: 4

Views: 9414

Answers (4)

sachin pathak
sachin pathak

Reputation: 87

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

Upvotes: 0

Abhijit
Abhijit

Reputation: 105

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.

Upvotes: 5

Ivan
Ivan

Reputation: 61

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.

Upvotes: 1

Melih Toksari
Melih Toksari

Reputation: 21

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

Upvotes: 1

Related Questions