Android You之Service理解


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.   
  13.   
  14. public class ServiceTestActivity extends Activity {  
  15.     //declare the buttons   
  16.     private Button btnStartService,btnStopService,btnBindService,btnUnbindService;  
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         //load the layout configuration from xml file   
  22.         setContentView(R.layout.main);  
  23.         //initialize necessary views   
  24.         initializeViews();  
  25.     }  
  26.       
  27.     /* 
  28.      * initialize necessary views 
  29.      */  
  30.     private void initializeViews(){  
  31.         initializeButtons();  
  32.     }  
  33.       
  34.     /* 
  35.      * initialize necessary buttons 
  36.      */  
  37.     private void initializeButtons(){  
  38.         //the button that will start a service   
  39.         this.btnStartService=(Button)findViewById(R.id.btnStartService);  
  40.         this.btnStartService.setOnClickListener(new BtnStartServiceOnClickListener());  
  41.         //the button that will stop a service   
  42.         this.btnStopService=(Button)findViewById(R.id.btnStopService);  
  43.         this.btnStopService.setOnClickListener(new BtnStopServiceOnClickListener());  
  44.         //the button that will bind a service   
  45.         this.btnBindService=(Button)findViewById(R.id.btnBindService);  
  46.         this.btnBindService.setOnClickListener(new BtnBindServiceOnClickListener());  
  47.         //the button that will unbind service   
  48.         this.btnUnbindService=(Button)findViewById(R.id.btnUnbindService);  
  49.         this.btnUnbindService.setOnClickListener(new BtnUnbindServiceOnClickListener());  
  50.     }  
  51.       
  52.     /* 
  53.      * onClickListener for btnStartService.aimed to start a service 
  54.      */  
  55.     class BtnStartServiceOnClickListener implements View.OnClickListener{  
  56.         @Override  
  57.         public void onClick(View view){  
  58.             Intent intent=new Intent(ServiceTestActivity.this,ServiceForTest.class);  
  59.             startService(intent);  
  60.         }  
  61.     }  
  62.   
  63.   
  64.       
  65.     /* 
  66.      * onClickListener for btnStopService .aimed to stop a service 
  67.      */  
  68.     class BtnStopServiceOnClickListener implements View.OnClickListener{  
  69.         @Override  
  70.         public void onClick(View view){  
  71.             Intent intent=new Intent(ServiceTestActivity.this,ServiceForTest.class);  
  72.             stopService(intent);  
  73.         }  
  74.     }  
  75.       
  76.     /* 
  77.      * onClickListener for btnBindService .aimed to bind a service 
  78.      */  
  79.     class BtnBindServiceOnClickListener implements View.OnClickListener{  
  80.         @Override  
  81.         public void onClick(View view){  
  82.             Intent intent=new Intent(ServiceTestActivity.this,ServiceForTest.class);  
  83.             bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);  
  84.         }  
  85.     }  
  86.       
  87.     /* 
  88.      * onClickListener for btnUnbindService .aimed to unbind a service 
  89.      */  
  90.     class BtnUnbindServiceOnClickListener implements View.OnClickListener{  
  91.         @Override  
  92.         public void onClick(View view){  
  93.             unbindService(serviceConnection);  
  94.         }  
  95.     }  
  96.       
  97.     /* 
  98.      * serviceConnection for bindService(as one of parameters) 
  99.      */  
  100.     private ServiceConnection serviceConnection=new ServiceConnection(){  
  101.   
  102.   
  103.         @Override  
  104.         public void onServiceConnected(ComponentName name, IBinder service) {  
  105.             // TODO Auto-generated method stub   
  106.               
  107.         }  
  108.   
  109.   
  110.         @Override  
  111.         public void onServiceDisconnected(ComponentName name) {  
  112.             // TODO Auto-generated method stub   
  113.               
  114.         }  
  115.           
  116.     };  
  117. }  
  • 1
  • 2
  • 下一页

相关内容