如果把Activity比喻为前台程序,那么Service就是后台程序,Service的整个生命周期都只会在后台执行。Service跟Activity一样也由Intent调用。在工程里想要添加一个Service,先新建继承Service的类,然后到AndroidManifest.xml -> Application ->Application Nodes中的Service标签中添加。
Service要由Activity通过startService 或者 bindService来启动,Intent负责传递参数。
startService与bindService都可以启动Service,那么它们之间有什么区别呢?它们两者的区别就是使Service的周期改变。由startService启动的Service必须要有stopService来结束Service,不调用stopService则会造成Activity结束了而Service还运行着。bindService启动的Service可以由unbindService来结束,也可以在Activity结束之后(onDestroy)自动结束。
布局文件的代码:
01.<?xml version="1.0" encoding="utf-8"?> 02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 03. android:orientation="vertical" android:layout_width="fill_parent" 04. android:layout_height="fill_parent"> 05. <Button android:layout_width="wrap_content" 06. android:layout_height="wrap_content" android:id="@+id/btnStartMyService" 07. android:text="StartMyService"></Button> 08. <Button android:layout_width="wrap_content" 09. android:layout_height="wrap_content" android:id="@+id/btnStopMyService" 10. android:text="StopMyService"></Button> 11. <Button android:layout_width="wrap_content" 12. android:layout_height="wrap_content" android:id="@+id/btnBindMyService" 13. android:text="BindMyService"></Button> 14. <Button android:layout_width="wrap_content" 15. android:layout_height="wrap_content" android:id="@+id/btnUnbindMyService" 16. android:text="UnbindMyService"></Button> 17. <Button android:layout_width="wrap_content" 18. android:layout_height="wrap_content" android:id="@+id/btnExit" 19. android:text="退出程序"></Button> 20.</LinearLayout>
testService.java的源码:
import android.app.Activity; 04.import android.app.Service; 05.import android.content.ComponentName; 06.import android.content.Intent; 07.import android.content.ServiceConnection; 08.import android.os.Bundle; 09.import android.os.IBinder; 10.import android.util.Log; 11.import android.view.View; 12.import android.widget.Button; 13. 14.public class testService extends Activity { 15. Button btnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit; 16. @Override 17. public void onCreate(Bundle savedInstanceState) { 18. super.onCreate(savedInstanceState); 19. setContentView(R.layout.main); 20. btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService); 21. btnStartMyService.setOnClickListener(new ClickEvent()); 22. 23. btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService); 24. btnStopMyService.setOnClickListener(new ClickEvent()); 25. 26. btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService); 27. btnBindMyService.setOnClickListener(new ClickEvent()); 28. 29. btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService); 30. btnUnbindMyService.setOnClickListener(new ClickEvent()); 31. 32. btnExit=(Button)this.findViewById(R.id.btnExit); 33. btnExit.setOnClickListener(new ClickEvent()); 34. } 35. @Override 36. public void onDestroy() 37. { 38. super.onDestroy(); 39. Log.e("Activity","onDestroy"); 40. } 41. 42. private ServiceConnection _connection = new ServiceConnection() { 43. @Override 44. public void onServiceConnected(ComponentName arg0, IBinder arg1) { 45. // TODO Auto-generated method stub 46. } 47. 48. @Override 49. public void onServiceDisconnected(ComponentName name) { 50. // TODO Auto-generated method stub 51. } 52. }; 53. class ClickEvent implements View.OnClickListener{ 54. 55. @Override 56. public void onClick(View v) { 57. Intent intent=new Intent(testService.this,MyService.class); 58. if(v==btnStartMyService){ 59. testService.this.startService(intent); 60. } 61. else if(v==btnStopMyService){ 62. testService.this.stopService(intent); 63. } 64. else if(v==btnBindMyService){ 65. testService.this.bindService(intent, _connection, Service.BIND_AUTO_CREATE); 66. } 67. else if(v==btnUnbindMyService){ 68. if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑 69. testService.this.unbindService(_connection); 70. } 71. else if(v==btnExit) 72. { 73. testService.this.finish(); 74. } 75. } . } }
MyService.java的源码:
import android.app.Service;
04.import android.content.Intent;
05.import android.os.IBinder;
06.import android.util.Log;
07.
08.public class MyService extends Service {
09. static public String ServiceState="";
10. @Override
11. public IBinder onBind(Intent arg0) {
12. Log.e("Service", "onBind");
13. ServiceState="onBind";
14. return null;
15. }
16. @Override
17. public boolean onUnbind(Intent intent){
18. super.onUnbind(intent);
19. Log.e("Service", "onUnbind");
20. ServiceState="onUnbind";
21. return false;
22.
23. }
24. @Override
25. public void onCreate(){
26. super.onCreate();
27. Log.e("Service", "onCreate");
28. ServiceState="onCreate";
29. }
30. @Override
31. public void onDestroy(){
32. super.onDestroy();
33. Log.e("Service", "onDestroy");
34. ServiceState="onDestroy";
35. }
36. @Override
37. public void onStart(Intent intent,int startid){
38. super.onStart(intent, startid);
39. Log.e("Service", "onStart");
40. ServiceState="onStart";
41. }
42.
43.}
本文详细介绍了Android中的Service组件,包括如何创建、启动及停止Service。对比了startService与bindService的区别,并提供了具体的代码示例。
5997

被折叠的 条评论
为什么被折叠?



