Detailed explanation of Service (Background Service) in Android

Source: Internet
Author: User

Detailed explanation of Service (Background Service) in Android

This article mainly introduces the Service (Background Service) details in Android. This article describes the concept, role, lifecycle, startup method, and code instance of the Service. For more information, see

1. concept:

(1). Service is an Activity that runs in the background. It is not a separate process. It only requires the application to tell it what to do in the background.

(2) If it is used to achieve interaction with the user, it needs to receive the display through the notification bar or by sending the broadcast, the UI.

(3). It is widely used, especially at the framework layer. More applications are called to system services.

2. role:

(1) It is used to handle background operations that do not interfere with the user's use. Such as download and network retrieval. It can be used to play music. It can be enabled through INTENT and bound to a host object (caller such as ACTIVITY.

(2) If Activity displays information on the front-end page, the Service performs operations in the background. If the Service interacts with the front-end UI, you can send a broadcast or notification bar.

3. lifecycle:

(1) The overall life time of the service starts when onCreate () is called and ends when the onDestroy () method is returned. Like activity, the service initializes it in onCreate () and releases residual resources in onDestroy.

(2). ** startService () method: ** onCreate ()-> onStartCommand ()-> onStart ()-> onDestroy ()

(3). ** BindService () method: ** onCreate ()-> onBinder ()-> onUnbind ()-> onDestroy (). The onUnbind () method returns and ends.

4. Startup Mode:

(1) the Service itself cannot run. It needs to be called through an Activity or other Context object.

(2) There are two ways to start the Service:

Context. startService () and Context. bindService () start the Service. If there are time-consuming operations in the onCreate () method or onStart () method of the service, you need to open a new thread. You can start a service in the preceding two ways.

Note:

Generally, the startService method is used to place time-consuming tasks in the background for processing. After the processing is completed, you can use the broadcast or notification bar to notify the foreground.

5. The following code is used for in-depth understanding:

(1). MainActivity. java class:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

Package com. example. servicetest;

 

Import com. example. servicetest. service. MyService;

 

Import android. app. Activity;

Import android. content. ComponentName;

Import android. content. Intent;

Import android. content. ServiceConnection;

Import android. OS. Bundle;

Import android. OS. IBinder;

Import android. util. Log;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. widget. Button;

 

Public class MainActivity extends Activity implements OnClickListener {

/** Flag space */

Private static String TAG = "com. example. servicetest. MainActivity ";

/** Start the service */

Private Button mBtnStart;

/** Bind a service */

Private Button mBtnBind;

 

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. activity_main );

InitView ();

}

 

/**

* Init the View

*/

Private void initView (){

MBtnStart = (Button) findViewById (R. id. startservice );

MBtnBind = (Button) findViewById (R. id. bindservice );

MBtnStart. setOnClickListener (this );

MBtnBind. setOnClickListener (this );

 

}

 

@ Override

Public void onClick (View view ){

Switch (view. getId ()){

// Method of starting the service

Case R. id. startservice:

StartService (new Intent (MyService. ACTION ));

Break;

// Service binding method

Case R. id. bindservice:

BindService (new Intent (MyService. ACTION), conn, BIND_AUTO_CREATE );

 

Break;

 

Default:

 

Break;

}

 

}

 

ServiceConnection conn = new ServiceConnection (){

Public void onServiceConnected (ComponentName name, IBinder service ){

Log. v (TAG, "onServiceConnected ");

}

 

Public void onServiceDisconnected (ComponentName name ){

Log. v (TAG, "onServiceDisconnected ");

}

};

 

@ Override

Protected void onDestroy (){

Super. onDestroy ();

System. out. println ("------- onDestroy ()--");

StopService (new Intent (MyService. ACTION ));

UnbindService (conn );

}

}

(2). MyService. java class:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

Package com. example. servicetest. service;

 

Import android. app. Service;

Import android. content. Intent;

Import android. OS. Binder;

Import android. OS. IBinder;

Import android. util. Log;

 

Public class MyService extends Service {

/** Flag space */

Private static String TAG = "com. example. servicetest. service. MyService ";

/** Behavior */

Public static final String ACTION = "com. example. servicetest. service. MyService ";

 

@ Override

Public void onCreate (){

Super. onCreate ();

 

System. out. println ("----- onCreate ()---");

 

}

 

@ Override

Public void onStart (Intent intent, int startId ){

Super. onStart (intent, startId );

System. out. println ("----- onStart ()---");

}

 

@ Override

Public int onStartCommand (Intent intent, int flags, int startId ){

System. out. println ("----- onStartCommand ()---");

Return super. onStartCommand (intent, flags, startId );

}

 

@ Override

Public IBinder onBind (Intent arg0 ){

System. out. println ("----- onBind ()---");

Return null;

}

 

 

@ Override

Public void onRebind (Intent intent ){

System. out. println ("----- onRebind ()---");

Super. onRebind (intent );

}

 

@ Override

Public boolean onUnbind (Intent intent ){

System. out. println ("----- onUnbind ()---");

Return super. onUnbind (intent );

}

 

@ Override

Public void onDestroy (){

System. out. println ("----- onDestroy ()---");

Super. onDestroy ();

}

 

}

(3). AndroidManifest. xml

?

1

2

3

4

5

6

7

8

9

10

<! -- Register -->

<Service android: name = "com. example. servicetest. service. MyService">

<Intent-filter>

 

<! -- Intent used to start the service -->

<Action android: name = "com. example. servicetest. service. MyService"/>

 

<Category android: name = "android. intent. category. default"/>

</Intent-filter>

</Service>

StartService method:

(1) When you press the startService button:

(2) When you continue to press the startService button:

BindService method:

(1) When you press the bindService button:

(2) When you continue to press the bindService button:

(3) First press startService and then bindService:

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.