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)