Android: Service stops when Activity is closed

14,255

Solution 1

You need to use startService so that the service is not stopped when the activity that binds to it is destroyed.

startService will make sure that the service is running even if your activity is destroyed (unless it is stopped because of memory constraints). When you call startService, the onStart method is called on the service. It doesn't restart (terminate and start again) the service. So it depends on how you implement the onStart method on the Service. The Service's onCreate, however, is only called once, so you may want to initialize things there.

Solution 2

I found a solution!

The problem is when you close your activity and your service is not configured as a foreground service, android system will recognize it as unused and close it.

here's an example where I added a notification :

void setUpAsForeground(String text) {
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            new Intent(getApplicationContext(), MainActivity.class),
            PendingIntent.FLAG_UPDATE_CURRENT);
    mNotification = new Notification();
    mNotification.tickerText = text;
    mNotification.icon = R.drawable.muplayer;
    mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
    mNotification.setLatestEventInfo(getApplicationContext(), "MusicPlayer",
            text, pi);
    startForeground(NOTIFICATION_ID, mNotification);

}

(A foreground service is a service that's doing something the user is actively aware of (such as playing music), and must appear to the user as a notification. That's why we create the notification here)

Share:
14,255
jclova
Author by

jclova

Senior Android developer at DEEP Inc. and CEO at EllevSoft. DEEP Inc. http://deep-inc.com/ EllevSoft https://play.google.com/store/apps/developer?id=EllevSoft

Updated on June 18, 2022

Comments

  • jclova
    jclova almost 2 years

    If I understand it correctly, bindService() with BIND_AUTO_CREATE will start a service and will not die until all bindings are all unbinded.

    But if I bindService(BIND_AUTO_CREATE) at onCreate() and hit back button to close the activity, the service calls onDestroy() and dies also.

    I don't call unbind() at anytime. So is that mean when the Activity got destroyed, the binding got destroyed also and the service gets destroyed also?

    What if I want the service to be always running, at the same time when the activity starts I want to bind it so that I can access the service?

    If I call StartService() and then bindService() at onCreate(), it will restart the service at every launch of Activity. (Which I don't want).

    So I could I start service once and then bind next time I launch the activity?

  • Shafiul
    Shafiul almost 12 years
    How can I communicate with the service from my activity? Say, I want to send notifications from the service to the activity when some work is done. The service performs some work periodically.
  • songhir
    songhir about 10 years
    I call bindService() with BIND_AUTO_CREATE : Service stops when Activity is closed; I call startService() , it ok. thx.
  • Richa
    Richa almost 8 years
    can u please elaborate bit on this ? i m facing issue with service
  • Sharp Edge
    Sharp Edge over 7 years
    It would have been better if you shared the entire example, like what is startForeground() method ?
  • Khalil Kitar
    Khalil Kitar over 7 years
    A Foreground Service in android is a background service which keeps running even after the parent application is closed. it takes (int id, Notification notification) as parameters.
  • Sharp Edge
    Sharp Edge over 7 years
    Can you share its code ? Update it in your answer if you can.
  • gumuruh
    gumuruh almost 4 years
    are you suggesting intentService & broadcastReceiver?