Several ways of communication between service and activity in Android

In Android, activity is mainly responsible for the display of foreground pages, and service is mainly responsible for tasks that need to run for a long time. Therefore, in our actual development, we often encounter the communication between activity and service. We generally start the background service in activity and start it through intent. In intent, we can transfer data to service, When we want to update the UI thread after the service performs some operations, what should we do? Next, I will introduce two ways to realize the communication between service and activity

1. By binder object

When the activity calls bindservice (intent service, serviceconnection Conn, int flags), we can get an object instance of a service, and then we can access the methods in the service. Let's understand it through an example, a small example of simulated download, and let's understand the way of communication through binder

First, we create a new communication project, and then create a new service class

The above code is relatively simple and the comments are relatively detailed. It is the most basic service application. I believe you can understand it. We call the startdownload () method to simulate the download task, and then update the progress every second. However, this is in progress in the background and we can't see it. Therefore, sometimes we need it to display the download progress in the foreground, So we'll use activity next

Through the above code, we bind a service to the activity, which requires a serviceconnection object, which is an interface. We use an anonymous internal class here

In the onserviceconnected (componentname, ibinder service) callback method, a binder object in msgservice is returned. We can get an msgservice object through the getservice () method, and then call some methods in msgservice. The code of activity is as follows

In fact, I still have some questions about the above code, that is, the method of monitoring progress changes. I update the UI directly in threads, doesn't it mean that I can't update UI operations in other threads? Maybe ProgressBar is special, and I haven't studied its source code. Friends who know can tell me, thank you!

The above code completes the operation of updating the UI in the service, but do you find it? We have to actively call getprogress () every time to obtain the progress value, and then call getprogress () every other second. Do you feel very passive? Is there a way to actively notify the activity when the progress in the service changes? The answer is yes. We can use the callback interface to realize the active notification of the service. If we don't understand the callback method, we can see

https://www.oudahe.com/p/26536/

Create a new callback interface

There are some small changes in the code of msgservice. In order to make it easier for you to understand, I'll post all the codes

The code in the activity is as follows

Is it more convenient to use the callback interface? When the progress changes, the service actively notifies the activity, and the activity can update the UI operation

2. In the form of broadcast

When our progress changes, we send a broadcast, register the broadcast receiver in the activity, and update the progressbar after receiving the broadcast. The code is as follows

Summary:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>