Android development —— 12, Service

 Service classification:
Services can be essentially divided into two types:

Started: When an application component (eg: Activity) starts the service by calling the startService() method, the service is in the started state. Once started, the service can run indefinitely in the background, just in time when the component that started it has been destroyed.

Bound: When an application component is bound to a service by calling the bindService() method, the service is in the bound state. A bound service provides a client-server interface to allow components to interact with the service, send requests, get results, and even do so across processes using interprocess IPC.

 Important methods of Service

onStartCommand()
unbind()
onCreate()
onDestroy ()

Started Service creation

Create a startup service by inheriting Service and IntentService.

Bound Service creation

Binding to a service is a Service class implementation class that allows other applications to bind and interact with it. In order to provide binding, the developer must implement the onBind() callback method.

When implementing binding to a service, the most important thing is to define the interface returned by the onBind() callback method. There are 3 ways.

a. Inherit the Binder class
b. Use Messenger
c. Using AIDL

Service life cycle

StartedService : When other components call the StartService method, the service is created. Then, the service runs indefinitely. It must call the stopself() method or other components call the stopService() method to stop the service. When the service stops, the system will destroy.
Bound Service : The service is created when the bindService() method is called by other components. The client then communicates with the service through the IBinder interface. The client closes the connection through the unbindService() method. Multiple clients can bind to the same service, and they are all unbound, the system destroys the service

Intent application

There are two ways to access the network via HTTP :

(1) Implemented using the HttpURLConnection in the java.net package
(2) Implemented through the HttpClient provided by Android .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325695731&siteId=291194637