Android Creation Service (i)

Source: Internet
Author: User

Service, one of the four components of Android, plays a very important role in Android development and is an application component that runs in the background without a user interface to perform time-consuming operations.

A service that starts with StartService () is in the "Started" state, and once started, the service is running in the background, even if the build that started it is destroyed.

Service can be used in many applications, such as multimedia playback when the user started other activity this time the program to continue to play in the background, such as the detection of changes in the file on the SD card, or in the background to record your geographic information changes, etc., in short, the service is always hidden in the background.

Below, let's look at how to create, using service

First define a class, inherit Android.app.Service, and rewrite onCreate,OnDestroy, Onstartcommand three methods

 Public classMyServiceextendsService {@Override Public voidonCreate () {LOG.I ("Shxtapp", "Create service"); } @Override Public voidOnDestroy () {LOG.I ("Shxtapp", "Stop Service"); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {LOG.I ("Shxtapp", "Start service"); return Super. Onstartcommand (Intent, flags, Startid); }}

and then

Register in Androidmanifest.xml, position with activity

< Service     Android:name =". MyService "></service>

Then, before you need to start the service, create a intent that points to the

New Intent (This, MyService.  Classnew myserviceconn ();

When you need to start a service

StartService (Intent);

When the need to end the service

StopService (Intent);

In the Onstartcommand method of the code, is the service to do the work, services generally do not exist alone, generally and broadcast coexistence, when receiving the designated broadcast, call service services to do the corresponding processing.

Service there is another way to use, we will introduce the next chapter, Goodbye!

Android Creation Service (i)

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.