Detailed explanation of data interaction between Android activity and service

① From a design perspective:

The design of Android activity is very similar to that of web pages. It can be seen from page Jump, connection, page positioning, URL and independent encapsulation of each page. It is mainly responsible for interacting with users.

Service runs in the background and silently provides users with functions for scheduling and overall planning. If the aboveground part of a tree is an activity, its huge root is service. Android's service component does not run in an independent process or thread. Like other components, it also runs in the main thread of the application. If the service component performs more time-consuming operations, the main thread will be blocked or suspended, so it cannot respond to the user's operations.

Therefore, time-consuming operations should not be placed in the UI thread, because UI 5S and broadcast 10s are blocked, which will cause ANR.

② From the perspective of use:

Service can not only establish a two-way connection to the activity and provide data and function support for the activity, but also accept intent requests in one direction for data analysis, processing and function scheduling.

③ In terms of roles:

The function of activity is relatively simple. It mainly displays some functions of the application to help users interact with the application, like a person's face. Service may act as a function scheduler or a function provider. It collects information from triggers for analysis and processing, and then updates the interface, modifies data or performs other operations. Considering the selection of input method, service acts as a function provider. The view component is the part that users can actually see in Android, such as buttons, input boxes, etc. it inherits from this class. The view is meaningful only when it is loaded into a container such as activity. On the contrary, the activity can successfully complete the task of interacting with users after loading these views, but the service does not need these fancy things, Just wait silently for events to happen or wait for orders.

There are two methods for Android to start service, one is startservice and the other is bindservice. The life cycle is as follows:

When executing startservice, if the caller does not have stopservice, the service will always run in the background. If startservice is called multiple times, the service can only be created once, that is, the oncreate method of the service can only be called once. However, the onstartcommand method is called every time startservice is called.

When the bindservice is executed, the caller calls the unbindservice method or the caller context does not exist (for example, the activity is finished). When the bindservice is executed for the first time, oncreate and onbind methods will be called, but when the bindservice is executed multiple times, oncreate and onbind methods will not be called multiple times, that is, services and binding services will not be created multiple times. Multiple components can be bound to the service at the same time, but the service will be destroyed after all the components are unbound.

When both startservice and bindservice are used, unbindservice and stopservice need to be called at the same time to terminate the service.

There are two methods for activity and service interaction: one is to use broadcast and the other is to use bindservice. This article only introduces the bindservice method.

In the example, msgservice simulates a time-consuming download task, mainactivity binds the service, obtains the download progress by registering the onprogresslistener callback, and updates the progress bar.

In this example, activity and service are in the same process, and Aidl technology needs to be used for calling service across processes.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>