Android service learning (II)

Source: Internet
Author: User

 

Android service learning (II)

 

Generally, each application runs in its own process, but sometimes it needs to pass objects between processes, you can use the application UI to write a service that runs in a different process. On the Android platform, a process generally cannot access the memory area of other processes. Therefore, they need to split objects into simple forms that the operating system can understand, so as to disguise them as cross-border access. Writing this disguised code is rather boring. Fortunately, Android provides an aidl tool for us to do this.

 

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

 

To use aidl, the Service must provide the service interface in the form of an aidl file. The aidl tool will generate a corresponding Java interface, in addition, the generated Service Interface contains a function-called stub service pile class. The service implementation class must inherit the stub service pile class. The onbind method of the Service will return the object of the implementation class, and then you can use it. See the following example:

 
Create an imyremoteservice. aidl file first

 


package org.allin.android.remote;interface IMusicControlService{        void play();         void stop();         void pause();}


 
If you are using the android plug-in of Eclipse, it will generate a Java Interface Class Based on the aidl file. The generated interface class has an internal class stub. What you need to do is inherit the class stub:
 

/*** @ Author allin. dev * http://allin.cnblogs.com/*/public class remotemusicservice extends Service {Private Static final string tag = "remotemusicservice"; private mediaplayer;/* (non-javadoc) ** @ see android. app. service # onbind (Android. content. intent) * // @ overridepublic ibinder onbind (intent) {return binder;} private final imusiccontrolservice. stub binder = new imusiccontrol Service. Stub () {@ overridepublic void stop () throws RemoteException {log. D (TAG, "Stop..."); If (mediaplayer! = NULL) {mediaplayer. stop (); try {// call the stop function mediaplayer before playing the video again through start. prepare ();} catch (ioexception ex) {ex. printstacktrace () ;}}@ overridepublic void play () throws RemoteException {log. D (TAG, "play .... "); If (mediaplayer = NULL) {mediaplayer = mediaplayer. create (remotemusicservice. this, R. raw. TMP); mediaplayer. setlooping (false);} If (! Mediaplayer. isplaying () {mediaplayer. start () ;}@ overridepublic void pause () throws RemoteException {log. D (TAG, "pause .... "); If (mediaplayer! = NULL & mediaplayer. isplaying () {mediaplayer. pause () ;}};@ overridepublic void ondestroy () {super. ondestroy (); log. D (TAG, "ondestroy"); If (mediaplayer! = NULL) {mediaplayer. Stop (); mediaplayer. Release ();}}}

 
 
When the client application connects to this service, the onserviceconnected method is called, and the client can obtain the ibinder object. See the onserviceconnected method of the client below:
 

private ServiceConnection sc = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {musicService = null;Log.d(TAG, "in onServiceDisconnected");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {musicService = IMusicControlService.Stub.asInterface(service);Log.d(TAG, "in onServiceConnected");}};

 
The startup interface is as follows:
 

 
 
[Download source code]


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.