Getting started with Android: getting started with service

Source: Internet
Author: User


I. Service Introduction

A service is similar to a service in windows. It only runs in the background without an interface. A service cannot run on its own, but needs to call context. startservice (intent); or context. bindservice (intent) enables the service;

There are two types of services:

(1) If the visitor has nothing to do with the service, after the visitor is disabled, the service can continue to run and startservice () is used ();

(2) If the visitor is related to the service (for example, the visitor needs to call the method provided by the Service), the service must be closed after the visitor is closed and bindservice () is used ();


Services are divided:

(1) local services: visitors and services are in one application;

(2) remote service: visitors and services are used by different users;

Note: If you want the service to run automatically upon startup, you can create a broadcast receiver and call the enable service code in onreceive;

Here, we will only introduce the situation that "visitors are not related to the service". We will discuss another situation later;

Ii. Core Service Code

(1) create a class that inherits services

(2) Configure <service> In androidmanifest. xml

(3) override oncreate () in service; Method

Start Service:


Intent service = new intent (context, xxxservice. Class );

Context. startservice (service );

Then the oncreate () method is called;

Stop Service:


Intent service = new intent (context, xxxservice. Class );

Context. stopservice (service );

Then the ondestroy () method is called;

3. Service Lifecycle

1. Start the service through context. startservice ()

Oncreate () --> onstart () --> ondestroy ()

Oncreate () is called when you create a service. If you call startservice () multiple times, the oncreate () method is still called only once;

Onstart () is called when startservice () is called. Multiple startservice () and onstart () methods are called multiple times;

Ondestroy () is called when the service is terminated;

2. Start the service through context. bindservice ()

Oncreate () --> onbind () --> onunbind () --> ondestroy ()

Onbind () is called when a service is bound. If you call bindservice () multiple times, the onbind () method is called only once;

Onunbind () is called when unbindservice is unbound;

Iv. mediaplayer core code

Mediaplayer player = mediaplayer. Create (getapplicationcontext (), R. Raw. xiazdong); // create a mediaplayer object

Player. setlooping (true); // loop playback

Player. Start (); // start playing

Player. Stop (); // stop playing

Player. Release (); // release resources

4. Apply MP3 playback to service


Here, you can only click the activity to play the music, and then start playing the music. The code for playing the music is put into the service. Therefore, even if the application is disabled, the music will not be stopped;

The effect is as follows:


Click the button in mainactivity to execute the following code:


Private onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view v) {If (V = button) {intent service = new intent (mainactivity. this, mediaplayerservice. class); mainactivity. this. startservice (service); // enable service} If (V = button2) {intent name = new intent (mainactivity. this, mediaplayerservice. class); mainactivity. this. stopservice (name); // stop the service }}};

Mediaplayerservice. Java

Package COM. xiazdong. mediaplayer; import android. app. service; import android. content. intent; import android. media. mediaplayer; import android. OS. ibinder; public class mediaplayerservice extends Service {private mediaplayer player; @ overridepublic ibinder onbind (intent) {return NULL ;}@ overridepublic void oncreate () {super. oncreate (); player = mediaplayer. create (getapplicationcontext (), R. raw. xiazdon G); player. setlooping (true); try {// because the mediaplayer create method has called the prepare method, you can directly start the method here. start ();} catch (exception e) {e. printstacktrace () ;}@ overridepublic void ondestroy () {// stop the Super Service. ondestroy (); If (player! = NULL) {player. Stop (); player. Release (); player = NULL ;}}}

Add the following in androidmanifest. xml:

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

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.