歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android You之Service理解

Android You之Service理解

日期:2017/3/1 10:49:21   编辑:Linux編程

Android You之Service理解

Service中文意思為服務,在android幫助文檔中的解釋為“A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. ”,中文意思大概為:一個服務是應用組成的一部分,它呈現一個程序的意願或者運行一個不需要向用戶交互或者不被其他應用程序所使用的一個長時間運行的操作。

下面了解一下service不是什麼:

1.A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.

service不是一個單獨的進程,service對象本身不能調用使自己運行在自己獨有的進程總,它必須被別的所調用,它運行在應用的進程中,並且作為其中的一部分。

2.A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).

service不是一個線程,它不是一個方法讓自己工作於主線程之外(避免應用程序出現無法響應的錯誤)

每個Service的調用必須在應用程序中的manifest文件中注冊,調用可以使用Context.startService()或者Context.bindService()這兩個方法。

兩種調用方法的區別簡單地總結可以是:

通過startService()方法調用service,service啟動會經歷onCreate->onStart這兩個階段,service停止的時候直接進入銷毀onDestory,如果調用者直接退出(非調用stopService)後,service還將繼續運行,知道調用者再次被啟動,調用stopService,服務才結束。

通過bindService()方法調用service,service啟動只經歷onCreate,這時候調用者就和service綁定在一起了,如果調用者退出,服務自動調用unBind->onDestory,結束服務。

關於如果多個方法交叉調用的情況,符合這樣的結果,如果在一個命令所做的部分已,則完成剩下的結果,但是如果只要調用了onBind,就不能使用stopService結束服務了。這裡留給大家依次列舉可能的情況。

主activity程序源碼:

  1. package ps.androidyue.servicetest;
  2. //import the necessary packages
  3. import android.app.Activity;
  4. import android.content.ComponentName;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.ServiceConnection;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10. import android.view.View;
  11. import android.widget.Button;
  12. public class ServiceTestActivity extends Activity {
  13. //declare the buttons
  14. private Button btnStartService,btnStopService,btnBindService,btnUnbindService;
  15. /** Called when the activity is first created. */
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. //load the layout configuration from xml file
  20. setContentView(R.layout.main);
  21. //initialize necessary views
  22. initializeViews();
  23. }
  24. /*
  25. * initialize necessary views
  26. */
  27. private void initializeViews(){
  28. initializeButtons();
  29. }
  30. /*
  31. * initialize necessary buttons
  32. */
  33. private void initializeButtons(){
  34. //the button that will start a service
  35. this.btnStartService=(Button)findViewById(R.id.btnStartService);
  36. this.btnStartService.setOnClickListener(new BtnStartServiceOnClickListener());
  37. //the button that will stop a service
  38. this.btnStopService=(Button)findViewById(R.id.btnStopService);
  39. this.btnStopService.setOnClickListener(new BtnStopServiceOnClickListener());
  40. //the button that will bind a service
  41. this.btnBindService=(Button)findViewById(R.id.btnBindService);
  42. this.btnBindService.setOnClickListener(new BtnBindServiceOnClickListener());
  43. //the button that will unbind service
  44. this.btnUnbindService=(Button)findViewById(R.id.btnUnbindService);
  45. this.btnUnbindService.setOnClickListener(new BtnUnbindServiceOnClickListener());
  46. }
  47. /*
  48. * onClickListener for btnStartService.aimed to start a service
  49. */
  50. class BtnStartServiceOnClickListener implements View.OnClickListener{
  51. @Override
  52. public void onClick(View view){
  53. Intent intent=new Intent(ServiceTestActivity.this,ServiceForTest.class);
  54. startService(intent);
  55. }
  56. }
  57. /*
  58. * onClickListener for btnStopService .aimed to stop a service
  59. */
  60. class BtnStopServiceOnClickListener implements View.OnClickListener{
  61. @Override
  62. public void onClick(View view){
  63. Intent intent=new Intent(ServiceTestActivity.this,ServiceForTest.class);
  64. stopService(intent);
  65. }
  66. }
  67. /*
  68. * onClickListener for btnBindService .aimed to bind a service
  69. */
  70. class BtnBindServiceOnClickListener implements View.OnClickListener{
  71. @Override
  72. public void onClick(View view){
  73. Intent intent=new Intent(ServiceTestActivity.this,ServiceForTest.class);
  74. bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
  75. }
  76. }
  77. /*
  78. * onClickListener for btnUnbindService .aimed to unbind a service
  79. */
  80. class BtnUnbindServiceOnClickListener implements View.OnClickListener{
  81. @Override
  82. public void onClick(View view){
  83. unbindService(serviceConnection);
  84. }
  85. }
  86. /*
  87. * serviceConnection for bindService(as one of parameters)
  88. */
  89. private ServiceConnection serviceConnection=new ServiceConnection(){
  90. @Override
  91. public void onServiceConnected(ComponentName name, IBinder service) {
  92. // TODO Auto-generated method stub
  93. }
  94. @Override
  95. public void onServiceDisconnected(ComponentName name) {
  96. // TODO Auto-generated method stub
  97. }
  98. };
  99. }
Copyright © Linux教程網 All Rights Reserved