后台的幽灵 - Service


本节内容涉及到
一 什么是Service
如何使用Service
Service的生命周期

一 什么是Service

Service,看名字就知道跟正常理解的“服务”差不多,后台运行,可交互这样的一个东西。它跟Activity的级别差不多,但是他不能自己运行,需要通过某一个Activity或者其他Context对象来调用, Context.startService() 和 Context.bindService()。

两种启动Service的方式有所不同。这里要说明一下的是如果你在Service的onCreate或者onStart做一些很耗时间的事情,最好在 Service里启动一个线程来完成,因为Service是跑在主线程中,会影响到你的UI操作或者阻塞主线程中的其他事情。

什么时候需要Service呢?比如播放多媒体的时候用户启动了其他Activity这个时候程序要在后台继续播放,比如检测SD卡上文件的变化,再或者在后台记录你地理信息位置的改变等等,总之服务嘛,总是藏在后头的。

 

如何使用Service

那接下来用代码来说明一下怎么使用Service,这里我们要讲的是Local Service也就是你自己的一个Service, 你也可以操作别的应用程序的service如果它允许你那么去做的话,这就设计到一个比较麻烦的东西interprocess communication (IPC),在不同的进程中通信的机制,这个我自己也还没有用过,等用了以后再跟大伙说说,通常情况下Local的就够用啦。

跟Activity一样首先你要写一个类继承自android.app.Service,在这里我叫他TestService
代码如下:

Java代码
  1. package  com.haric.tutorial;  
  2.   
  3. import  android.app.Notification;  
  4. import  android.app.NotificationManager;  
  5. import  android.app.PendingIntent;  
  6. import  android.app.Service;  
  7. import  android.content.Intent;  
  8. import  android.os.Binder;  
  9. import  android.os.IBinder;  
  10. import  android.util.Log;  
  11.   
  12. public   class  TestService  extends  Service {  
  13.     private   static   final  String TAG =  "TestService" ;  
  14.     private  NotificationManager _nm;  
  15.   
  16.     @Override   
  17.     public  IBinder onBind(Intent i) {  
  18.         Log.e(TAG, "============> TestService.onBind" );  
  19.         return   null ;  
  20.     }  
  21.   
  22.     public   class  LocalBinder  extends  Binder {  
  23.         TestService getService() {  
  24.             return  TestService. this ;  
  25.         }  
  26.     }  
  27.   
  28.     @Override   
  29.     public   boolean  onUnbind(Intent i) {  
  30.         Log.e(TAG, "============> TestService.onUnbind" );  
  31.         return   false ;  
  32.     }  
  33.   
  34.     @Override   
  35.     public   void  onRebind(Intent i) {  
  36.         Log.e(TAG, "============> TestService.onRebind" );  
  37.     }  
  38.   
  39.     @Override   
  40.     public   void  onCreate() {  
  41.         Log.e(TAG, "============> TestService.onCreate" );  
  42.         _nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  43.         showNotification();  
  44.     }  
  45.   
  46.     @Override   
  47.     public   void  onStart(Intent intent,  int  startId) {  
  48.         Log.e(TAG, "============> TestService.onStart" );  
  49.     }  
  50.   
  51.     @Override   
  52.     public   void  onDestroy() {  
  53.         _nm.cancel(R.string.service_started);  
  54.         Log.e(TAG, "============> TestService.onDestroy" );  
  55.     }  
  56.   
  57.     private   void  showNotification() {  
  58.         Notification notification = new  Notification(R.drawable.face_1,  
  59.                 "Service started" , System.currentTimeMillis());  
  60.   
  61.         PendingIntent contentIntent = PendingIntent.getActivity(this0 ,  
  62.                 new  Intent( this , TestServiceHolder. class ),  0 );  
  63.   
  64.         // must set this for content view, or will throw a exception   
  65.         notification.setLatestEventInfo(this"Test Service" ,  
  66.                 "Service started" , contentIntent);  
  67.   
  68.         _nm.notify(R.string.service_started, notification);  
  69.     }  
  70. }  
package com.haric.tutorial;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class TestService extends Service {
	private static final String TAG = "TestService";
	private NotificationManager _nm;

	@Override
	public IBinder onBind(Intent i) {
		Log.e(TAG, "============> TestService.onBind");
		return null;
	}

	public class LocalBinder extends Binder {
		TestService getService() {
			return TestService.this;
		}
	}

	@Override
	public boolean onUnbind(Intent i) {
		Log.e(TAG, "============> TestService.onUnbind");
		return false;
	}

	@Override
	public void onRebind(Intent i) {
		Log.e(TAG, "============> TestService.onRebind");
	}

	@Override
	public void onCreate() {
		Log.e(TAG, "============> TestService.onCreate");
		_nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		showNotification();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.e(TAG, "============> TestService.onStart");
	}

	@Override
	public void onDestroy() {
		_nm.cancel(R.string.service_started);
		Log.e(TAG, "============> TestService.onDestroy");
	}

	private void showNotification() {
		Notification notification = new Notification(R.drawable.face_1,
				"Service started", System.currentTimeMillis());

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, TestServiceHolder.class), 0);

		// must set this for content view, or will throw a exception
		notification.setLatestEventInfo(this, "Test Service",
				"Service started", contentIntent);

		_nm.notify(R.string.service_started, notification);
	}
}

 其中用到Notification是为了明显地表明Service存活的状态,跟demo的code学过来的,这样看上去直观一点,更多关于Notification的内容以后UI部分来写吧,现在就知道怎么使用就好了。

Java代码
  1. @Override   
  2.     public   void  onCreate() {  
  3.         Log.e(TAG, "============> TestService.onCreate" );  
  4.         _nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  5.         showNotification();  
  6.     }  
@Override
	public void onCreate() {
		Log.e(TAG, "============> TestService.onCreate");
		_nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		showNotification();
	}

像这样,我在Service的几个生命周期函数中加了打印log的语句,方便测试。

 

Java代码
  1. public   class  LocalBinder  extends  Binder {  
  2.         TestService getService() {  
  3.             return  TestService. this ;  
  4.         }  
  5.     }  
public class LocalBinder extends Binder {
		TestService getService() {
			return TestService.this;
		}
	}

这个方法是为了让调用者得到这个Service并操作它。
Service本身就这样简单了,你需要做什么就在onCreate和onStart里做好了,起个线程什么的。

再看一下它的调用者,TestServiceHolder

Java代码
  1. package  com.haric.tutorial;  
  2.   
  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.view.View.OnClickListener;  
  12. import  android.widget.Button;  
  13. import  android.widget.Toast;  
  14.   
  15. public   class  TestServiceHolder  extends  Activity {  
  16.     private   boolean  _isBound;  
  17.     private  TestService _boundService;  
  18.   
  19.     public   void  onCreate(Bundle savedInstanceState) {  
  20.         super .onCreate(savedInstanceState);  
  21.         setContentView(R.layout.test_service_holder);  
  22.         setTitle("Service Test" );  
  23.   
  24.         initButtons();  
  25.     }  
  26.       
  27.     private  ServiceConnection _connection =  new  ServiceConnection() {  
  28.         public   void  onServiceConnected(ComponentName className, IBinder service) {  
  29.             _boundService = ((TestService.LocalBinder)service).getService();  
  30.               
  31.             Toast.makeText(TestServiceHolder.this"Service connected" ,  
  32.                     Toast.LENGTH_SHORT).show();  
  33.         }  
  34.   
  35.         public   void  onServiceDisconnected(ComponentName className) {  
  36.             // unexpectedly disconnected,we should never see this happen.   
  37.             _boundService = null ;  
  38.             Toast.makeText(TestServiceHolder.this"Service connected" ,  
  39.                     Toast.LENGTH_SHORT).show();  
  40.         }  
  41.     };  
  42.   
  43.     private   void  initButtons() {  
  44.         Button buttonStart = (Button) findViewById(R.id.start_service);  
  45.         buttonStart.setOnClickListener(new  OnClickListener() {  
  46.             public   void  onClick(View arg0) {  
  47.                 startService();  
  48.             }  
  49.         });  
  50.   
  51.         Button buttonStop = (Button) findViewById(R.id.stop_service);  
  52.         buttonStop.setOnClickListener(new  OnClickListener() {  
  53.             public   void  onClick(View arg0) {  
  54.                 stopService();  
  55.             }  
  56.         });  
  57.   
  58.         Button buttonBind = (Button) findViewById(R.id.bind_service);  
  59.         buttonBind.setOnClickListener(new  OnClickListener() {  
  60.             public   void  onClick(View arg0) {  
  61.                 bindService();  
  62.             }  
  63.         });  
  64.   
  65.         Button buttonUnbind = (Button) findViewById(R.id.unbind_service);  
  66.         buttonUnbind.setOnClickListener(new  OnClickListener() {  
  67.             public   void  onClick(View arg0) {  
  68.                 unbindService();  
  69.             }  
  70.         });  
  71.     }  
  72.   
  73.     private   void  startService() {  
  74.         Intent i = new  Intent( this , TestService. class );  
  75.         this .startService(i);  
  76.     }  
  77.   
  78.     private   void  stopService() {  
  79.         Intent i = new  Intent( this , TestService. class );  
  80.         this .stopService(i);  
  81.     }  
  82.   
  83.     private   void  bindService() {  
  84.         Intent i = new  Intent( this , TestService. class );  
  85.          bindService(i, _connection, Context.BIND_AUTO_CREATE);  
  86.          _isBound = true ;  
  87.     }  
  88.   
  89.     private   void  unbindService() {  
  90.         if  (_isBound) {  
  91.             unbindService(_connection);  
  92.             _isBound = false ;  
  93.         }  
  94.     }  
  95. }  
package com.haric.tutorial;

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TestServiceHolder extends Activity {
	private boolean _isBound;
	private TestService _boundService;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_service_holder);
		setTitle("Service Test");

		initButtons();
	}
	
	private ServiceConnection _connection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
        	_boundService = ((TestService.LocalBinder)service).getService();
            
            Toast.makeText(TestServiceHolder.this, "Service connected",
                    Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName className) {
            // unexpectedly disconnected,we should never see this happen.
        	_boundService = null;
            Toast.makeText(TestServiceHolder.this, "Service connected",
                    Toast.LENGTH_SHORT).show();
        }
    };

	private void initButtons() {
		Button buttonStart = (Button) findViewById(R.id.start_service);
		buttonStart.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				startService();
			}
		});

		Button buttonStop = (Button) findViewById(R.id.stop_service);
		buttonStop.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				stopService();
			}
		});

		Button buttonBind = (Button) findViewById(R.id.bind_service);
		buttonBind.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				bindService();
			}
		});

		Button buttonUnbind = (Button) findViewById(R.id.unbind_service);
		buttonUnbind.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				unbindService();
			}
		});
	}

	private void startService() {
		Intent i = new Intent(this, TestService.class);
		this.startService(i);
	}

	private void stopService() {
		Intent i = new Intent(this, TestService.class);
		this.stopService(i);
	}

	private void bindService() {
		Intent i = new Intent(this, TestService.class);
		 bindService(i, _connection, Context.BIND_AUTO_CREATE);
		 _isBound = true;
	}

	private void unbindService() {
		if (_isBound) {
            unbindService(_connection);
            _isBound = false;
        }
	}
}

 

这里可以看到两种启动方法,start和bind,当然也是通过intent调用的,在intent中指明指定要启动的Service的名字,stop也一样

Java代码
  1. private   void  startService() {  
  2.         Intent i = new  Intent( this , TestService. class );  
  3.         this .startService(i);  
  4.     }  
  5.   
  6. private   void  stopService() {  
  7.         Intent i = new  Intent( this , TestService. class );  
  8.         this .stopService(i);  
  9.     }  
private void startService() {
		Intent i = new Intent(this, TestService.class);
		this.startService(i);
	}

private void stopService() {
		Intent i = new Intent(this, TestService.class);
		this.stopService(i);
	}

对于bind的话,需要一个ServiceConnection对象

Java代码
  1. private  ServiceConnection _connection =  new  ServiceConnection() {  
  2.         public   void  onServiceConnected(ComponentName className, IBinder service) {  
  3.             _boundService = ((TestService.LocalBinder)service).getService();  
  4.               
  5.             Toast.makeText(TestServiceHolder.this"Service connected" ,  
  6.                     Toast.LENGTH_SHORT).show();  
  7.         }  
  8.   
  9.         public   void  onServiceDisconnected(ComponentName className) {  
  10.             // unexpectedly disconnected,we should never see this happen.   
  11.             _boundService = null ;  
  12.             Toast.makeText(TestServiceHolder.this"Service connected" ,  
  13.                     Toast.LENGTH_SHORT).show();  
  14.         }  
  15.     };  
private ServiceConnection _connection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
        	_boundService = ((TestService.LocalBinder)service).getService();
            
            Toast.makeText(TestServiceHolder.this, "Service connected",
                    Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName className) {
            // unexpectedly disconnected,we should never see this happen.
        	_boundService = null;
            Toast.makeText(TestServiceHolder.this, "Service connected",
                    Toast.LENGTH_SHORT).show();
        }
    };

用来把Activity和特定的Service连接在一起,共同存亡,具体的生命周期细节下一段来讲。


Service的生命周期

Service的生命周期方法比Activity少一些,只有onCreate, onStart, onDestroy
我们有两种方式启动一个Service,他们对Service生命周期的影响是不一样的。


1 通过startService

    Service会经历 onCreate -> onStart
   stopService的时候直接onDestroy

   如果是调用者(TestServiceHolder)自己直接退出而没有调用stopService的
   话,Service会一直在后台运行。
   下次TestServiceHolder再起来可以stopService。

 

  2 通过bindService   

    Service只会运行onCreate, 这个时候 TestServiceHolder 和TestService绑定在一起

   TestServiceHolder 退出了,Srevice就会调用onUnbind->onDestroyed
   所谓绑定在一起就共存亡了。

 

那有同学问了,要是这几个方法交织在一起的话,会出现什么情况呢?
一个原则是Service的onCreate的方法只会被调用一次,就是你无论多少次的startService又 bindService,Service只被创建一次。如果先是bind了,那么start的时候就直接运行Service的onStart方法,如果先 是start,那么bind的时候就直接运行onBind方法。如果你先bind上了,就stop不掉了,对啊,就是stopService不好使了,只 能先UnbindService, 再StopService,所以是先start还是先bind行为是有区别的。

看起来情况很多,不过我会把这次的代码包括上回Activity生命周期的研究代码都贴上了,希望你喜欢!大家有兴趣可以回去点点按钮看看log,多看几遍就知道了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值