Android background services-services without interaction

Source: Internet
Author: User

Service is one of the four main components of Android. It is mainly responsible for background services and runs for a long time.

I. Main Features

1) The priority is higher than the Activity. Even if the Activity is stopped, it will not be terminated easily.

2) even if it is terminated by the system, the Service automatically returns to the running status after the copper replies to the resource.

3) No interface operations, consistent operation in the background

Ii. Lifecycle

Life cycle: after the Activity is started, the onCreate method creates the Service and the onStartCommand method once, while the onStartCommand method executes multiple times when the Activity is started multiple times. When the Service is terminated, the onDestory method is executed.

The trigger methods include onCreate, onStartCommand, and onDestory.

Iii. Creation and Method

When using the Service, you must inherit from the Service abstract class to implement the methods. Common methods include:

1) onCreate): Not Required to be overwritten. Creates, initializes, or all services. It is executed once when the Service is started, and only once

2) onStartCommand (): it must not be overwritten. Responsible for the work that the Service should do. Each time the Service is started, it is executed once.

3) onDestory): Release the resource. Only once when disabled

4. Start and Stop Modes

1: do not interact with users

Startup method: startService (intent object)

Termination method: stopService (intent object)

5. services that do not interact with users

Test method:

◆ Create a class MyService inherited from the Service, override the onCreate, onStartCommand, and onDestory methods, and add an output statement to each method body.

Public class MyService extends Service {// method triggered when interacting with the user, but both life cycles must be overwritten @ Override public IBinder onBind (Intent intent) {return null ;} // used to create a Service and complete initialization. You can @ Override public void onCreate () {System. out. println ("onCreate"); super. onCreate () ;}// used to mark the start of the Activity life cycle and complete the work @ Override public int onStartCommand (Intent intent, int flags, int startId) {System. out. println ("OnStartCommand"); return super. onStartCommand (intent, flags, startId);} // end of the lifecycle @ Override public void onDestroy () {System. out. println ("onDestory"); super. onDestroy ();}}

◆ On the main interface, add two buttons: start and stop, and add a listener for the two buttons.

Public class MainActivity extends Activity implements OnClickListener {private Intent intent; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. start ). setOnClickListener (this); // Add the findViewById (R. id. stop ). setOnClickListener (this); // Add listening intent = new Intent (this, MyService. class) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. start: startService (intent); // start Service break; case R. id. stop: stopService (intent); // terminate Service break ;}}}

◆ View the background output, which may occur as follows:

1) After the Start button is clicked for the first time: the onStart method and the onStartCommand method are executed. 2) after the Start button is clicked for the second time, only the onStartCommand method is executed. 3) after each start button is clicked, The onStartCommand method is executed only once. 4) after the Stop button is clicked, The onDestory method is executed once.

Vi. Specific applications

Function implementation: provides a timer in the Service to let it count and observe the results when start and stop.

◆ MyService

Public class MyService extends Service {private Timer timer; // The Timer declares private TimerTask task; // The timer Task declares private int I = 0; // variable initialization @ Override public IBinder onBind (Intent intent) {return null ;}@ Override public void onCreate () {super. onCreate (); System. out. println ("onCreate -----") ;}@ Override public int onStartCommand (Intent intent, int flags, int startId) {System. out. println ("onStartCommand -----"); start (); // put the method of the start timer in this method return super. onStartCommand (intent, flags, startId) ;}@ Override public void onDestroy () {super. onDestroy (); stop (); // put the method for disabling the timer in this method System. out. println ("onDestroy -----");} // start public void start () {timer = new Timer (); // instantiate the timer task = new TimerTask () {// instantiate the timer Task @ Override public void run () {I ++; // variable auto-increment System. out. println (I); // Print Output}; timer. schedule (task, 1000,100 0); // start timer} // disable public void stop () {timer. cancel ();}}

◆ The main interface is the same as above

◆ Result Display

1) Click start for the first time: starts the onCreate and onStartCommand methods, and the timer in onStartCommand starts the work cycle count output.

2) Click the return key to continue the output.

3) Click Stop to Stop the output. The onDestory method is executed.

13:25:45 08-31. 703: I/System. out (2055): onCreate ---- // after you click start for the first time, 08-31 13:25:45. 703: I/System. out (2055): onStartCommand ----- 08-31 13:25:46. 764: I/System. out (2055): 108-31 13:25:47. 779: I/System. out (2055): 208-31 13:25:48. 815: I/System. out (2055): 308-31 13:25:49. 883: I/System. out (2055): 408-31 13:25:50. 893: I/System. out (2055): 508-31 13:25:51. 922: I/System. out (2055): 608-31 13:25:52. 923: I/System. out (2055): 708-31 13:25:53. 991: I/System. out (2055): 8 // click back to continue output 08-31 13:25:54. 983: I/System. out (2055): 908-31 13:25:55. 998: I/System. out (2055): 1008-31 13:25:57. 000: I/System. out (2055): 1108-31 13:25:57. 433: I/System. out (2055): 1208-31 13:25:58. 456: I/System. out (2055): 1308-31 13:25:59. 039: I/System. out (2055): 1408-31 13:25:59. 474: I/System. out (2055): 1508-31 13:25:59. 474: I/System. out (2055): onDestory ---- // after you click stop

Note: The Service is a single thread. If you click Start multiple times, even if you click the stop button for the same number of times, you can only terminate the Service that you first triggered.


There is also a way to start the Service. We will continue tomorrow .. Computers are not powerful. 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/103K25428-0.gif "/>


This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1286436

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.