Android Study Notes (6)-usage of service + mediaplayer

Source: Internet
Author: User

1. What is service?

The service can be used in multiple applications. For example, when the user starts other activities during multimedia playback, the program must continue playing the video in the background, for example, detecting file changes on the SD card, or record the changes in your location in the background. In short, the service is always stored in the background.


2. Two MethodsService Startup Process

Context. startservice () Startup Process:


Context. startservice ()-> oncreate ()-> onstart ()-> Service Running-> context. stopservice ()-> ondestroy ()-> service stop

If the service is not running, Android calls oncreate () and then onstart ();

If the service is already running, only onstart () is called. Therefore, the onstart method of a service may be called multiple times.

If the stopservice is called ondestroy directly, if the caller directly exits without calling the stopservice, the Service will continue to run in the background. After the service is started, the caller can use the stopservice to disable the service.

Therefore, the life cycle for calling startservice is oncreate --> onstart (which can be called multiple times) --> ondestroy


Context. bindservice () Startup Process:

Context. bindservice ()-> oncreate ()-> onbind ()-> Service Running-> onunbind ()-> ondestroy ()-> service stop
 

Onbind () returns an ibind interface instance to the client. ibind allows the client to call back the service methods, such as obtaining the service instance, running status, or other operations. At this time, the caller (such as activity) will be bound with the service, and the context will exit, and srevice will call onunbind-> ondestroy to exit accordingly.

Therefore, the life cycle of calling bindservice is oncreate --> onbind (only once, cannot be bound multiple times) --> onunbind --> ondestory.

During each service enabling and disabling process, only onstart can be called multiple times (through multiple startservice calls), other oncreate, onbind, onunbind, ondestory can only be called once in a lifecycle.


3. Service Lifecycle

The lifecycle of a service is not as complex as that of an activity. It only inherits three methods: oncreate (), onstart (), and ondestroy ().

When we start the service for the first time, we call the oncreate () and onstart () Methods successively. when the service is stopped, the ondestroy () method is executed.

Note that if the service has been started, when we start the service again, the oncreate () method is not executed, but the onstart () method is directly executed.

4. A simple example:

Define a myservice class

Package COM. ideal. study; import android. app. service; import android. content. intent; import android. media. mediaplayer; import android. OS. ibinder; public class myservice extends Service {private mediaplayer MP; @ overridepublic void oncreate () {// todo auto-generated method stub // initialize the Music Resource try {system. out. println ("create player"); // create a mediaplayer object MP = new mediaplayer (); MP = mediaplayer. create (myservice. this, R. raw. bgmusic); // MP. prepare ();} catch (illegalstateexception e) {// todo auto-generated catch blocke. printstacktrace ();} super. oncreate () ;}@ overridepublic void onstart (intent, int startid) {// todo auto-generated method stub // start playing music MP. start (); // process the event after the music is played. MP. setoncompletionlistener (New mediaplayer. oncompletionlistener () {public void oncompletion (mediaplayer MP) {// todo auto-generated method stub // loop playback try {MP. start ();} catch (illegalstateexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}); // An error occurred while playing the music. setonerrorlistener (New mediaplayer. onerrorlistener () {public Boolean onerror (mediaplayer MP, int what, int extra) {// todo auto-generated method stub // release the resource try {MP. release ();} catch (exception e) {e. printstacktrace () ;}return false ;}}); super. onstart (intent, startid) ;}@ overridepublic void ondestroy () {// todo auto-generated method stub // stop playing music and release resource MP when the service is stopped. stop (); MP. release (); super. ondestroy () ;}@ overridepublic ibinder onbind (intent) {// todo auto-generated method stubreturn NULL ;}}

Call other required activities

Open service

// Play the background music intent intentsv = new intent (learningflashactivity. This, myservice. Class); startservice (intentsv );

Disable Service

// Disable intent intentsv = new intent (learninglist. This, myservice. Class); stopservice (intentsv );

Remember to register

<service            android:name=".MyService" />

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.