requirements: I want to make the service run in the background for a long time and want to invoke the method inside the servicemixed-mode open service(1) First call the StartService () method to ensure that the service runs in the background for a long time(2) call Bindservice () to get the method that we defined in the man-in-the-middle object invocation service(3) Unbindservice () see if the service will be destroyed(4) Last Call StopService () stop service
//Music Playback Services Public classMusicserviceextendsService {//[2] returning the man-in-the-middle object we define@Override Publicibinder onbind (Intent Intent) {return NewMybinder (); } //The first time the service is opened is the call@Override Public voidonCreate () {Super. OnCreate (); } //called when the service is destroyed@Override Public voidOnDestroy () {Super. OnDestroy (); } //dedicated to playing music. Public voidPlaymusic () {System.out.println ("The music is playing."); //TODO and so finish the multimedia to improve the function } //the music is suspended. Public voidPausemusic () {System.out.println ("The music is suspended."); } //How the music continues to play Public voidReplaymusic () {System.out.println ("The music continues to play."); } //[1] Defining a man-in-the-middle object (IBinder) Private classMybinderextendsBinderImplementsiservice{//call the method of playing music@Override Public voidCallplaymusic () {playmusic (); } //How to call pause music@Override Public voidCallpausemusic () {pausemusic (); } //method of calling resume playback@Override Public voidCallreplaymusic () {replaymusic (); } } }
Public Interface IService { // Put the method we want to expose into the interface public void callplaymusic (); publicvoid callpausemusic (); Public void Callreplaymusic (); }
Public classMainactivityextendsActivity {PrivateIService IService;//This is what we define as the middleman. PrivateMyConn Conn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //[0] First call the StartService method to open the Service guarantee service in the background long-term operationIntent Intent =NewIntent ( This, Musicservice.class); StartService (Intent); //[1] The purpose of calling Bindservice is to get our defined man-in-the-middle objectconn =Newmyconn (); //Connect the Musicservice service get our defined man-in-the-middle objectBindservice (Intent, Conn, bind_auto_create); } //Click the button to play music Public voidClick1 (View v) {//call the method of playing musicIservice.callplaymusic (); } //Pause Music Public voidClick2 (View v) {//How to call pause musicIservice.callpausemusic (); } //continue playing Public voidClick3 (View v) {//call to continue playingIservice.callreplaymusic (); } //called when activity is destroyed@Overrideprotected voidOnDestroy () {//Unbind service when activity is destroyedUnbindservice (conn); Super. OnDestroy (); } Private classMyConnImplementsserviceconnection {//called when the connection succeeds@Override Public voidonserviceconnected (componentname name, IBinder service) {//get our defined man-in-the-middle objectIService =(iservice) service; } @Override Public voidonservicedisconnected (componentname name) {} }}
Baidu Music Box Frame