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程序源碼:
- package ps.androidyue.servicetest;
- //import the necessary packages
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.view.View;
- import android.widget.Button;
-
-
- public class ServiceTestActivity extends Activity {
- //declare the buttons
- private Button btnStartService,btnStopService,btnBindService,btnUnbindService;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //load the layout configuration from xml file
- setContentView(R.layout.main);
- //initialize necessary views
- initializeViews();
- }
-
- /*
- * initialize necessary views
- */
- private void initializeViews(){
- initializeButtons();
- }
-
- /*
- * initialize necessary buttons
- */
- private void initializeButtons(){
- //the button that will start a service
- this.btnStartService=(Button)findViewById(R.id.btnStartService);
- this.btnStartService.setOnClickListener(new BtnStartServiceOnClickListener());
- //the button that will stop a service
- this.btnStopService=(Button)findViewById(R.id.btnStopService);
- this.btnStopService.setOnClickListener(new BtnStopServiceOnClickListener());
- //the button that will bind a service
- this.btnBindService=(Button)findViewById(R.id.btnBindService);
- this.btnBindService.setOnClickListener(new BtnBindServiceOnClickListener());
- //the button that will unbind service
- this.btnUnbindService=(Button)findViewById(R.id.btnUnbindService);
- this.btnUnbindService.setOnClickListener(new BtnUnbindServiceOnClickListener());
- }
-
- /*
- * onClickListener for btnStartService.aimed to start a service
- */
- class BtnStartServiceOnClickListener implements View.OnClickListener{
- @Override
- public void onClick(View view){
- Intent intent=new Intent(ServiceTestActivity.this,ServiceForTest.class);
- startService(intent);
- }
- }
-
-
-
- /*
- * onClickListener for btnStopService .aimed to stop a service
- */
- class BtnStopServiceOnClickListener implements View.OnClickListener{
- @Override
- public void onClick(View view){
- Intent intent=new Intent(ServiceTestActivity.this,ServiceForTest.class);
- stopService(intent);
- }
- }
-
- /*
- * onClickListener for btnBindService .aimed to bind a service
- */
- class BtnBindServiceOnClickListener implements View.OnClickListener{
- @Override
- public void onClick(View view){
- Intent intent=new Intent(ServiceTestActivity.this,ServiceForTest.class);
- bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
- }
- }
-
- /*
- * onClickListener for btnUnbindService .aimed to unbind a service
- */
- class BtnUnbindServiceOnClickListener implements View.OnClickListener{
- @Override
- public void onClick(View view){
- unbindService(serviceConnection);
- }
- }
-
- /*
- * serviceConnection for bindService(as one of parameters)
- */
- private ServiceConnection serviceConnection=new ServiceConnection(){
-
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- // TODO Auto-generated method stub
-
- }
-
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- // TODO Auto-generated method stub
-
- }
-
- };
- }