Android Service uses

Service as one of the four major components of Android, and it is widely used. Like activity, service also has a series of lifecycle callback functions that you can implement to monitor changes in service state and perform appropriate work when appropriate.

Services are generally divided into two types:

1: Local service, Local Service is used inside the application. In the Service can call Context.startService () to start, call Context.stopService () to end. You can call Service.stopSelf() or Service.stopSelfResult() internally to stop it yourself. No matter how many times startService() is called, just call stopService() once to stop.

2: Remote Service, Remote Service is used between applications within the Android system. You can define an interface and expose the interface for other applications to operate. The client establishes a connection to the service object and invokes the service through that connection. Call the Context.bindService() method to establish a connection and start it to call Context.unbindService() to close the connection. Multiple clients can be bound to the same service. If the service has not been loaded yet, bindService() will load it first. It can be reused by other applications, such as defining a weather forecast service, and providing it with other application calls.

Android Service life cycle

Speaking of the service life cycle, let's build a class to inherit the service and see

这里写图片描述

周期图:

这里写图片描述

public class DemoService extends Service {


    @Override
    Public void onCreate() {
        super.onCreate();
        // The service is being created
        Create service
    }

    @Override
    Public int onStartCommand(Intent intent, int flags, int startId) {
        Return super.onStartCommand(intent, flags, startId);
        // The service is starting, due to a call to startService()
        Start service
    }

    @Nullable
    @Override
    Public IBinder onBind(Intent intent) {
        Return null;
        Binding service
        // A client is binding to the service with bindService()
    }

    @Override
    Public boolean onUnbind(Intent intent) {
        Return super.onUnbind(intent);
        // All clients have unbound with unbindService()
        Untied service
    }

    @Override
    Public void onRebind(Intent intent) {
        super.onRebind(intent);
        // A client is binding to the service with bindService(),
        // after onUnbind() has already been called
    }

    @Override
    Public void onDestroy() {
        super.onDestroy();
        Destruction service
        // The service is no longer used and is being destroyed
    }
 }

How do I start the service, what are the external methods?

startService(), start the service stopService(), stop the service bindService(), bind the service unbindService(), unbind service

Lifecycle method brief introduction

startService()

role: start Service service After calling startService() manually, the internal methods are automatically called: onCreate(), onStartCommand() If a service is started multiple times by startService, onCreate() will only be called once. onStartCommand() call times=startService() times

stopService()

role: close the service service After calling stopService() manually, the internal method is called automatically: onDestory() If a service is started and bound, if it is not bound, stopService() cannot stop the service.

bindService()

作用: Binding Service Service After calling bindService() manually, the internal method is automatically called: onCreate(), onBind()

unbindService()

: Unbind Service Service After manually calling unbindService(), the internal methods are automatically called: onCreate(), onBind(), onDestory()

Note

startService() and stopService() can only open and close the Service, and cannot operate the Service; bindService() and unbindService() can operate the Service opened by Service

startService, and the service still exists after the caller exits; The Service opened by BindService. After the caller exits, the Service is destroyed by the caller.

is a brief description of the service, and more will be explained next.