9

My question is how to create an always running background service. I created an IntentService which runs in background as long as the app is running. However, when app is killed, service is gone as well. What i want to create is to create a background service that always runs and posts notifications. (Similar to whatsapp, facebook or any other similar applications.)

When the notification is clicked, it should start the application as well.

How can i do it?

user3686811
  • 137
  • 1
  • 2
  • 9
  • Have you looked into alarm receivers and pending intents? They can keep a service running including when your phone is asleep. – Chucky Jun 06 '14 at 09:08
  • If you find a way to keep your service running, other than making it a foreground service, that's a major Android bug and will probably be fixed. (edit: or not. apparently I don't understand how START_STICKY works) – user253751 Jun 06 '14 at 10:37
  • for new APIs you can use this : https://stackoverflow.com/a/49878237/2201814 – MHSaffari Apr 17 '18 at 12:50

3 Answers3

13

the best to do this is create custom service that extends your service not intent Service. and start a separate thread in your service. start service in the launching of app One more important this dont bind it to any object cause service will be killed once your object destroyed. then onStartCommand() method return START_STICKY I will give you the sample implementation.

public class CustomService extends Service {

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    return START_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    // TODO Auto-generated method stub
     Intent restartService = new Intent(getApplicationContext(),
                this.getClass());
        restartService.setPackage(getPackageName());
        PendingIntent restartServicePI = PendingIntent.getService(
                getApplicationContext(), 1, restartService,
                PendingIntent.FLAG_ONE_SHOT);

        //Restart the service once it has been killed android


        AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +100, restartServicePI);

}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    //start a separate thread and start listening to your network object
}

}

onTaskRemoved restarts your service after 100milli seconds once it is killed. START_STICKY doesnt work in kitkat. You have to implement onTAskRemoved. And one more thing if the user goes to app settings and stops your service then there is no way you can restart it

Preethi Rao
  • 5,117
  • 1
  • 16
  • 29
4

You must start service using start command. And you must rewrite onStartCommmand function in service:

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

START_STIKY means that service will be restarted automamtically (http://developer.android.com/reference/android/app/Service.html#START_STICKY)

If you use bindService function instead of startCommand, your service will stoped then all binded activitiess goes down.

More than that if you want to autostart your service after booting device you must implement BroadcastReceiver for receiving ACTION_BOOT_COMPLETED intent and handle this event for start service.

busylee
  • 2,540
  • 1
  • 16
  • 35
0

The following will do as long as your activity is around.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

Unfortunately when your app is killed and the device runs out of resources it will kill it.
But if you want to keep your service running for days, make sure you are running your service in the foreground.

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103