Call Service through aidl

Source: Internet
Author: User

Some examples of services found on the Internet are relatively simple. They start the service through startservice ("action") and then stop the service through stopservice ("service. You can only start and stop a service without using the service function. The following example describes how to enable aidl to control music playback.
1. A file suffixed with aidl in the project package:
Imusiccontrolservice. aidl

Package com. Dream. androidstud2.service; -------- the package name must be the same as the package name of the current project!

Interface imusiccontrolservice
Void playmusic (); --------> play music
Void stopmusic (); -------> stop playing music

After you click Save, an imusiccontrolservice. Java file is created in the GEN/directory of the preceding package name.
2. Create a layout file in the Res/layout directory:
Startserviceactivity. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = ""
Androidrientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Id = "@ + ID/TV _main"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: textsize = "18px"/>

Android: text = "playing music"
Android: Id = "@ + ID/btn_play"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>

Android: text = "Stop playing"
Android: Id = "@ + ID/btn_stop"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>

3. Create a service class and instantiate the playmusic () and stopmusic () interfaces in the imusiccontrolservice class.
Private Final imusiccontrolservice. Stub binder = new imusiccontrolservice. Stub ()

@ Override
Public void playmusic () throws RemoteException {
// Todo auto-generated method stub

Player = mediaplayer. Create (controlmusicservice. This, R. Raw. shanghaitan );
Player. Start ();

@ Override
Public void stopmusic () throws RemoteException {
// Todo auto-generated method stub

If (player. isplaying ())
Player. Stop ();

In the onbind () method of this class, return the binder of the above instance, that is, return binder;
4. Create a startserviceactivity class to inherit the activity class. In this class, use serviceconnection to connect to the background service.
Private Final serviceconnection = new serviceconnection ()

// This method is called when you connect to the service for the first time.
@ Override
Public void onserviceconnected (componentname name, ibinder Service ){
// Todo auto-generated method stub

Imusiccontrolservice = imusiccontrolservice. stub. asinterface (service );


// This method is called when the service is disconnected.
@ Override
Public void onservicedisconnected (componentname name ){
// Todo auto-generated method stub
System. Out. println ("service unconntection ");
Imusiccontrolservice = NULL;



Bind the service in the oncreate method:
Intent intent = new intent ();
Intent. setclass (startserviceactivity. This, controlmusicservice. Class );
Bindservice (intent, serviceconnection, context. bind_auto_create );
When you click the playmusic button, run the following code:
Imusiccontrolservice. playmusic ();
When you click the stopmusic button, run the following code:
Imusiccontrolservice. stopmusic ();
Unbindservice (serviceconnection );

Now we can control the service through aidl in the activity.


Note: bindservice (intent, serviceconnetciton, INT) compares paid resources and time. Therefore, this code is generally executed in the oncreate () method. In this way, myicontrolmusic = NULL will not appear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.