Search code examples
androidsocketsservicebackground-process

Android Service is recreated after destroying activity


First of all I'm creating app that must be working in the background (even if user killed application). I have bluetooth server (raspberry pi) and my app should act like a client to that server. When connection is established I need to keep it alive and disconnect only if user is too far prom server or server sends specific disconnection command. So I simply found BluetoothChatExample and put all the code from activity to service (because I need to communicate even if user kills application and my service should be independent from my main activity).

Here's some code snippets from my service

public class BluetoothCommunicationService extends Service {
    public BluetoothCommunicationService() {

    }

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

   //....
   //....
   //....
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       if(intent.getAction.equals("stop") {
           stopSelf();
       }
       return START_STICKY;
    }

private class AcceptThread extends Thread {
            //....
}

private class ConnectThread extends Thread {
    //....
}

private class ConnectedThread extends Thread {
    //....
}

@Override
    public void onDestroy() {
        super.onDestroy();

}

} So as you can see I started my service as START_STICKY but here's the problem when I'm killing the application from the "recently used apps" list I'm also killing my service (even if onDestroy() is not called!!!) and then again onCreate is called and service is starting itself from scratch. But bluetooth communication is based on sockets so they are also recreated and my connection is lost. I also tried START_NOT_STICKY however then my service is destroyed as soon when I close my app and it's never recreated. Is it possible to keep service alive even (and not recreate it) after user closed my app?


Solution

  • First of all I'm creating app that must be working in the background (even if user killed application).

    Not unless you are a system app you are not. Android's app model is very clear that apps cannot force themselves to run continuously, for good reason. What if a large number of apps on your phone decided to do that? Mobile devices are not desktop computers. They have batteries and can't achieve reasonable battery life with processes continuously spinning in the background. To get reasonable battery life, Android depends on being able to put the CPU to sleep when nothing is going on.

    Moreover, if a user swipes away your activity, the app will be put in an inactive state. That means it cannot receive broadcasts (that includes alarm manager wakeups), and can only be re-started by the user starting your activity from the launcher, another app starting one of your activities, or another app starting a service within the app. This is also true for newly installed apps: they can't run until you explicitly use them.

    For non-system apps, the best you can do is use startForeground() + START_STICKY. This is the strongest indicator to Android that your service is important and should not be killed.

    System apps can declare themselves persistent in their manifest, and their process will never be killed. Unless you are building the Android dist and flashing it yourself though this isn't going to be an option.