Android in-depth introduction to Service mechanisms-1-services in Android

Source: Internet
Author: User

Reading directory

I. What is a Service?

Ii. layout file writing

Iii. Code File writing

Iv. Compilation of project definition files

V. Running Effect

I. What is a Service?

Service is one of the components of the Android system, and Activity, Intent, Conent Provider and said that the four kings of Android, the Service is invisible, there is no interface, it runs in the background, services generally process time-consuming and long-running operations. I used to provide a Windows service for an e-commerce website, that is, to review the unaudited orders issued by users. If the orders comply with certain specifications, they are approved, this service is always running, just as the e-commerce website has been running online, it can save a lot of manual time, so the Service generally handles operations that work for a long time.

I read a book saying, "Even services started through Activity do not run in the same process, but they belong to different processes ", I read the Android SDK "A Service is not a separate process. the Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part. the Service is not an independent process. The Service object itself does not mean that it runs in its own process, and it runs in the same process as the application. When there is a conflict between the theory of books and the Android SDK, we take the Android SDK as the standard, so we think that the Service and the application with it are in a process. Because we know that the Service and the application with it are in a process, if the operations in your Service are blocked, the whole application will not respond, so we open a new thread in the Service.

  Ii. layout file writing

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: orientation = "vertical" 4 android: layout_width = "fill_parent" 5 android: layout_height = "fill_parent">
6
7 <Button android: layout_width = "wrap_content" 8 android: layout_height = "wrap_content" 9 android: text = "Start Service" 10 android: id = "@ + id/start"/> 11 <Button android: layout_width = "wrap_content" 12 android: layout_height = "wrap_content" 13 android: text = "cancel Service" 14 android: id = "@ + id/end"/> 15 </LinearLayout>

 Iii. Code File writing

3.1 MainActivity. java

1 package com. menglin. service1; 2 3 import android. app. activity; 4 import android. content. intent; 5 import android. content. intentFilter; 6 import android. OS. bundle; 7 import android. view. view; 8 import android. view. view. onClickListener; 9 import android. widget. button; 10 11 public class MainActivity extends Activity12 {13 // declare two Button objects 14 private buttons btn_start, btn_end; 15 16 @ Override17 public void onCreate (Bundle savedInstanceState) 18 {19 super. onCreate (savedInstanceState); 20 // load the layout file main. xml21 setContentView (R. layout. main); 22 // obtain two Button objects through the findViewById () method 23 btn_start = (Button) findViewById (R. id. start); 24 btn_end = (Button) findViewById (R. id. end); 25 // bind the two buttons to the listener. Click Event 26 btn_start.setOnClickListener (btn_start_listener); 27 btn_end.setOnClickListener (btn_end_listener ); 28} 29 30 // listen to the event 31 private OnClickListener btn_start_listener = new OnClickListener () 32 {33 @ Override34 public void onClick (View v) 35 {36 // create an Intent object 37 Intent intent = new Intent (); 38 // The first parameter is your own class object, the second parameter is the 39 intent object of the Service to be called. setClass (MainActivity. this, Service1.class); 40 // start service 41 startService (intent); 42} 43}; 44 45 // listen to click event 46 private OnClickListener btn_end_listener = new OnClickListener () 47 {48 @ Override49 public void onClick (View v) 50 {51 // create an Intent object 52 Intent intent = new Intent (); 53 // The first parameter is your own class object, and the second parameter is the 54 intent object of the Service to be called. setClass (MainActivity. this, Service1.class); 55 // close service 56 stopService (intent); 57} 58}; 59}

3.2 Service1.java

1 package com. menglin. service1; 2 3 import android. app. service; 4 import android. content. intent; 5 import android. OS. IBinder; 6 import android. util. log; 7 8 public class Service1 extends Service 9 {10 11 private String TAG = "service"; 12 13 @ Override14 public IBinder onBind (Intent intent) 15 {16 // TODO Auto-generated method stub17 return null; 18} 19 20 // This method will be called when the Service is started 21 @ Override22 public void onCreate () 23 {24 Log. I (TAG, "onCreate"); 25 super. onCreate (); 26} 27 28 // This method will be called when the system is destroyed 29 @ Override30 public void onDestroy () 31 {32 Log. I (TAG, "onDestroy"); 33 super. onDestroy (); 34} 35 36 // This method will be called when the Service is started 37 @ Override38 public int onStartCommand (Intent intent, int flags, int startId) 39 {40 Log. I (TAG, "onStartCommand"); 41 return super. onStartCommand (intent, flags, startId); 42} 43 44}

Thu: Compile the project definition file

AndroidMainfest. xml

To make the Android system aware of this custom service, we need to add the service node <service android: name = ". Service1"> </service>.

V. Running Effect

When we click the "Start Service" button, we find that the system outputs the information we want for "onCreate". We click the "Start Service" button again and find that the onCreate () method is not called again, instead, the onStartCommand () method is called. After a service is started, it is always running in the background. If you want to start the service again, the onCreate () method will no longer be called, because this service has been created, but the onStartCommand () method is called directly, some of the main operations are implemented in the onStartCommand () method, that is, in onStartCommand () open a new thread in the method, and perform some operations based on the Inetent object passed in by the Activity. When we do not need this Service, We can click "cancel Service, here, we click the "cancel Service" button to not only stop the Service, but also output "onDestroy ".

Related Article

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.