0

I have a messaging app based on webscokets that relies on a background service to send and receive messages. Once the service is started, I need it to run indefinitely in the background, even when the app closes as well as the phone goes into sleep mode.

In the app, the service is started just as a user logs in, and in the onCreate method as startService(new Intent(LoggingIn.this, MessagingService.class));

How can I setup my service to run permanently in the background?

private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service. Because we have bound to a explicit
            // service that we know is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            imService = ((MessagingService.IMBinder) service).getService();

            if (imService.isUserAuthenticated() == true) {
                // Intent i = new Intent(LoggingIn.this, ListOfFriends.class);
                Intent i = new Intent(LoggingIn.this, MainActivity.class);
                startActivity(i);
                LoggingIn.this.finish();
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            // Because it is running in our same process, we should never
            // see this happen.
            imService = null;
            Toast.makeText(LoggingIn.this, R.string.local_service_stopped,
                    Toast.LENGTH_SHORT).show();
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*
         * Start and bind the imService
         */
        startService(new Intent(LoggingIn.this, MessagingService.class));

...etc
Sauron
  • 6,399
  • 14
  • 71
  • 136

2 Answers2

0

Use startForeground() in your onStartCommand() method in your service.

tachyonflux
  • 20,103
  • 7
  • 48
  • 67
0

you need to use START_STICKY in the onStartCommand: http://developer.android.com/reference/android/app/Service.html#START_STICKY

Then you can read this thread to start your service during boot: Android -Starting Service at Boot Time

You need to define a name for the service in the manifest to make it run in a different thread: android:process=":my_process" so it will continue to run inbackground if you close the app

Community
  • 1
  • 1
arthur_gg
  • 279
  • 1
  • 8
  • From docs: `This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback`, I need the service permanently up, not starting and stopping, how can I refactor the code to have the service up all the time? – Sauron Dec 09 '14 at 04:15
  • ok I got your issue, you need to specify to use your service in a separate thread, you can do it in the manifest by specifying a name android:process=":my_process" in the service decalration. then your service will run even if you close the app. This plus the STICKY mode and it should be good – arthur_gg Dec 09 '14 at 04:48
  • But if android shuts down the process, is there a way to notify and then bring it back up? – Sauron Dec 09 '14 at 04:51
  • The service will be restarted automatically by the system. Try to kill some services on your phone like Google Play, It will be restarted by the system – arthur_gg Dec 09 '14 at 07:09
  • START_STICKY If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), so you put your websocket connection in this method and retrieve the last messages – arthur_gg Dec 09 '14 at 07:11