Android应用程序四大组件之Service(一)

What is a Service?

     Service是一个应用程序组件,可以用来处理一些比较耗时的操作.

•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不是一个单独的进程.

•  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不是一个线程.

启动方法

1.Content.startService()

     此启动方式,不会与绑定者没有关系,即使程序退出,servcie仍然运行着.

2.Content.bindService()

    bindService会与绑定者销毁,即程序退出,service就立即停止.

 

Service Lifecycle

两种Service启动方式不同,生命周期也不一样.

1.startService

启动服务(在要与绑定的activity上添加这行代码)
startService(new Intent(ServiceDemoActivity.this, MyService.class));

 MyServcie.class

public class MyService extends Service{
	

	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("onBind()");
		return myBinder;
	}
	
	@Override
	public void onCreate() {
		System.out.println("onCreate()");
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("intent:"+intent+"(flags):"+flags+"startId:"+startId);
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		System.out.println("onDestroy()");
		super.onDestroy();
	}
}

当我们启动Service时,响应的可以看到输出日志onCreate()->onStartCommand();   多次startService并不会启动多个服务,onCreate()在创建时被调用,在Android系统1.x以前是并不是重写onStartCommand()而是onStart().onBind()在此启动方式并不会被调用.  

 //停住服务
(在要与绑定的activity上添加这行代码) 
stopService(new Intent(ServiceDemoActivity.this, MyService.class));


当我们停止Service时,响应的可以看到输出日志onDestroy().

2.bindService

(在要与绑定的activity上添加这行代码)此service与application处于同一进程时情况.通过bindService启动服务.

bindService(new Intent(ServiceDemoActivity.this,MyService.class), serconn, BIND_AUTO_CREATE);
//需在onServiceConnected()从myservice中onBind()返回Ibinder对象,通过该对象取得相应的service实例
   private ServiceConnection serconn = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			System.out.println("onServiceDisconnected");
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			System.out.println("onServiceConnected");
			
			myService = ((MyBinder)service).getService();
			mTextView.setText("get from Service time::"+myService.getSystemTime());
			
		}
	};

 //在service类声明一个内部类继承Binder在该内部类中提供方法返回service实例.//在service类中声明这个内部的实例new MyBinder().以便onBind返回 
public class MyService extends Service{
	
	private MyBinder myBinder = new MyBinder();
	
	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("onBind()");
		return myBinder;
	}
	
	@Override
	public void onCreate() {
		System.out.println("onCreate()");
		super.onCreate();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("intent:"+intent+"(flags):"+flags+"startId:"+startId);
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		System.out.println("onDestroy()");
		super.onDestroy();
	}

	@Override
	public boolean onUnbind(Intent intent) {
		System.out.println("onUnbind()");
		return super.onUnbind(intent);
	}

	@Override
	public void onRebind(Intent intent) {
		System.out.println("onRebind()");
		super.onRebind(intent);
	}
	
	public class MyBinder extends Binder{
		MyService getService(){
			return MyService.this;
		}
	}
	
	public String getSystemTime(){
		return System.currentTimeMillis()+"";
	}
}
当我启动服务时,可看见log输出onCreate()->onBind()->onServiceConnected
unbindService(serconn);
当我们停止服务时,可看见log输出onUnbind()->onDestroy()
 
 
 
 

                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值