Android Aidl and remote service call example code

Android: Aidl and remote service call

The content of this lecture is difficult to understand. Maybe you can't understand it after reading a lot of materials, but it lacks simplicity. So let's simply take an example of the progress bar in a music player to illustrate the value and usage of Aidl and remote service. Just run away from this example and experience it. The following example is me

Part of the project instance being prepared.

First, explain the problems we face. If you don't understand the following description, please refer to the previous course:

First, we know that if you need to play music in Android, the most important way is to use the built-in mediaplayer object. If we control the mediaplayer object in activity to play, once you open another program such as browser, the singing will stop immediately. Of course, this is not the result we need. What we need is to be able to listen to music in the background while doing other things, so we need to put the operation on the mediaplayer object in the background service.

Second, we have transferred the mediaplayer operation to the service. According to our previous practice, we send an intent object to the service object in the activity, and send information such as play and pause to the service in the intent, so that the service can know what to do. All this looks beautiful, but now there is a new problem. That is, I want to display a progress bar in the activity. This progress bar should move forward synchronously with the song progress in mediaplayer in the service. Moreover, if I click a position in the progress bar and want the song to jump to a new time point to continue playing, how can I achieve this?

Third, we need to operate the mediaplayer object in the service in the activity as if the object is our own. We can use the Android interface definition language (Aidl) technology:

1. Encapsulate the mediaplayer operation in the service into an interface (. Aidl file) 2. Create a subclass in the service to implement the stub object of the interface 3. Return the stub object in the onbind () method. 4. Connect the service by binding the service in the activity, but do not use intent to pass information. Instead, in the onserviceconnected method of serviceconnection, the client that obtains the stub object in the service uses the proxy. We can operate the mediaplayer object in the service by operating the agent in the activity. In this way, we can operate the objects in the service like local objects, and the requirements such as progress bar will be solved naturally.

The following examples are not specially prepared for this lecture, so some irrelevant codes are not annotated. Please forgive me (the complete explanation of this example will be put in the project training and is being prepared):

1. Create a new project app_ Elfplayer, starting activity is a startup screen: coveractivity

2、AndroidManifest. The contents of XML are as follows:

We notice that there are two activities, one service, and permission statements for reading and writing external storage. 3. Coveractivity The Java code is as follows: This is a full screen startup screen, and it will jump to playeractivity in 2 seconds

4、PlayerActivity. The Java code is as follows:

5. Iserviceplayer Aidl, which is placed in the same package as the java file. The contents are as follows:

Once you've written this iserviceplayer Aidl file, ADT will automatically help you generate iserviceplayer in the gen directory Java file.

6、MusicService. The contents of Java are as follows:

7. Implementation effect diagram:

Finally, Aidl provides a very simple way to expose objects or methods in one process to another program, just as another program has these functions.

The above is the introduction and simple application of Android Aidl and remote service. We will continue to supplement relevant knowledge in the future. Thank you for your support!

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