Service Components in Android

Source: Internet
Author: User
Serivce components:

First, you must inherit from Serivce to implement the methods in its life cycle. Then, you must declare the <service> In AndroidMainfest. xml before using it.

You can call startService to start a Service, bindService to bind an existing Service, and RPC (Remote process call) to call services between different processes.

Life cycle: onBind, a required method, returns a binding interface to the service

OnCreate: when a Service is created for the first time, it is called by the system.

OnStart (). This method is called when serivce is started using the startService () method.

OnDestory this method is called when Serivce is no longer used

 

To use a Service, you must declare serivce in the AndroidMainfest. xml file <service> and add <intent-filter> to the <service> element to specify how to access the Service.

 

 

Note: The Life Cycle of calling startService is onCreate ----------- à onStart.

The life cycle of calling bindService is onCreate ------------- à onBind.

 

 

Package com. king. android. controls;
2
3 import android. app. Service;
4 import android. content. Intent;
5 import android. OS. IBinder;
6 import android. widget. Toast;
7
8 /**
9
10 * Description: Demonstration of the first service instance
11 * Author: Andy. Liu
12 * Time: 07:21:48
13 **/
14 public class FirstService extends Service {
15
16 private static final String MY_SERVICE = "com. king. android. controls. my_service"; // The action specified by the Service
17
18 @ Override
19 public IBinder onBind (Intent arg0 ){
20
21 Toast. makeText (FirstService. this, "onBind ......", Toast. LENGTH_LONG). show ();
22 return null;
23}
24 @ Override
25 public void onCreate (){
26 Toast. makeText (FirstService. this, "onCreate...", Toast. LENGTH_LONG). show ();
27 super. onCreate ();
28}
29
30 @ Override
31 public void onStart (Intent intent, int startId ){
32 Toast. makeText (FirstService. this, "onStart ......", Toast. LENGTH_LONG). show ();
33 super. onStart (intent, startId );
34}
35
36 @ Override
37 public void onDestroy (){
38 Toast. makeText (FirstService. this, "OnDestory...", Toast. LENGTH_LONG). show ();
39 super. onDestroy ();
40}
41
42}
43
44
45
46
47
48 package com. king. android. controls;
49
50 import android. app. Activity;
51 import android. app. Service;
52 import android. content. ComponentName;
53 import android. content. Intent;
54 import android. content. ServiceConnection;
55 import android. OS. Bundle;
56 import android. OS. IBinder;
57 import android. view. View;
58 import android. view. View. OnClickListener;
59 import android. widget. Button;
60 import android. widget. Toast;
61
62 import com. king. android. R;
63
64 /**
65
66 * Description: demonstrate the Service Activity.
67 * Author: Andy. Liu
68 * Time: 07:27:19
69 **/
70 public class ServiceActivity extends Activity implements OnClickListener {
71 private static final String MY_SERVICE = "com. king. android. controls. my_service"; // action specified by the Service
72 @ Override
73 protected void onCreate (Bundle savedInstanceState ){
74 super. onCreate (savedInstanceState );
75 initView ();
76}
77
78 private void initView (){
79 setContentView (R. layout. show_service_layout );
80 Button btnSerivce = (Button) findViewById (R. id. btn_start );
81 btnSerivce. setOnClickListener (this );
82 btnSerivce = (Button) findViewById (R. id. btn_stop );
83 btnSerivce. setOnClickListener (this );
84 btnSerivce = (Button) findViewById (R. id. btn_bind );
85 btnSerivce. setOnClickListener (this );
86 btnSerivce = (Button) findViewById (R. id. btn_unBind );
87 btnSerivce. setOnClickListener (this );
88}
89
90 @ Override
91 public void onClick (View v ){
92 Intent intent = null;
93 switch (v. getId ()){
94 case R. id. btn_start:
95 intent = new Intent ();
96 intent. setAction (MY_SERVICE );
97 startService (intent );
98 break;
99 case R. id. btn_stop:
100 intent = new Intent ();
101 intent. setAction (MY_SERVICE );
102 stopService (intent );
103 break;
104 case R. id. btn_bind:
105 intent = new Intent ();
106 intent. setAction (MY_SERVICE );
107 bindService (intent, conn, Service. BIND_AUTO_CREATE );
108 break;
109 case R. id. btn_unBind:
110 intent = new Intent ();
111 intent. setAction (MY_SERVICE );
112 unbindService (conn );
113 break;
114}
115}
116
117 private ServiceConnection conn = new ServiceConnection (){
118
119 @ Override
120 public void onServiceDisconnected (ComponentName name ){
121 Toast. makeText (ServiceActivity. this, "connection successful", Toast. LENGTH_LONG). show ();
122}
123
124 @ Override
125 public void onServiceConnected (ComponentName name, IBinder service ){
126 Toast. makeText (ServiceActivity. this, "Disconnect", Toast. LENGTH_LONG). show ();
127}
128 };
129
130}

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.