Android: Services and Life Cycle

A service is a component that runs in the background, regardless of the specific activity. So it is not necessary to have a layout that makes up the screen, and it is suitable for implementing a function which must be operated continuously for a long time.

To run an application as a service, it inherits the Service class and implements it by overriding the lifecycle method.

public class MyService extends Service {
    public void onCreate() {
        Thread t = new Thread() {
            void run() {  }
        };
        t.start();
    }

    public void onDestroy() {

    }
}

Life cycle of service

There is a life cycle, like a service activity. Typically, unbounded services are used to implement local services, and bounded services are used to implement remote services.

This means that Bounded Services can run remotely implemented services from other applications running on the same device.

Unbounded Service

  • onCreate(): Called when the service is created
  • onStart(): Called whenever the service is started by the startService() method
  • onDestroy(): Called when the service is terminated

Bounded Service

  • onCreate(): Called when the service is created
  • onBind(): Called when the service is started by the bindService() method
  • onUnbind(): Called when the service is disconnected
  • onDestroy(): Called when the service is terminated

Share This Post

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다

Latest Post