Android四大组件初识之Service

Service作为Android四大组件之一,可以与Activity建立双向连接(绑定模式),提供数据和功能。也能够接收Intent单方面请求(调用模式),进行数据处理和调度功能。

Service与Activity一样运行在主线程(UI线程),所以在Service中执行耗时操作,可能会造成Activity不能及时响应用户的交互请求,然后程序就被系统干掉啦。

个人看法 四大组件都是运行在主线程里面,应该有一个消息循环在调度。不同的操作,不同的消息。例开启一个Service,对应消息的处理,可能是new 一个Service然后调用onCreate、onStartCommand等。一个消息处理完,又返回消息循环处理下一个消息。至于消息可以用队列存储。

生命周期

Service只有在一开始创建实例的时候执行onCreate,后面所有的请求,无论是调用模式还是绑定模式都是在该实例里面处理。Service实例创建后只有两种销毁的方法,系统回收或者显示执行了stopService方法。在Service销毁的时候调用onDestroy。

调用模式

通过Context.startService请求一个Service的方式,称为调用模式。该方式Context就打个招呼,告诉Service该做事情了。Service的实例收到请求就调用自己的onStartCommand方法。

1、使用Intent确定请求的Service,然后startService。

 

2、Service主要是onCreate初始化,onStartCommand执行动作,或者发送一个开始的消息(Handler机制)。

代码

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class StickyService extends Service {
    private int callsum=0;
    
	public StickyService() {
    }
    @Override
    public void onCreate() {
        super.onCreate();
        callsum++;
        Log.d("StickyService","..............onCreate callSum="+callsum);
    }
	
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        callsum++;
        Log.d("StickyService","..............onStartCommand callSum="+callsum);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        callsum++;
        Log.d("StickyService","..............onDestroy callSum="+callsum);
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

  

为了验证Service一直只有一个实例,用一个int变量callSum做访问统计。结果如下。

onStartCommand要求返回一个标志,有三种标志START_REDELIVER_INTENT、START_STICKY、START_NOT_STICKY。三种标志代表的是系统在内存不足回收该Service之后不同的操作模式。

l  START_STICKY在回收之后,一旦系统有充足的资源便要重新创建该Service的实例,并且调用onStartCommand,intent为null。此模式除非显示调用stopService,不然它就“不死”。

l  START_REDELIVER_INTENT操作相同,但是intent值为给Service收到的最后一个请求intent。这个模式关注的是每个intent的处理,用来确保每个intent的请求都能完成。

l  START_NOT_STICKY说明系统可以无条件回收,也不用重新创建Service的实例。一般用于长期运行的Service,在onStartCommand中可以创建定时任务来唤醒自己。

 

绑定模式

在Activity和Service之间可以使用绑定模式。调用bindService,发送一个bind请求。如果bind成功,activity可以获得Service一个内部类(自己定义的)的引用,内部类可以访问Service所有的方法和属性。这样就建立一个连接。绑定模式不调用onStartCommand,调用onBind,解绑调用onUnBind.

Service代码

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.util.Log;

public class BindService extends Service {
	public BindService() {
	}
	@Override
	public Binder onBind(Intent intent) {
		// TODO: Return the communication channel to the service.
		return new TBinder();
	}	
	@Override
	public boolean onUnbind(Intent intent) {
		return false;
	}
	@Override
	public void onCreate() {
		super.onCreate();
		Log.d("BindService",".................... onCreate");
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d("BindService","....................onStartCommand");
		return START_NOT_STICKY;
	}
	//创建一个Binder的子类。TBinder是一个内部类,可以访问Service的所有属性和方法。
	public class TBinder extends Binder {
		public String printServiceName(){
			return "BindService";
		}
	}
}

  

Activity代码

void onBindSer(){
	Intent intent =new Intent(MainActivity.this,BindService.class);
	intentList.add(intent);
	/**
	* sc为绑定成功或者失败之后的回调
	* flag=BIND_AUTO_CREATE表示创建一个Service
	*/
	bindService(intent,sc,BIND_AUTO_CREATE);
}
//创建服务绑定的回调接口对象
ServiceConnection sc=new ServiceConnection() {
	@Override
	public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
		//传入参数很明显了,一个是组件名,一个是IBinder
		Log.d("MainActivity",componentName+" bind success");
		BindService.TBinder binder=(BindService.TBinder)iBinder;
		Log.d("MainActivity","serviceName = "+binder.printServiceName());
	}	

	@Override
	public void onServiceDisconnected(ComponentName componentName) {
		Log.d("MainActivity",componentName+" bind failed ");
	}
};

  

 

日志输出

猜你喜欢

转载自www.cnblogs.com/suen061/p/10587531.html
今日推荐