Service: test the aidl IPC Tool

Source: Internet
Author: User

1. Service Introduction


There are two types of service: local and remote.


Local is mainly used for calling between local applications, and remote is mainly used between different applications or processes. Here the aidl IPC Mechanism is used.


For details about the aidl IPC Mechanism, refer to SDK Dev guide!

Of course, it may take some time if you want to fully understand this mechanism, but I just want to use it !~ ~

 

Ii. Service and Process


Android components run in the same process by default. They are managed by their respective processes. They are part of the main thread rather than the thread opened by the system.

That is to say, if you perform time-consuming operations on these components (including services), you need to start another thread.


You can use the Android: Process = attribute to describe the process of an application.


Iii. remote service


Let's take a look at the file directory structure.


Create an aidl file under the corresponding package. Here is the imusicservice. aidl file.


The ADT will automatically generate the corresponding Java source file for you.


Imusicservice. aidl file:

Package csdn. blog. myaidl; <br/> interface imusicservice {<br/> void play (); <br/> void stop (); <br/> void pause (); <br/>}

In this way, we can use it in the application...

Package csdn. blog; <br/> Import Java. io. ioexception; <br/> Import android. app. service; <br/> Import android. content. intent; <br/> Import android. media. mediaplayer; <br/> Import android. OS. ibinder; <br/> Import android. OS. remoteException; <br/> Import android. util. log; <br/> Import csdn. blog. myaidl. imusicservice; <br/> public class musicplayerservice extends Service {<br/> // debug <br/> Private Static final S Tring tag = "service_06_remote"; </P> <p> private mediaplayer mplayer = NULL; </P> <p> private imusicservice. stub binder = new imusicservice. stub () {</P> <p> @ override <br/> Public void stop () throws RemoteException {<br/> // todo auto-generated method stub <br/> If (mplayer! = NULL) {<br/> mplayer. stop (); <br/> try {<br/> mplayer. prepare (); <br/>} catch (illegalstateexception e) {<br/> log. E (TAG, E. tostring (); <br/>}catch (ioexception e) {<br/> log. E (TAG, E. tostring (); <br/>}</P> <p> @ override <br/> Public void play () throws RemoteException {<br/> // todo auto-generated method stub <br/> If (mplayer = NULL) {<br/> mplayer = mediaplayer. create (musicplayerser Vice. This, R. Raw. Tiger); <br/>}</P> <p> If (! Mplayer. isplaying () {<br/> mplayer. start (); <br/>}</P> <p> @ override <br/> Public void pause () throws RemoteException {<br/> // todo auto-generated method stub <br/> If (mplayer! = NULL & mplayer. isplaying () {<br/> mplayer. pause (); <br/>}< br/>}; <br/> @ override <br/> Public void oncreate () {<br/> // todo auto-generated method stub <br/> // log. D (TAG, "musicplayerservice --> oncreate -- onbind"); // invalid </P> <p> // debug thread <br/> system. out. println ("musicplayerservice id -->" + thread. currentthread (). GETID (); <br/> system. out. println ("musicplayerservice name -->" + thread. currentthread (). getname (); </P> <p> system. out. println ("musicplayerservice --> oncreate -- onbind"); <br/> super. oncreate (); <br/>}< br/> @ override <br/> Public ibinder onbind (intent) {<br/> // todo auto-generated method stub <br/> try {<br/> binder. play (); <br/>}catch (RemoteException e) {<br/> log. E (TAG, E. tostring (); <br/>}</P> <p> // log. D (TAG, "log musicplayerservice --> onbind"); // invalid <br/> system. out. println ("musicplayerservice --> onbind"); <br/> return binder; <br/>}< br/> @ override <br/> Public Boolean onunbind (intent) {<br/> // todo auto-generated method stub <br/> // log. D (TAG, "musicplayerservice --> onunbind"); // invalid <br/> system. out. println ("musicplayerservice --> onunbind"); <br/> return Super. onunbind (intent); <br/>}< br/> @ override <br/> Public void ondestroy () {<br/> // todo auto-generated method stub <br/> // log. D (TAG, "musicplayerservice --> ondestroy"); // invalid <br/> system. out. println ("musicplayerservice --> ondestroy"); <br/> mplayer. stop (); <br/> mplayer. release (); <br/> super. ondestroy (); <br/>}< br/>}

In activity, I do this:

Package csdn. blog; <br/> Import android. app. activity; <br/> Import android. content. componentname; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. content. serviceconnection; <br/> Import android. OS. bundle; <br/> Import android. OS. ibinder; <br/> Import android. OS. remoteException; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import csdn. blog. myaidl. imusicservice; <br/> public class service_06_remoteact extends activity implements onclicklistener {<br/> Private Static final string tag = "service_06_remoteact "; </P> <p> Private Static final string service_action = "csdn. blog. musicplayerservice "; </P> <p> private button btnplay, btnstop, btnpause, btnexit = NULL; </P> <p> private imusicservice mimusicplayerservice = NULL; </P> <p> private serviceconnection mconnection = new serviceconnection () {</P> <p> @ override <br/> Public void onservicedisconnected (componentname name) {<br/> // todo auto-generated method stub <br/> log. D (TAG, "onservicedisconnected ()"); <br/> mimusicplayerservice = NULL; <br/>}</P> <p> @ override <br/> Public void onserviceconnected (componentname name, ibinder Service) {<br/> // todo auto-generated method stub <br/> log. D (TAG, "onserviceconnected ()"); <br/> mimusicplayerservice = imusicservice. stub. asinterface (service); <br/>}< br/>}; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> // debug thread <br/> system. out. println ("Main id -->" + thread. currentthread (). GETID (); <br/> system. out. println ("main name -->" + thread. currentthread (). getname (); </P> <p> btnplay = (button) findviewbyid (R. id. button_play); <br/> btnstop = (button) findviewbyid (R. id. button_stop); <br/> btnpause = (button) findviewbyid (R. id. button_pause); <br/> btnexit = (button) findviewbyid (R. id. button_exit); <br/> btnplay. setonclicklistener (this); <br/> btnstop. setonclicklistener (this); <br/> btnpause. setonclicklistener (this); <br/> btnexit. setonclicklistener (this); </P> <p> connection (); <br/>}</P> <p> @ override <br/> protected void ondestroy () {<br/> // todo auto-generated method stub <br/> log. D (TAG, "ondestroy --> unbindservice"); <br/> unbindservice (mconnection); <br/> super. ondestroy (); <br/>}< br/> private void connection () {<br/> // todo auto-generated method stub <br/> log. D (TAG, "bindservice"); <br/> bindservice (new intent (service_action), mconnection, context. bind_auto_create); <br/>}< br/> @ override <br/> Public void onclick (view V) {<br/> // todo auto-generated method stub <br/> switch (v. GETID () {<br/> case R. id. button_play: <br/> try {<br/> mimusicplayerservice. play (); <br/>}catch (RemoteException e) {<br/> log. E (TAG, E. tostring (); <br/>}< br/> break; <br/> case R. id. button_stop: <br/> try {<br/> mimusicplayerservice. stop (); <br/>}catch (RemoteException e) {<br/> log. E (TAG, E. tostring (); <br/>}< br/> break; <br/> case R. id. button_pause: <br/> try {<br/> mimusicplayerservice. pause (); <br/>}catch (RemoteException e) {<br/> log. E (TAG, E. tostring (); <br/>}< br/> break; <br/> case R. id. button_exit: <br/> finish (); <br/> break; <br/> default: <br/> break; <br/>}< br/>}

Finally, do not forget to declare in the manifest. XM file:

<! -- Android: Process = ": Remote" indicates that the service runs normally in another process without writing this code. <br/>. <Br/> ": Remote" is only an identifier and can be named at will, for example, "anyname". <br/> --> <br/> <service android: Name = ". musicplayerservice "Android: Process =": Remote "> <br/> <intent-filter> <br/> <action Android: Name =" csdn. blog. musicplayerservice "/> <br/> </intent-filter> <br/> </service


Run cmd to view the PID and process ID of the Android Application in the command line ADB shell ps.









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.