前面文章介绍了activity以及intent的使用,本文就来介绍service。如果把activity比喻为前台程序,那么service就是后台程序,service的整个生命周期都只会在后台执行。service跟activity一样也由intent调用。在工程里想要添加一个service,先新建继承service的类,然后到androidmanifest.xml -> application ->application nodes中的service标签中添加。
service要由activity通过startservice 或者 bindservice来启动,intent负责传递参数。
先贴出本文程序运行截图如下:
本文主要讲解service的调用,以及其生命周期。
上图是startservice之后再stopservice的service状态变化。
上图是bindservice之后再unbindservice的service状态变化。
startservice与bindservice都可以启动service,那么它们之间有什么区别呢?它们两者的区别就是使service的周期改变。由startservice启动的service必须要有stopservice来结束service,不调用stopservice则会造成activity结束了而service还运行着。bindservice启动的service可以由unbindservice来结束,也可以在activity结束之后(ondestroy)自动结束。
上图是startservice之后再activity.finish()的service状态变化,service还在跑着。
上图是bindservice之后再activity.finish()的service状态变化,service最后自动unbindservice。
main.xml代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/btnstartmyservice" android:text= "startmyservice" ></button> <button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/btnstopmyservice" android:text= "stopmyservice" ></button> <button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/btnbindmyservice" android:text= "bindmyservice" ></button> <button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/btnunbindmyservice" android:text= "unbindmyservice" ></button> <button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/btnexit" android:text= "退出程序" ></button> </linearlayout> |
testservice.java的源码如下:
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.testservice; import android.app.activity; import android.app.service; 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.widget.button; public class testservice extends activity { button btnstartmyservice,btnstopmyservice,btnbindmyservice,btnunbindmyservice,btnexit; @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); btnstartmyservice=(button) this .findviewbyid(r.id.btnstartmyservice); btnstartmyservice.setonclicklistener( new clickevent()); btnstopmyservice=(button) this .findviewbyid(r.id.btnstopmyservice); btnstopmyservice.setonclicklistener( new clickevent()); btnbindmyservice=(button) this .findviewbyid(r.id.btnbindmyservice); btnbindmyservice.setonclicklistener( new clickevent()); btnunbindmyservice=(button) this .findviewbyid(r.id.btnunbindmyservice); btnunbindmyservice.setonclicklistener( new clickevent()); btnexit=(button) this .findviewbyid(r.id.btnexit); btnexit.setonclicklistener( new clickevent()); } @override public void ondestroy() { super .ondestroy(); log.e( "activity" , "ondestroy" ); } private serviceconnection _connection = new serviceconnection() { @override public void onserviceconnected(componentname arg0, ibinder arg1) { // todo auto-generated method stub } @override public void onservicedisconnected(componentname name) { // todo auto-generated method stub } }; class clickevent implements view.onclicklistener{ @override public void onclick(view v) { intent intent= new intent(testservice. this ,myservice. class ); if (v==btnstartmyservice){ testservice. this .startservice(intent); } else if (v==btnstopmyservice){ testservice. this .stopservice(intent); } else if (v==btnbindmyservice){ testservice. this .bindservice(intent, _connection, service.bind_auto_create); } else if (v==btnunbindmyservice){ if (myservice.servicestate== "onbind" ) //service绑定了之后才能解绑 testservice. this .unbindservice(_connection); } else if (v==btnexit) { testservice. this .finish(); } } } } |
myservice.java的源码如下:
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
|
package com.testservice; import android.app.service; import android.content.intent; import android.os.ibinder; import android.util.log; public class myservice extends service { static public string servicestate= "" ; @override public ibinder onbind(intent arg0) { log.e( "service" , "onbind" ); servicestate= "onbind" ; return null ; } @override public boolean onunbind(intent intent){ super .onunbind(intent); log.e( "service" , "onunbind" ); servicestate= "onunbind" ; return false ; } @override public void oncreate(){ super .oncreate(); log.e( "service" , "oncreate" ); servicestate= "oncreate" ; } @override public void ondestroy(){ super .ondestroy(); log.e( "service" , "ondestroy" ); servicestate= "ondestroy" ; } @override public void onstart(intent intent, int startid){ super .onstart(intent, startid); log.e( "service" , "onstart" ); servicestate= "onstart" ; } } |