Android提高第五篇之Service

本文来自http://blog.csdn.net/hellogv/,引用必须注明出处!

上次介绍了Activity以及Intent的使用,这次就介绍Service,如果把Activity比喻为前台程序,那么Service就是后台程序,Service的整个生命周期都只会在后台执行。Service跟Activity一样也由Intent调用。在工程里想要添加一个Service,先新建继承Service的类,然后到AndroidManifest.xml -> Application ->Application Nodes中的Service标签中添加。

Service要由Activity通过startService 或者 bindService来启动,Intent负责传递参数。先贴出本文程序运行截图:

Android提高第五篇之Service

本文主要讲解Service的调用,以及其生命周期。

Android提高第五篇之Service

上图是startService之后再stopService的Service状态变化。

Android提高第五篇之Service

上图是bindService之后再unbindService的Service状态变化。

startService与bindService都可以启动Service,那么它们之间有什么区别呢?它们两者的区别就是使Service的周期改变。由startService启动的Service必须要有stopService来结束Service,不调用stopService则会造成Activity结束了而Service还运行着。bindService启动的Service可以由unbindService来结束,也可以在Activity结束之后(onDestroy)自动结束。

Android提高第五篇之Service

上图是startService之后再Activity.finish()的Service状态变化,Service还在跑着。

Android提高第五篇之Service

上图是bindService之后再Activity.finish()的Service状态变化,Service最后自动unbindService。

main.xml代码:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <Buttonandroid:layout_width="wrap_content"
  6. android:layout_height="wrap_content"android:id="@+id/btnStartMyService"
  7. android:text="StartMyService"></Button>
  8. <Buttonandroid:layout_width="wrap_content"
  9. android:layout_height="wrap_content"android:id="@+id/btnStopMyService"
  10. android:text="StopMyService"></Button>
  11. <Buttonandroid:layout_width="wrap_content"
  12. android:layout_height="wrap_content"android:id="@+id/btnBindMyService"
  13. android:text="BindMyService"></Button>
  14. <Buttonandroid:layout_width="wrap_content"
  15. android:layout_height="wrap_content"android:id="@+id/btnUnbindMyService"
  16. android:text="UnbindMyService"></Button>
  17. <Buttonandroid:layout_width="wrap_content"
  18. android:layout_height="wrap_content"android:id="@+id/btnExit"
  19. android:text="退出程序"></Button>
  20. </LinearLayout>

testService.java的源码:

  1. packagecom.testService;
  2. importandroid.app.Activity;
  3. importandroid.app.Service;
  4. importandroid.content.ComponentName;
  5. importandroid.content.Intent;
  6. importandroid.content.ServiceConnection;
  7. importandroid.os.Bundle;
  8. importandroid.os.IBinder;
  9. importandroid.util.Log;
  10. importandroid.view.View;
  11. importandroid.widget.Button;
  12. publicclasstestServiceextendsActivity{
  13. ButtonbtnStartMyService,btnStopMyService,btnBindMyService,btnUnbindMyService,btnExit;
  14. @Override
  15. publicvoidonCreate(BundlesavedInstanceState){
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.main);
  18. btnStartMyService=(Button)this.findViewById(R.id.btnStartMyService);
  19. btnStartMyService.setOnClickListener(newClickEvent());
  20. btnStopMyService=(Button)this.findViewById(R.id.btnStopMyService);
  21. btnStopMyService.setOnClickListener(newClickEvent());
  22. btnBindMyService=(Button)this.findViewById(R.id.btnBindMyService);
  23. btnBindMyService.setOnClickListener(newClickEvent());
  24. btnUnbindMyService=(Button)this.findViewById(R.id.btnUnbindMyService);
  25. btnUnbindMyService.setOnClickListener(newClickEvent());
  26. btnExit=(Button)this.findViewById(R.id.btnExit);
  27. btnExit.setOnClickListener(newClickEvent());
  28. }
  29. @Override
  30. publicvoidonDestroy()
  31. {
  32. super.onDestroy();
  33. Log.e("Activity","onDestroy");
  34. }
  35. privateServiceConnection_connection=newServiceConnection(){
  36. @Override
  37. publicvoidonServiceConnected(ComponentNamearg0,IBinderarg1){
  38. //TODOAuto-generatedmethodstub
  39. }
  40. @Override
  41. publicvoidonServiceDisconnected(ComponentNamename){
  42. //TODOAuto-generatedmethodstub
  43. }
  44. };
  45. classClickEventimplementsView.OnClickListener{
  46. @Override
  47. publicvoidonClick(Viewv){
  48. Intentintent=newIntent(testService.this,MyService.class);
  49. if(v==btnStartMyService){
  50. testService.this.startService(intent);
  51. }
  52. elseif(v==btnStopMyService){
  53. testService.this.stopService(intent);
  54. }
  55. elseif(v==btnBindMyService){
  56. testService.this.bindService(intent,_connection,Service.BIND_AUTO_CREATE);
  57. }
  58. elseif(v==btnUnbindMyService){
  59. if(MyService.ServiceState=="onBind")//Service绑定了之后才能解绑
  60. testService.this.unbindService(_connection);
  61. }
  62. elseif(v==btnExit)
  63. {
  64. testService.this.finish();
  65. }
  66. }
  67. }
  68. }

MyService.java的源码:

  1. packagecom.testService;
  2. importandroid.app.Service;
  3. importandroid.content.Intent;
  4. importandroid.os.IBinder;
  5. importandroid.util.Log;
  6. publicclassMyServiceextendsService{
  7. staticpublicStringServiceState="";
  8. @Override
  9. publicIBinderonBind(Intentarg0){
  10. Log.e("Service","onBind");
  11. ServiceState="onBind";
  12. returnnull;
  13. }
  14. @Override
  15. publicbooleanonUnbind(Intentintent){
  16. super.onUnbind(intent);
  17. Log.e("Service","onUnbind");
  18. ServiceState="onUnbind";
  19. returnfalse;
  20. }
  21. @Override
  22. publicvoidonCreate(){
  23. super.onCreate();
  24. Log.e("Service","onCreate");
  25. ServiceState="onCreate";
  26. }
  27. @Override
  28. publicvoidonDestroy(){
  29. super.onDestroy();
  30. Log.e("Service","onDestroy");
  31. ServiceState="onDestroy";
  32. }
  33. @Override
  34. publicvoidonStart(Intentintent,intstartid){
  35. super.onStart(intent,startid);
  36. Log.e("Service","onStart");
  37. ServiceState="onStart";
  38. }
  39. }

你可能感兴趣的:(android)