Android Service (ii) remote services

Source: Internet
Author: User

Typically each application runs within its own process, but sometimes it needs to pass objects between processes, and you can write a service that runs in a different process through the application UI. In the Android platform, a process typically does not have access to memory areas in other processes. So, they need to split the objects into simple forms that the operating system can understand so that they are disguised as objects across the boundary. Writing this kind of camouflage code is pretty tedious, but Android gives us the Aidl tool to do it.

Aidl (Android Interface Description Language) is an IDL language that generates a piece of code that enables two processes running on an Android device to interact with the internal communication process. If you need to access the method of an object in another process (for example, a service) in a process (for example, in one activity), you can use AIDL to generate such code to disguise the passing of various parameters.

To use Aidl,service to provide the service interface as a aidl file, the Aidl tool generates a corresponding Java interface and includes a stub service pile class for the function call in the generated service interface. The implementation class of the service needs to inherit the stub services pile class. The Onbind method of the service returns the object of the implementation class, and then you can use it, see the following example:


Create a Imyremoteservice.aidl file first

 Package Org.allin.android.remote; Interface imusiccontrolservice{        void  play ();          void stop ();          void pause ();}



If you are using Eclipse's Android plugin, it will generate a Java interface class based on this aidl file. There is an inner class stub in the generated interface class, and all you have to do is inherit the stub class:

/** * @authorAllin.dev *http://allin.cnblogs.com/ * */ Public classRemotemusicserviceextendsService {Private Static FinalString TAG = "Remotemusicservice"; PrivateMediaPlayer MediaPlayer; /** (Non-javadoc) * * @see Android.app.service#onbind (android.content.Intent)*/@Override Publicibinder onbind (Intent Intent) {returnBinder; }     Private FinalImusiccontrolservic<script type= "Text/javascript" ><!--mce:1--></script>e.stub binder =Newimusiccontrolservice.stub () {@Override Public voidStop ()throwsremoteexception {log.d (TAG,"Stop ...."); if(MediaPlayer! =NULL) {mediaplayer.stop (); Try {                    //If you need to play through start again after calling stop, you need to call the Prepare function beforeMediaplayer.prepare (); } Catch(IOException ex) {ex.printstacktrace (); } }} @Override Public voidPlay ()throwsremoteexception {log.d (TAG,"Play ...."); if(MediaPlayer = =NULL) {MediaPlayer= Mediaplayer.create (Remotemusicservice. This, r.raw.tmp); Mediaplayer.setlooping (false); }            if(!mediaplayer.isplaying ())            {Mediaplayer.start (); }} @Override Public voidPause ()throwsremoteexception {log.d (TAG,"Pause ...."); if(MediaPlayer! =NULL&&mediaplayer.isplaying ())            {Mediaplayer.pause ();              }                   }     }; @Override Public voidOnDestroy () {Super. OnDestroy (); LOG.D (TAG,"OnDestroy"); if(MediaPlayer! =NULL) {mediaplayer.stop ();        Mediaplayer.release (); }    }}


When the client application connects to the service, the Onserviceconnected method is called and the client can obtain the IBinder object. See the following client Onserviceconnected method:

PrivateServiceconnection sc =Newserviceconnection () {@Override Public voidonservicedisconnected (componentname name) {Musicservice=NULL; LOG.D (TAG,"In Onservicedisconnected"); } @Override Public voidonserviceconnected (componentname name, IBinder service) {Musicservice=IMusicControlService.Stub.asInterface (service); LOG.D (TAG,"In Onserviceconnected"); }    };


Android Service (ii) remote services

Related Article

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: info-contact@alibabacloud.com 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.