Aidl and remote service calls

Source: Internet
Author: User

[Part 1]

The content of this lecture: aidl and remote service calls the source code: app_elfplayer. It is difficult to understand the content. You may not understand a lot of information, but it is easy to use. So let's take an example of the progress bar in a music player to illustrate the value and usage of aidl and remote service. You can skip this example and try it out. The example below is part of the project instance I am preparing. First, describe the problems we are facing. If you do not understand the following descriptions, please refer to the previous courses: First, we know that in Android, if you need to play music, the most important method is to use the built-in mediaplayer object.
The mediaplayer object is used for playback. Once you open another program, such as a browser, the song will immediately stop. This is of course not the result we need. What we need is to be able to listen to songs in the background while doing other things, so we need to put operations on the mediaplayer object in the background service. Second, we have moved the mediaplayer operation to the service. According to our previous practice, we send an intent object to the service object in the activity, in intent, send the playback, pause, and other information to the service, so that the service knows what to do. It looks wonderful,
But now a new problem occurs, that is, I want to display a progress bar in the activity. This progress bar will be synchronized to the front with the progress of the song in the mediaplayer in the service, how can I achieve this if I click a position in the progress bar and want to redirect the song to a new time point to continue playing? Third, we need to operate the mediaplayer object in the service in the activity, just as this object is our own. We can use the android Interface Definition Language (aidl) technology: 1. encapsulate mediaplayer operations in the service into an interface (. aidl file)
2. Create a subclass in the service to implement the interface's Stub (stub) object 3. Return the stub object in the onbind () method. 4. Use the Service binding method in the activity to connect to the service, but do not use intent to transmit information. Instead, use the agent to obtain the stub object client of the Service in the onserviceconnected method of serviceconnection. You can perform operations on the mediaplayer object in the service by operating the agent in the activity. In this way, we can operate on objects in the service like local objects, so the requirement for a progress bar will naturally welcome the edge.
. The following example is not specifically prepared for this lecture, so some irrelevant code is not added with comments. Please forgive me (this example will be provided in the project training and is being prepared ): 1. Create a project app_elfplayer and start activity as a startup screen: coveractivity2 and androidmanifest. the XML content is as follows: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 we noticed that there are 2 activities, 1 service, permission statement for reading and writing external storage 3. coveractivity. the Java code is as follows: This is a full screen startup screen, which will jump to playeractivity01 in 2 seconds.
Package app. android. elfplayer; 02 03 import android. app. activity; 04 import android. content. intent; 05 import android. OS. bundle; 06 import android. OS. handler; 07 import android. view. window; 08 import android. view. windowmanager; 09 10 public class coveractivity
Extends activity {11/** called when the activity is first created. */12 @ override 13 public void oncreate (bundle savedinstancestate) {14 super. oncreate (savedinstancestate); 15 getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen );
16 requestwindowfeature (window. feature_no_title); 17 setcontentview (R. layout. cover); 18 19 new handler (). postdelayed (New runnable () {20 21 @ override 22 public void run () {23 intent mainintent = new intent (coveractivity. this, playeractivity. class); 24 coveractivity. this. startactivity (mainintent );
25 coveractivity. this. finish (); 26} 27 28 }, 2000); 29 30} 31} 4. playeractivity. the Java code is as follows: 001 package app. android. elfplayer; 002 003 import android. app. activity; 004 import android. content. componentname; 005 import android. content. context; 006 import android. content. intent;
007 import android. content. serviceconnection; 008 import android. OS. bundle; 009 import android. OS. handler; 010 import android. OS. ibinder; 011 import android. OS. message; 012 import android. OS. remoteException; 013 import android. util. log; 014 import android. view. view;
015 import android. widget. imagebutton; 016 import android. widget. seekbar; 017 import android. widget. seekbar. onseekbarchangelistener; 018 019 public class playeractivity extends activity {020 021 public static final int play = 1; 022 public static final int
Pause = 2; 023 024 imagebutton blocks; 025 imagebutton blocks; 026 imagebutton blocks; 027 imagebutton blocks; 028 imagebutton blocks; 029 seekbar blocks; 030 031 blocks iPlayer; 032 Boolean isplaying
= False; 033 Boolean isloop = false; 034 035 @ override 036 public void oncreate (bundle savedinstancestate) {037 super. oncreate (savedinstancestate); 038 setcontentview (R. layout. player); 039 040 imagebuttonfavorite = (imagebutton) findviewbyid (R. id. imagebuttonfavorite );
041 imagebuttonnext = (imagebutton) findviewbyid (R. id. imagebuttonnext); 042 imagebuttonplay = (imagebutton) findviewbyid (R. id. imagebuttonplay); 043 imagebuttonpre = (imagebutton) findviewbyid (R. id. imagebuttonpre); 044 imagebuttonrepeat = (imagebutton) findviewbyid (R. id. imagebuttonrepeat );
045 musicseekbar = (seekbar) findviewbyid (R. id. musicseekbar); 046 047 bindservice (new intent (playeractivity. this, musicservice. class), Conn, context. bind_auto_create); 048 startservice (new intent (playeractivity. this, musicservice. class); 049 050 imagebuttonplay. setonclicklistener (New
View. onclicklistener () {051 052 @ override 053 public void onclick (view v) {054 log. I ("Yao", "imagebuttonplay-> onclick"); 055 056 if (! Isplaying) {057 try {058 iPlayer. play (); 059} catch (RemoteException e) {060 E. printstacktrace (); 061} 062 imagebuttonplay. setbackgroundresource (R. drawable. pause_button );
063 isplaying = true; 064 065} else {066 try {067 iPlayer. pause (); 068} catch (RemoteException e) {069 E. printstacktrace (); 070} 071 imagebuttonplay. setbackgroundresource (R. drawable. play_button); 072 isplaying = false; 073} 074} 075}); 076 077 musicseekbar. setonseekbarchangelistener (New
Secret () {078 079 @ override 080 public void onprogresschanged (seekbar, int progress, Boolean fromuser) {081} 082 083 @ override 084 public void onstarttrackingtouch (seekbar) {085} 086 087 @ override 088 public void onstoptrackingtouch (seekbar
Seekbar) {089 if (iPlayer! = NULL) {090 try {091 iPlayer. seekto (seekbar. getprogress (); 092} catch (RemoteException e) {093 E. printstacktrace (); 094} 095} 096} 097}); 098 099 handler. post (updatethread); 100} 101 102 private serviceconnection Conn
= New serviceconnection () {103 Public void onserviceconnected (componentname classname, ibinder Service) {104 log. I ("Yao", "serviceconnection-> onserviceconnected"); 105 iPlayer = iserviceplayer. stub. asinterface (service); 106} 107 108 public void onservicedisconnected (componentname
Classname) {109}; 110}; 111 112 handler = new handler () {113 @ override 114 public void handlemessage (Message MSG) {115}; 116 }; 117 118 private runnable updatethread = new runnable () {119 @ override 120 public void run () {121 If (iPlayer! =
Null) {122 try {123 musicseekbar. setmax (iPlayer. getduration (); 124 musicseekbar. setprogress (iPlayer. getcurrentposition (); 125} catch (RemoteException e) {126 E. printstacktrace (); 127} 128} 129 handler. post (updatethread); 130} 131}; 132 133} 5. iserviceplayer used. aidl is placed in the same package as the Java file. The content is as follows: 01
Package app. android. elfplayer; 02 interface iserviceplayer {03 void play (); 04 void pause (); 05 void stop (); 06 int getduration (); 07 int getcurrentposition (); 08 void seekto (INT current); 09 Boolean setloop (Boolean loop); 10} once you have written this iserviceplayer. aidl file, ADT will automatically help you generate iserviceplayer In the gen directory. java file 6, musicservice. java content: 01
Package app. android. elfplayer; 02 03 import android. app. service; 04 import android. content. intent; 05 import android. media. mediaplayer; 06 import android. OS. ibinder; 07 import android. OS. remoteException; 08 import android. util. log; 09 10 public class musicservice
Extends Service {11 12 string tag = "Yao"; 13 14 public static mediaplayer mplayer; 15 16 public Boolean ispause = false; 17 18 iserviceplayer. stub stub = new iserviceplayer. stub () {19 20 @ override 21 public void play () throws RemoteException {22 mplayer. start ();
23} 24 25 @ override 26 Public void pause () throws RemoteException {27 mplayer. pause (); 28} 29 30 @ override 31 public void stop () throws RemoteException {32 mplayer. stop (); 33} 34 35 @ override 36 Public int getduration () throws RemoteException {37 return
Mplayer. getduration (); 38} 39 40 @ override 41 public int getcurrentposition () throws RemoteException {42 return mplayer. getcurrentposition (); 43} 44 45 @ override 46 Public void seekto (INT current) throws RemoteException {47 mplayer. seekto (current); 48
} 49 50 @ override 51 public Boolean setloop (Boolean loop) throws RemoteException {52 return false; 53} 54 55}; 56 57 @ override 58 public void oncreate () {59 log. I (TAG, "musicservice oncreate ()"); 60 mplayer = mediaplayer. create (getapplicationcontext (),
Elfplayerutil. getfileinsd ("wind.mp3"); 61} 62 63 @ override 64 public ibinder onbind (intent) {65 return stub; 66} 67 68} 7. For other code and resources, refer to the source code included in this lecture, compile and run the program, and view the results. Finally, aidl provides a very simple method, this allows us to expose an object or method in a process to another program, just as another program has these features. Finally, I would like to thank a song website for using their UI elements for its image materials.

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.